Skip to main content
 首页 » 编程设计

c#之无法从 C#.NET 更新 Access 数据库文件

2024年02月13日12xiaohuochai

我尝试使用 Visual C# 学习一些编码。我创建了一个用于添加和更新 Access 数据库的表单。

我可以成功添加到 Access 文件,但我无法更新它们。

我通过在互联网上的一些搜索写了一个类似下面的代码,但我得到了这个错误:

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

我的代码是:

public partial class form1 : Form 
{ 
   private OleDbConnection con; 
 
   private void btnUpDate_Click(object sender, EventArgs e) 
   { 
            string FirstName = txtFirstName.Text; 
            string Family = txtFamily.Text; 
            string City = txtCity.Text; 
            string approve = txtapprove.Text; 
            string OfficeNumber = txtOfficeNumber.Text; 
            string OfficialDossier = txtOfficialDossier.Text; 
            string Department = txtDepartment.Text; 
            string Organization = txtOrganization.Text; 
 
            OleDbConnection oleDBConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Data Source=F:\\Database.accdb"); 
            string query = "UPDATE Sheet1 SET FirstName=@FirstName, Family=@Family, City=@City, approve=@approve, OfficeNumber=@OfficeNumber, OfficialDossier=@OfficialDossier, Department=@Department, Organization=@Organization WHERE OfficeNumber=@OfficeNumber"; 
 
            //string query = "UPDATE aspnet_Users SET FirstName=@FirstName, Family=@Family,  City=@City, approve=@approve, OfficeNumber=@OfficeNumber, OfficialDossier=@OfficialDossier, Department=@Department WHERE OfficeNumber=@OfficeNumber"; 
 
            OleDbCommand cmd = new OleDbCommand(query, oleDBConn); 
            cmd.CommandType = CommandType.Text; 
            cmd.Parameters.AddWithValue("@OfficeNumber", OfficeNumber); 
            cmd.Parameters.AddWithValue("@OfficialDossier", OfficialDossier); 
            cmd.Parameters.AddWithValue("@FirstName", FirstName); 
            cmd.Parameters.AddWithValue("@Family", Family); 
            cmd.Parameters.AddWithValue("@City", City); 
            cmd.Parameters.AddWithValue("@approve", approve); 
            cmd.Parameters.AddWithValue("@Department", Department); 
            cmd.Parameters.AddWithValue("@Organization", Organization); 
 
            try 
            { 
                con.Open(); 
 
                int result = cmd.ExecuteNonQuery(); 
 
                if (result > 0) 
                    MessageBox.Show("Success!"); 
                else 
                    MessageBox.Show("Sorry!"); 
            } 
            catch (OleDbException) 
            { 
                MessageBox.Show("There is a problem!"); 
            } 
            finally 
            { 
                con.Close(); 
            } 
        } 
    } 

我哪里错了?我不使用 DataSet 和 DataAdapter。问题出在哪里?

我正在使用 VS 2010

请您参考如下方法:

问题 1:您没有打开分配给 OleDbCommand 对象的连接对象 oleDBConn。

您已将 oleDBConn 分配给 OleDbCommand 对象,如下所示:

OleDbCommand cmd = new OleDbCommand(query, oleDBConn);//here you have assigned oleDbConn 

但是您打开了不同的 ConnectionObject con,如下所示:

con.Open(); 

解决方案一:

替换此内容:您应该始终打开分配给 OleDbCOmmand 对象的 OleDbConnection (oleDBConn) 对象。

 con.Open(); 

有了这个:

oleDBConn.Open(); 

问题 2:您已经创建了一个额外的连接对象 con(在您的 btnUpDate_Click 函数之上)并且错误地使用了一样。(打开和关闭错误的连接对象而不是正确的连接对象)

解决方案 2:删除在 btnUpDate_Click 函数之上创建的额外连接对象,并将所有 con 事件替换为 oleDBConn.

完整代码:

try 
{ 
oleDBConn.Open(); 
 
int result = cmd.ExecuteNonQuery(); 
 
if (result > 0) 
MessageBox.Show("Success!"); 
else 
MessageBox.Show("Sorry!"); 
} 
catch (OleDbException ex) 
{ 
MessageBox.Show("There is a problem!"+ex.ToString()); 
} 
finally 
{ 
oleDBConn.Close(); 
}