我正在尝试让这些文本框显示来自数据库的数据,我能够显示数据,但每次我尝试更新它都不会保存。这就是我所做的
private void ClubRecord_Load(object sender, EventArgs e)
{
try
{
sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Eastern_Property_Maintenance.mdb";
dbConn = new OleDbConnection(sConnection);
dbConn.Open();
sql = "SELECT * FROM Club ORDER BY CompanyName, CompanyAddress, CompanyPhone;";
dbCmd = new OleDbCommand();
dbCmd.CommandText = sql;
dbCmd.Connection = dbConn;
dbreader = dbCmd.ExecuteReader();
while (dbreader.Read())
{
string CompanyName = dbreader["CompanyName"].ToString();
string CompanyAddress = dbreader["CompanyAddress"].ToString();
string CompanyPhone = dbreader["CompanyPhone"].ToString();
txtCompanyName.Text = CompanyName;
txtCompanyAddress.Text = CompanyAddress;
txtCompanyPhone.Text = CompanyPhone;
}
dbreader.Close();
// dbConn.Close();
}
catch (System.Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
}
private void btnConfirmChanges_Click(object sender, EventArgs e)
{
string companyName = txtCompanyName.Text;
string companyAddress = txtCompanyAddress.Text;
string companyPhone = txtCompanyPhone.Text;
string Update = "UPDATE [Club] SET [CompanyName]= @CompanyName,[CompanyAddress]=@CompanyAddress,[CompanyPhone]=@CompanyPhone";
OleDbCommand dbcmd = new OleDbCommand(Update, dbConn);
dbCmd.Parameters.AddWithValue("@CompanyName", companyName);
dbCmd.Parameters.AddWithValue("@CompanyAddress", companyAddress);
dbCmd.Parameters.AddWithValue("@CompanyPhone", companyPhone);
try
{
dbCmd.ExecuteNonQuery();
MessageBox.Show("Update Complete");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
}
非常感谢任何帮助。
答案 0 :(得分:0)
这是一个微妙的拼写错误,但我认为这里有一些东西需要学习,所以我想给出一个完整的答案:
您创建一个名为dbcmd
OleDbCommand dbcmd = new OleDbCommand(Update, dbConn);
然后填写不同命令dbCmd
的参数集合并执行此命令。
dbCmd.Parameters.AddWithValue("@CompanyName", companyName);
dbCmd.Parameters.AddWithValue("@CompanyAddress", companyAddress);
dbCmd.Parameters.AddWithValue("@CompanyPhone", companyPhone);
C#区分大小写,因此两个命令不是同一个命令,但是如何导致更新失败是值得一看的。
您已声明名为dbCmd
的全局OleDbCommand变量,并且此变量仍设置为用于加载文本框的SELECT查询。因此,当您致电ExecuteNonQuery
时,它不会更新任何内容。
您只需将更新后的代码中的所有引用从dbCmd
更改为dbcmd
即可解决问题。但是,这里要学习的教训是,尽可能避免使用全局变量。
Connection是全局变量的另一个例子,这是最糟糕的,因为它在初始表单加载后保持打开状态。不要那样做。 Connectiosn保留了宝贵的系统资源,因此应在使用using statement模式
后立即关闭private void btnConfirmChanges_Click(object sender, EventArgs e)
{
string companyName = txtCompanyName.Text;
string companyAddress = txtCompanyAddress.Text;
string companyPhone = txtCompanyPhone.Text;
string Update = @"UPDATE [Club] SET [CompanyName]= @CompanyName,
[CompanyAddress]=@CompanyAddress,
[CompanyPhone]=@CompanyPhone"; // A where here should be mandatory
using(OleDbConnection dbConn = new OleDbConnection(aGlobalConnectionStringWouldBeSafeHere))
using(OleDbCommand dbcmd = new OleDbCommand(Update, dbConn))
{
dbcmd.Parameters.AddWithValue("@CompanyName", companyName);
dbcmd.Parameters.AddWithValue("@CompanyAddress", companyAddress);
dbcmd.Parameters.AddWithValue("@CompanyPhone", companyPhone);
try
{
dbcmd.ExecuteNonQuery();
MessageBox.Show("Update Complete");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
}
}
顺便说一下,我希望你的表只包含一个记录,因为没有WHERE子句你可以用文本框的值更新每个记录。