"'='附近的语法不正确"运行时错误c#asp.net

vbm*_*n11 1 c# sql asp.net

SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
SqlCommand command = new SqlCommand();

command.CommandText = 
   "INSERT INTO [Users] 
    VALUES (Username=@username, Firstname=@firstname
          , Lastname=@lastname, ProfilePic=NULL, Bio=@bio, Email=@email
          , EmailIsVerified=@emailverified, Hash=@hash, CompanyID=@companyid)";
command.Parameters.AddWithValue("@username", m_username);
command.Parameters.AddWithValue("@firstname", m_firstname);
command.Parameters.AddWithValue("@lastname", m_lastname);
command.Parameters.AddWithValue("@bio", m_bio);
command.Parameters.AddWithValue("@email", m_email);
command.Parameters.AddWithValue("@emailverified", (m_emailIsVerified ? "yes" : "no"));
command.Parameters.AddWithValue("@hash", m_hash);
command.Parameters.AddWithValue("@companyid", m_companyID);
command.CommandType = CommandType.Text;
command.Connection = sqlConnection;

sqlConnection.Open();

command.ExecuteNonQuery();

sqlConnection.Close();
Run Code Online (Sandbox Code Playgroud)

使用上面的代码我得到"语法错误near ="错误.我做错了什么?

Ada*_*ger 6

您需要更改INSERT语句的语法:

INSERT INTO [Users] (UserName, FirstName, ...)
SELECT @UserName, @firstname, ...
Run Code Online (Sandbox Code Playgroud)

有关INSERT语法的文档

明确列出您将要列的列被认为是更好的做法INSERT INTO.如果修改了表的列顺序,这可以防止任何(可能很难解决)问题.这可能导致您INSERT失败(插入无效类型),或将值插入意外列(重新排序列,但它们具有相同的类型,因此插入将成功)