将文本框值插入数据库

我是一个新手,想要一些关于C#编程的建议

我想将文本框中的值存储到数据库中。 到目前为止,我有以下内容:

string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Customers.mdf;Integrated Security=True;User Instance=True"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); string query = "INSERT INTO ProjectList (ProjectName, BiddingDueDate, Status, ProjectStartDate, ProjectEndDate, AssignedTo, PointsWorth, StaffCredits) VALUES ('"+projName+"', '"+bidDueDate+"', '"+status+"', '"+projectStartDate+"', '"+projectEndDate+"', '"+assignedTo+"', '"+pointsWorth+"', '"+aStaffCredits+"')"; SqlCommand command = new SqlCommand(query, connection); command.ExecuteNonQuery(); connection.Close(); 

代码中没有错误,但我似乎无法弄清楚为什么数据库中没有存储任何内容。

首先,您的代码适用于SQL注入攻击 – 您确实应该使用参数化查询。

此外,如果使用参数,则可以具有某些类型安全性,并且值将正确转换为SQL Server。

很难说出这里有什么问题,因为你连接的值对我们来说是未知的(例如, bidDueDate看起来是什么样的?在执行它之前bidDueDate查询是什么样的?)。

我通常会把它写成一个存储过程,带上插入记录所需的参数,在我的C#中我会创建命令对象,为它添加正确的参数(和类型)。

请参阅此 MSDN页面上的示例(SqlCommand.Parameters)。

至少你的代码应如下所示:

 void SaveData(string projectName, DateTime biddingDueDate, string status, DateTime projectStartDate, string assignedTo, int pointsWorth, string staffCredits) { try { string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Customers.mdf;Integrated Security=True;User Instance=True"; using (SqlConnection connection = new SqlConnection(connectionString)) using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "INSERT INTO ProjectList (ProjectName, BiddingDueDate, Status, ProjectStartDate, ProjectEndDate, AssignedTo, PointsWorth, StaffCredits) VALUES (@projectName, @biddingDueDate, @status, @projectStartDate, @projectStartDate, @assignedTo, @pointsWorth, @staffCredits)"; command.Parameters.AddWithValue("@projectName", projectName); command.Parameters.AddWithValue("@biddingDueDate", biddingDueDate); command.Parameters.AddWithValue("@status", status); command.Parameters.AddWithValue("@projectStartDate", projectStartDate); command.Parameters.AddWithValue("@assignedTo", assignedTo); command.Parameters.AddWithValue("@pointsWorth", pointsWorth); command.Parameters.AddWithValue("@staffCredits", staffCredits); connection.Open(); command.ExecuteNonQuery(); } } catch (SqlException ex) { Console.WriteLine(ex.Message); } } 

参数的类型可以自动确定(尝试):

 command.Parameters.AddWithValue("@biddingDueDate", biddingDueDate); 

或手动指定:

 command.Parameters.Add("@biddingDueDate", System.Data.SqlDbType.DateTime).Value = biddingDueDate; 

您还可以将日期转换为具有指定格式的字符串,以最大限度地降低数据库端错误解析(因为文化依赖的特殊性等)的风险:

 command.Parameters.Add("@biddingDueDate", System.Data.SqlDbType.DateTime).Value = biddingDueDate.ToString("yyyy-MM-dd"); // also you can use just yyyyMMdd 

如果示例中的变量是TextBox,它应该像projName.Text,status.Text一样编写。

您是否将“复制到输出目录”属性设置为“始终复制”数据库文件?

因为这会在每次构建时覆盖您的数据库文件。

你要做的第一件事就是找出出了什么问题

 Console.WriteLine(thisQuery); 

StringthisQuery=

这将向您显示您正在调用Db的语句,并且通过查看输出语句的错误可能很清楚。

如果您的ProjectStartDate和日期通常是数据库中的日期时间值,那么在插入带有’的数据时会出现错误。 应该是这样的:

 String thisQuery = "INSERT INTO ProjectList (ProjectName, BiddingDueDate, Status, ProjectStartDate, ProjectEndDate, AssignedTo, PointsWorth, StaffCredits) VALUES ('"+projName+"', "+bidDueDate+", '"+status+"', "+projectStartDate+", "+projectEndDate+", '"+assignedTo+"', '"+pointsWorth+"', '"+aStaffCredits+"')";