2013-04-16 130 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();