C#通过sql将记录添加到表中 [英] C# Adding a record to a table via sql

查看:72
本文介绍了C#通过sql将记录添加到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

将记录添加到我的表时会引发错误.

Hey guys,

When adding a record to my table its throwing an error.

DA.InsertCommand = new SqlCommand ("INSERT INTO BookDetails VALUES(@BOOKID, @TITLE, @AUTHORFIRSTNAME, @AUTHORSURNAME,@ACQUISITIONDATE,@PUBLISHERNAME,@ISBN,@DDCLASSIFICATION",SC);
           DA.InsertCommand.Parameters.Add("@BOOKID", SqlDbType.NVarChar).Value = txtBookID.Text;
           DA.InsertCommand.Parameters.Add("@TITLE", SqlDbType.NChar).Value = txtTitle.Text;
           DA.InsertCommand.Parameters.Add("@AUTHORFIRSTNAME", SqlDbType.NChar).Value = txtAFirstName.Text;
           DA.InsertCommand.Parameters.Add("@AUTHORSURNAME", SqlDbType.NChar).Value = txtASurname.Text;
           DA.InsertCommand.Parameters.Add("@ACQUISITIONDATE", SqlDbType.Date).Value = txtADate.Text;
           DA.InsertCommand.Parameters.Add("@PUBLISHERNAME", SqlDbType.NChar).Value = txtPubName.Text;
           DA.InsertCommand.Parameters.Add("@ISBN", SqlDbType.NVarChar).Value = txtISBN.Text;
           DA.InsertCommand.Parameters.Add("@DDCLASSIFICATION", SqlDbType.VarChar).Value = txtDDClassification.Text;


           SC.Open();
           DA.InsertCommand.ExecuteNonQuery();
           SC.Close();

           MessageBox.Show("Record Added Successfully");







DDCLASSIFICATION附近的语法不正确,但是拼写正确且类型正确,我在做什么错?








incorrect syntax near DDCLASSIFICATION, yet the spelling is correct, and the type is correct, what am i doing wrong ?


Much help would be appreciated

推荐答案

您忘了右括号!
You forgot the close bracket!
DA.InsertCommand = new SqlCommand ("INSERT INTO BookDetails VALUES(@BOOKID, @TITLE, @AUTHORFIRSTNAME, @AUTHORSURNAME,@ACQUISITIONDATE,@PUBLISHERNAME,@ISBN,@DDCLASSIFICATION",SC);

成为:

DA.InsertCommand = new SqlCommand ("INSERT INTO BookDetails VALUES(@BOOKID, @TITLE, @AUTHORFIRSTNAME, @AUTHORSURNAME,@ACQUISITIONDATE,@PUBLISHERNAME,@ISBN,@DDCLASSIFICATION)",SC);


顺便说一句:在INSERT命令中列出要输入值的字段是一个非常好的主意!否则,如果您的数据库已更改,则可能导致难以发现问题.


BTW: It is a VERY good idea to list the field that the values are being put into as part of teh INSERT command! Otehrswise if your database is changed, it can cause difficult to spot problems.


OriginalGriff,

非常感谢您为我的代码提供帮助,昨晚我花了6个小时来尝试解决它,从更改数据类型到将其转换为字符串.

非常感谢!

ps,最后一点是什么意思,

顺便说一句:最好在INSERT命令的一部分中列出要放入值的字段!否则,如果数据库发生更改,可能会导致难以发现问题."



欢呼声

路易丝:)
OriginalGriff,

Thankyou very much for helping me with my code, i spent 6 hours last night trying to solve it, from changing the datatype, to converting it to string.

Thankyou so much!

Ps what did you mean by the last bit,

"BTW: It is a VERY good idea to list the field that the values are being put into as part of teh INSERT command! Otehrswise if your database is changed, it can cause difficult to spot problems."



Cheers

Louise :)


这篇关于C#通过sql将记录添加到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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