Cmd.executenonquery()错误消息 [英] Cmd.executenonquery() error message

查看:110
本文介绍了Cmd.executenonquery()错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinForms VS 2017中的更新按钮上有以下代码。

    private   void  btnEdit_Click( object  sender,EventArgs e)
{
if (txtOrder.Text!=
{
cmd = new SqlCommand( UPDATE AER_INBOUND_PLASTICS SET [STATUS] = @ status,[DATE] = @ date,[SOPO_NO] = @ order,[SHIPPER] = @ shipper,[VENDOR] = @ vendor,[WAREHOUSE] = @仓库,[评论] = @评论,[PURCH_PART_CODE] = @ part1,[PURCH_PART_CODE_2] = @ part2,GETDATE(),[MODIFIED_BY] = @ user WHERE ID = @ id,con);
con.Open();
cmd.Parameters.AddWithValue( @ id,ID);
cmd.Parameters.AddWithValue( @ status,comboBoxStatus.SelectedItem.ToString() );
cmd.Parameters.AddWithValue( @ date,txtLoadtime.Text);
cmd.Parameters.AddWithValue( @ order,txtOrder.Text);
cmd.Parameters.AddWithValue( @ shipper,txtShipper.Text);
cmd.Parameters.AddWithValue( @ vendor,txtVendor.Text);
cmd.Parameters.AddWithValue( @ warehouse,comboBoxWarehouse.SelectedItem.ToString() );
cmd.Parameters.AddWithValue( @ comments,txtComments.Text);
cmd.Parameters.AddWithValue( @ part1,txtPart1.Text);
cmd.Parameters.AddWithValue( @ part2,txtPart2.Text);
cmd.Parameters.AddWithValue( @ user,Environment.UserName);
cmd.ExecuteNonQuery();
MessageBox.Show( 记录已成功更新。);
con.Close();
dataGridView1.Refresh();
ClearData();
}
else
{
MessageBox.Show( 请选择要更新的记录。);
}
}

表单正确加载,文本框从DataGridView SelectedRow正确填充。当我修改文本框并单击更新按钮时,我在cmd.ExecuteNonQuery()行上收到以下错误:附加信息:'('。


$ b附近的语法不正确$ b我在任何地方都看不到任何括号。我似乎无法追踪更新过程中的错误。



我是什么尝试过:



我搜索了代码并确实在SqlCommand语句中找不到方括号,但修复它并没有解决问题。我是按照本网站上的说明(http://www.c-sharpcorner.com/uploadfile/1e050f/insert-update-and-delete-record-in-datagridview-c-sharp/)

解决方案

您的代码中有一个随机的 GETDATE(),而您没有分配给任何列。



看起来你复制了早期INSERT语句中的UPDATE语句而忘记将列名放在

哦,为了Pete的缘故! !表中有一个名为MODIFIED的字段,它是修改记录的日期时间。我输入[MODIFIED] = GETDATE(),它完美无缺!谢谢!

I have the following code on an update button in WinForms VS 2017.

private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtOrder.Text != "")
            {
                cmd = new SqlCommand("UPDATE AER_INBOUND_PLASTICS SET [STATUS]=@status,[DATE]=@date,[SOPO_NO]=@order,[SHIPPER]=@shipper,[VENDOR]=@vendor,[WAREHOUSE]=@warehouse,[COMMENTS]=@comments,[PURCH_PART_CODE]=@part1,[PURCH_PART_CODE_2]=@part2,GETDATE(),[MODIFIED_BY]=@user WHERE ID=@id", con);
                con.Open();
                cmd.Parameters.AddWithValue("@id", ID);
                cmd.Parameters.AddWithValue("@status", comboBoxStatus.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@date", txtLoadtime.Text);
                cmd.Parameters.AddWithValue("@order", txtOrder.Text);
                cmd.Parameters.AddWithValue("@shipper", txtShipper.Text);
                cmd.Parameters.AddWithValue("@vendor", txtVendor.Text);
                cmd.Parameters.AddWithValue("@warehouse", comboBoxWarehouse.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@comments", txtComments.Text);
                cmd.Parameters.AddWithValue("@part1", txtPart1.Text);
                cmd.Parameters.AddWithValue("@part2", txtPart2.Text);
                cmd.Parameters.AddWithValue("@user", Environment.UserName);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Updated Successfully.");
                con.Close();
                dataGridView1.Refresh();
                ClearData();
            }
            else
            {
                MessageBox.Show("Please Select Record to Update.");
            }
        }

The form loads correctly, and the textboxes are populated correctly from the DataGridView SelectedRow. When I modify the textboxes and click on the update button, I get the following error on the cmd.ExecuteNonQuery() line: Additional information: Incorrect syntax near '('.

I can't see any parentheses missing anywhere. I can't seem to track down where the error is in the update process.

What I have tried:

I scoured the code and did find a square bracket missing in the SqlCommand statement, but fixing that has not resolved the problem. I am following the instructions on this site (http://www.c-sharpcorner.com/uploadfile/1e050f/insert-update-and-delete-record-in-datagridview-c-sharp/)

解决方案

You have a random GETDATE() in your code that you are not assigning to any column.

Looks like you copied the UPDATE statement from an earlier INSERT one and forgot to put the column name in


Oh for Pete's sake!! There is a field in the table called MODIFIED which is the datetime the record is modified. I put in [MODIFIED]=GETDATE() and it works perfectly! Thanks!!


这篇关于Cmd.executenonquery()错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆