更新数据库与文本框

本文关键字:文本 数据库 更新 | 更新日期: 2023-09-27 18:06:31

我试图让这些文本框显示数据从数据库,我能够显示数据,但每次我尝试更新它不会保存。这是我所做的

    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;
        }
    }

更新数据库与文本框

这是一个细微的打字错误,但我认为这里有一些值得学习的东西,所以我想给出一个完整的答案:

创建一个名为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是另一个全局变量的例子,这是最糟糕的,因为它在初始表单加载后保持打开状态。不要那样做。连接将有价值的系统资源锁定,因此应该在使用后立即关闭(应用using语句模式

)。
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子句,您可以用文本框的值更新每条记录。