2010-11-05 47 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