简体   繁体   English

如何正确使用 c# 中的更新语句

[英]How can i use update statements in c# correctly

This is my update statement that cosntains a syntax error, please can anyonje point out the error to me im having a real struggle, my knowlede isnt the best.这是我的更新语句,包含语法错误,请anyonje 向我指出错误,我真的很挣扎,我的知识不是最好的。

Updated but sti;ll having errors.已更新但仍有错误。

string sql = $"UPDATE Appointments SET AppDate = {dtpDate.Value.ToShortDateString()}, ContactName = {txtCustomer.Text}, ContactNumber = {txtNumber}, Comment = {txtComment} WHERE ID = {AppID}";
                String contactNo = txtNumber.Text.ToString();
                String comment = txtComment.Text.ToString();
                string date1 = dtpDate.Value.ToString("dd/M/yyyy");
                


                string sql = $"UPDATE Appointments SET AppDate = @Date1, ContactName = @Customer, ContactNumber = @ContactNo, Comment = @Comment WHERE ID = @AppID";

                SqlParameter param = new SqlParameter();
                param.ParameterName = "@Date1";
                param.Value = date1;
                param.ParameterName = "@Customer";
                param.Value = customer;
                param.ParameterName = "@ContactNo";
                param.Value = contactNo;
                param.ParameterName = "@Comment";
                param.Value = comment;
                param.ParameterName = "@AppID";
                param.Value = AppID;```

Asuming datatype of ContactName, ContactNumber and Comments as VARCHAR , dtpDate.Value as Date and ID to be of type int , Your query needs to be have a syntax like this:假设 ContactName、ContactNumber 和 Comments 的数据类型为VARCHAR ,dtpDate.Value 为Date且 ID 为int类型,您的查询需要具有如下语法:

string sql = $"UPDATE Appointments SET AppDate = '{dtpDate.Value.ToString("yyyy-MM-dd")}', ContactName = '{txtCustomer.Text}', ContactNumber = '{txtNumber}', Comment = '{txtComment}' WHERE ID = {AppID}";

During concatenation in C#, you need to make sure that string values are enclosed in single quotes.在 C# 的连接过程中,您需要确保字符串值用单引号括起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM