我正在编写一个必须具有以下能力的应用程序:
- 应该在 Web 界面上看到 GUI(因此我使用的是 C# Web 应用程序)
- 能够选择 .mdb 文件
- 添加行
- 删除行
- 编辑行
- 将所有内容保存回 .mdb 文件。
我已经用谷歌搜索了很多年。
我在一个普通的 C# windows 窗体应用程序中编写了一个类似的应用程序,我使用 OLEdb 来做所有事情,它工作得很好,它不仅限于一个 .mdb 文件。
在 C# Windows 窗体应用程序中,我使用了 Gridview 控件。由于 Web 应用程序没有 Gridview,我使用了数据网格。
这是到目前为止的代码:
public partial class _Default : System.Web.UI.Page
{
string databasePath = "____________.mdb";
DataTable userTable = new DataTable();
OleDbDataAdapter adapter;
OleDbConnection connection;
OleDbCommand command;
OleDbCommandBuilder builder;
DataSet ds;
DataSet tempDs;
public string DatabaseName = null;
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
protected void Page_Load(object sender, EventArgs e)
{
tbDatabasePath.Text = databasePath;
}
protected void btnLoadData_Click(object sender, EventArgs e)
{
ReadRecords();
}
protected void btnGetTables_Click(object sender, EventArgs e)
{
try
{
ddTables.Items.Clear();
ddTables.Text = null;
getTables();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Could not retrieve the Tables');", true);
}
}
private void getTables()
{
string tempstring = "";
string getTConnection = connectionString + databasePath; ;
OleDbConnection myConnection = new OleDbConnection(getTConnection);
myConnection.Open();
DataTable datT = myConnection.GetSchema("Tables");
for (int i = 0; i < datT.Rows.Count; i++)
{
tempstring = datT.Rows[i][2].ToString();
if (tempstring.Contains("MSys"))
{
}
else
{
ddTables.Items.Add(datT.Rows[i][2].ToString());
}
}
myConnection.Close();
}
private void ReadRecords()
{
datagrid.DataSource = null;
OleDbDataReader reader = null;
try
{
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + databasePath);
OleDbCommand cmd = new OleDbCommand("Select * FROM " + ddTables.Text, connection);
adapter = new OleDbDataAdapter(cmd);
builder = new OleDbCommandBuilder(adapter);
ds = new DataSet("MainDataSet");
tempDs = new DataSet("TempDataSet");
connection.Open();
adapter.Fill(tempDs);
tempDs.Clear();
tempDs.Dispose();
adapter.Fill(ds, ddTables.Text);
userTable = ds.Tables[ddTables.Text];
}
catch
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Could not load the data');", true);
}
finally
{
if (reader != null) reader.Close();
//if (connection != null) connection.Close();
datagrid.ShowFooter = true;
}
CreateTempTable(0, 10);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('DONE');", true);
}
private void ReadRecords2()
{
// datagrid.DataSource = null;
OleDbDataReader reader = null;
try
{
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + databasePath);
OleDbCommand cmd = new OleDbCommand("Select * FROM " + ddTables.Text, connection);
adapter = new OleDbDataAdapter(cmd);
builder = new OleDbCommandBuilder(adapter);
ds = new DataSet("MainDataSet");
tempDs = new DataSet("TempDataSet");
connection.Open();
adapter.Fill(tempDs);
tempDs.Clear();
tempDs.Dispose();
adapter.Fill(ds, ddTables.Text);
userTable = ds.Tables[ddTables.Text];
}
catch
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Could not load the data');", true);
}
finally
{
if (reader != null) reader.Close();
//if (connection != null) connection.Close();
}
CreateTempTable(0, 10);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('DONE');", true);
}
private void CreateTempTable(int startRecord, int noOfRecords)
{
try
{
userTable.Rows.Clear();
adapter.Fill(ds, "");
userTable = ds.Tables[""];
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Could not load the data in CreateTempTable');", true);
}
datagrid.DataSource = userTable.DefaultView;
datagrid.DataBind();
}
protected void datagrid_RowEditing(object sender, GridViewEditEventArgs e)
{
datagrid.EditIndex = e.NewEditIndex;
ReadRecords();
}
protected void datagrid_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void datagrid_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
adapter.Update(userTable);
}
protected void datagrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
adapter.Update(userTable);
ReadRecords();
}
protected void datagrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
datagrid.EditIndex = -1;
ReadRecords();
}
protected void datagrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rowToDelete = e.RowIndex;
datagrid.DeleteRow(rowToDelete);
adapter.Update(userTable);
ReadRecords();
}
}
}
它可以 :
- 获取 .mdb 文件
- 获取文件中的表格并显示它
- 从选定的表中读取数据并在gridview中显示
- 添加编辑删除按钮
现在,当我单击编辑按钮时,它使选定的行可编辑。但是当我单击更新按钮时,我得到“对象引用未设置为对象的实例”。该代码在表单应用程序中工作..
有什么办法适配器.Update(userTable); 将能够像在表单应用程序中一样工作吗?
谁能帮我看看我的问题。如果我只能将更改保存回 .mdb 文件,我会非常高兴。