2010-11-05 46 views
3

即时新手这里,并希望对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(); 

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

+0

您正在使用的变量是那些文本框名称或字符串值? – 2010-11-05 10:31:10

回答

-1

如果您的ProjectStartDate和日期一般都是DB中的日期时间值,那么在使用'插入数据时会出错。 它应该是这样的:

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

总是使用SQL参数aka参数化查询 – abatishchev 2010-11-05 10:57:20

+0

我知道......但这只是为了帮助他并解决他的问题:) – 2010-11-05 14:01:16

11

首先,你的代码是成熟SQL Injection attacks - 你真的应该使用参数化查询。另外,如果使用参数,则可以具有某些类型安全性,并且这些值将被正确地转换为SQL Server。

这是很难说这里有什么问题,因为你是串联的值是我们未知的(例如,什么是bidDueDate样子?这是什么thisQuery看你执行它之前是怎样的?)。

我通常会将它作为一个存储过程,将你需要的参数用于插入记录,在我的C#中,我将创建命令对象,并向它添加正确的参数(和类型)。

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

+0

+1针对sql注入 – 2010-11-05 10:33:03

+2

+1为Bobby'; DROP TABLE students; - – 2010-11-05 10:37:18

+0

http://xkcd.com/327/ – abatishchev 2010-11-05 10:39:24

0

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

0

你有'复制到输出目录'属性设置为'始终复制'数据库文件?

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

0

你想做的事,找出什么错误是把

Console.WriteLine(thisQuery); 

StringthisQuery=

这将显示您正在呼叫的分贝正是语句行后的第一件事,从查看输出结果来看,这种说法可能很清楚。

7

至少你的代码应该是这样的:

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; 

也可以将日期转换为指定格式的字符串,以最大限度地减少错误解析的风险(因为c ulture dependent specificity等)在数据库端:

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

感谢您向我展示代码的样子!另一个问题是,线:command.Parameters.AddWithValue(“@ biddingDueDate”,biddingDueDate);只有当biddingDueDate是一个字符串? – jill 2010-11-05 11:12:12

+0

@jill:查看我编辑过的帖子 – abatishchev 2010-11-05 11:23:39

+0

@jill:btw,你可以使用撇号将句法突出显示为注释:'var hello =“world!”;'' – abatishchev 2010-11-05 11:25:14