2013-04-16 127 views
1
con.Open(); 
cmd2 = new SqlCommand("insert into dailyWorkout('"+RadioButton1.Text+"', '"+RadioButton2.Text+"', '"+RadioButton3.Text+"', '"+RadioButton4.Text+"', '"+RadioButton5.Text+"', '"+Label1.Text+"')", con); 

cmd2.ExecuteNonQuery(); 

嘿,夥計們,一直致力於這個網站了一段時間,但將數據放入數據庫中說asp.net插入數據到數據庫

附近有語法錯誤)「當我得到一個錯誤。

與其他東西,我把它同樣的方式,它的工作原理,這不。

+5

請使用SQL參數;這段代碼很容易被sql注入。 –

+3

請 - ** STOP **連接在一起你的SQL語句!這是(a)對SQL注入攻擊開放,(b)對性能不佳,並且(c)導致這種問題。請改用**參數化查詢**! ***總是*** –

+1

此外,您還沒有指定在哪裏插入(列名)和「值」關鍵字。請檢查http://www.w3schools.com/sql/sql_insert.asp – aliassce

回答

3

你應該真的真的真的使用參數化查詢,以避免SQL注入(並提高性能,並避免與類型轉換等問題)

因此,我建議使用代碼是這樣的:

// define your *parametrized* SQL statement 
string insertStmt = "INSERT INTO dbo.YourTable(Col1, Col2, Col3) VALUES(@Val1, @Val2, @Val3);"; 

// put SqlConnection and SqlCommand into "using" blocks to ensure proper disposal 
using(SqlConnection conn = new SqlConnection("-your-connection-string-here-")) 
using(SqlCommand cmd = new SqlCommand(insertStmt, conn)) 
{ 
    // set the parameters to the values you need 
    cmd.Parameters.AddWithValue("@Val1", "Some String here"); 
    cmd.Parameters.AddWithValue("@Val2", 42); 
    cmd.Parameters.AddWithValue("@Val3", DateTime.Today.AddDays(-7)); 

    // open connection, execute query, close connection right away 
    conn.Open(); 
    int rowsAffected = cmd.ExecuteNonQuery(); 
    conn.Close(); 
}  

要記住的要點:

  • ALWAYS使用parametri zed查詢 - 不會將您的SQL語句連接在一起!
  • SqlConnectionSqlCommandusing(...) { ... }塊,以確保妥善處置
  • 始終明確定義要在SELECT,也儘可能晚地使用INSERT聲明
  • 打開連接,執行查詢列的列表,馬上再次關閉連接
0

這將做這項工作,但我強烈建議使用參數。

con.Open(); 
cmd2 = new SqlCommand("insert into dailyWorkout values ('"+RadioButton1.Text+"', '"+RadioButton2.Text+"', '"+RadioButton3.Text+"', '"+RadioButton4.Text+"', '"+RadioButton5.Text+"', '"+Label1.Text+"')", con); 

cmd2.ExecuteNonQuery(); 

,而不是你上面的代碼更好倒是使用

cmd2 = new SqlCommand("insert into dailyWorkout values (@val1, @val2, @val3,@val4,@val5,@val6)", con); 
cmd2.Parameters.AddWithValue("@val1",RadioButton1.Text); 
cmd2.Parameters.AddWithValue("@val2",RadioButton2.Text); 
cmd2.Parameters.AddWithValue("@val3",RadioButton3.Text); 
cmd2.Parameters.AddWithValue("@val4",RadioButton4.Text); 
cmd2.Parameters.AddWithValue("@val5",RadioButton5.Text); 
cmd2.Parameters.AddWithValue("@val6",Label1.Text) 
    cmd2.ExecuteNonQuery(); 
+1

另外我還建議**在'INSERT'語句中明確列出表的列。使表結構突然變化時,使事情更健壯,避免惱人的問題.... –

+1

我同意@marc_s – aliassce

+0

@marc_s我該怎麼做? –

0

確定其已經提到,不注射參數那樣。 但是,如果你一定要,問題是你最終的SQL字符串看起來像:

insert into dailyWorkout('string1', 'string2', 'string3', 'string4', 'string5', 'string6') 

當它應該是

insert into dailyWorkout(columnName1,columnName2,columnName3,columnName4,columnName5,columnName6) 
values('string1', 'string2', 'string3', 'string4', 'string5', 'string6') 

但你真的應該考慮:

 var sqlCmd = new SqlCommand("insert into dailyWorkout(columnName1,columnName2,columnName3,columnName4,columnName5,columnName6) values(@v1, @v2, @v3, @v4, @v5, @v6)", default(SqlConnection)); 
     sqlCmd.Parameters.Add("@v1", SqlDbType.NVarChar).Value = RadioButton1.Text; 
     sqlCmd.Parameters.Add("@v2", SqlDbType.NVarChar).Value = RadioButton2.Text; 
     sqlCmd.Parameters.Add("@v3", SqlDbType.NVarChar).Value = RadioButton3.Text; 
     sqlCmd.Parameters.Add("@v4", SqlDbType.NVarChar).Value = RadioButton4.Text; 
     sqlCmd.Parameters.Add("@v5", SqlDbType.NVarChar).Value = RadioButton5.Text; 
     sqlCmd.Parameters.Add("@v6", SqlDbType.NVarChar).Value = Label1.Text; 
     sqlCmd.ExecuteNonQuery();