2011-05-24 26 views
1

我使用这个代码片段在我的数据库更新值:更新在ASP.NET

SqlConnection con = new SqlConnection(@"Data Source=SAMA-PC\SQLEXPRESS;Initial Catalog=advCenter;Integrated Security=True"); 
string str = "[email protected]"; 
SqlCommand com2 = new SqlCommand("select [user_Account] from User in str where [user_Email][email protected]", con); 
SqlCommand com = new SqlCommand("update User set [user_Account]=? WHERE [[email protected]]", con); 
com.Parameters.AddWithValue("user_Account",str); 
com.Parameters.AddWithValue("@em",str); 
con.Open(); 
com.ExecuteNonQuery(); 
com2.ExecuteNonQuery(); 
con.Close(); 

,但我得到这个错误关键字“用户”附近

不正确的语法。
第40行:com.ExecuteNonQuery();

回答

3

“用户”是SQL中的保留字。裹在方括号表的名称来指定它的东西名称:你为什么要使用两个单独的SqlCommand对象

[User] 
0

?绝对不需要.....我会尝试更新或选择 - 不要将两个完全独立的操作混合到一个调用中......

另外:您应该使用参数化查询来避免SQL注入攻击,你应该把你的SqlConnectionSqlCommand对象放入使用块中 - 试试这个:

string updateStmt = 
    "UPDATE dbo.[User] SET [user_Account] = @AccountValue WHERE [user_Email] = @UserEMail;"; 

using(SqlConnection con = new SqlConnection(@"Data Source=SAMA-PC\SQLEXPRESS;Initial Catalog=advCenter;Integrated Security=True")) 
using(SqlCommand _cmd = new SqlCommand(updateStmt, con)) 
{ 
    _cmd.Parameters.Add("@AccountValue", SqlDbType.VarChar, 100).Value = str; 
    _cmd.Parameters.Add("@UserEMail", SqlDbType.VarChar, 100).Value = str; 

    con.Open(); 
    _cmd.ExecuteNonQuery(); 
    con.Close(); 
}