我的更新c#代码无法正常工作,我可以一次更新两个关系表吗?

时间:2014-03-31 08:21:21

标签: c# windows-applications

我试图一次更新两个表,但是我在更新代码上遇到了一些语法错误你能给我一些想法吗?插入代码工作正常,我试图复制插入代码并编辑更新按钮单击

这是我的代码

    private void button2_Click(object sender, EventArgs e)
    {
        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
        @"Data source= C:\Users\user\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\crt_db.accdb";

        try
        {


            conn.Open();
            String Name = txtName.Text.ToString();

            String AR = txtAr.Text.ToString();
            String Wereda = txtWereda.Text.ToString();
            String Kebele = txtKebele.Text.ToString();
            String House_No = txtHouse.Text.ToString();
            String P_O_BOX = txtPobox.Text.ToString();
            String Tel = txtTel.Text.ToString();
            String Fax = txtFax.Text.ToString();
            String Email = txtEmail.Text.ToString();
            String Item = txtItem.Text.ToString();
            String Dep = txtDep.Text.ToString();
            String k = "not renwed";


            String Remark = txtRemark.Text.ToString();

            String Type = txtType.Text.ToString();
            String Brand = txtBrand.Text.ToString();
            String License_No = txtlicense.Text.ToString();
            String Date_issued = txtDate.Text.ToString();
            String my_querry = "update crtPro set  Name='" + Name + "',AR='" + AR + "',Wereda='" + Wereda + "',Kebele='" + Kebele + "',House_No='" + House_No + "',P_O_BOX='" + P_O_BOX + "',Tel='" + Tel + "',Fax='" + Fax + "',Email='" + Email + "',Item='" + Item + "',Dep='" + Dep + "','" + k + "',Remark='" + Remark + "' where Name='" + Name + "' ";
            OleDbCommand cmd = new OleDbCommand(my_querry, conn);
            cmd.ExecuteNonQuery();


            String my_querry1 = "SELECT max(PID) FROM crtPro";
            OleDbCommand cmd1 = new OleDbCommand(my_querry1, conn);

            string var = cmd1.ExecuteScalar().ToString();





            String ki = txtStatus.Text.ToString();
            String my_querry2 = "update crtItemLicense set PID=" + var + ",Type='" + Type + "',Brand='" + Brand + "',License_No='" + License_No + "',Date_issued='" + Date_issued + "' where PID=" + var + "";
            OleDbCommand cmd2 = new OleDbCommand(my_querry2, conn);
            cmd2.ExecuteNonQuery();
            MessageBox.Show("Message added succesfully");

        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed due to" + ex.Message);
        }
        finally
        {
            conn.Close();

        }

1 个答案:

答案 0 :(得分:0)

基于所提供的少量信息(例如,您使用的是什么数据库 - SQL Server 2012?)的最可能的问题是,您在连接的动态sql中提供的数据类型不匹配数据库中列的数据类型。您已使用引号包围每个值 - 这意味着它将被解释为varchar。如果您的日期值格式错误(例如,如果Date_Issued是日期列)或者它是数字列,那么它将会出错。

解决方案是使用参数化查询替换动态SQL,例如:

String my_querry = "update crtPro set Name=@name, AR=@ar, Wereda=@Wereda, etc ...";
OleDbCommand cmd = new OleDbCommand(my_querry, conn);
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@name", Name);
cmd.Parameters.AddWithValue("@myParam", Convert.ToDateTime(txtDate.Text.Trim()));
...
cmd.ExecuteNonQuery();

您可以进一步了解here

PS确保您的参数与SQL中使用的参数顺序相同,因为oledbcommand实际上并不关心您所谓的参数。见here