C# 插入sql错误(“其中”)

C# 插入sql错误(“其中”),c#,asp.net,sql,C#,Asp.net,Sql,我创建了一些简单的代码,但看起来我的Insert出现了问题,我得到了关于“where”的错误。我做错了什么 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand( "insert into dbo.UserInfo (Login, Password

我创建了一些简单的代码,但看起来我的Insert出现了问题,我得到了关于“where”的错误。我做错了什么

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(
  "insert  into dbo.UserInfo (Login, Password, UserType, ID) where Login =@Login and Password=@Password  and Type=@UserType ", con);
{
  cmd.Parameters.AddWithValue("@Login",TextBox1.Text );
  cmd.Parameters.AddWithValue("@Password", TextBox2.Text+".123");
  cmd.Parameters.AddWithValue("@Type", DropDownList1.SelectedValue);

  int rows = cmd.ExecuteNonQuery();

  con.Close(); 
}

INSERT
语句没有
WHERE
子句,
UPDATE
语句有。

sqlinsert
语句是

INSERT INTO Table_Name ( Col1, Col2, Col3) 
VALUES ( Val1, Val2, Val3);
我想,

insert into dbo.UserInfo (Login, Password, UserType, ID) 
where Login =@Login and Password=@Password  and Type=@UserType "

尝试将代码更改为此。

只有当存在实际的select语句时,才会使用WHERE子句

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand(
            "insert  into dbo.UserInfo (Login, Password, UserType, ID) " +
              " VALUES(@Login,@Password,@UserType) ", con);
            {
                cmd.Parameters.AddWithValue("@Login",TextBox1.Text );
                cmd.Parameters.AddWithValue("@Password", TextBox2.Text+".123");
                cmd.Parameters.AddWithValue("@Type", DropDownList1.SelectedValue);

                int rows = cmd.ExecuteNonQuery();

              con.Close(); 
            }
差不多

insert  into dbo.UserInfo (Login, Password, UserType, ID) 
SELECT  Login, Password, UserType, ID
FROM    Table
where   Login =@Login 
and     Password=@Password  
and     Type=@UserType
insert  into dbo.UserInfo (Login, Password, UserType, ID) 
VALUES (@Login,@Password,@UserType, @ID)
否则,您只需使用这些值。差不多

insert  into dbo.UserInfo (Login, Password, UserType, ID) 
SELECT  Login, Password, UserType, ID
FROM    Table
where   Login =@Login 
and     Password=@Password  
and     Type=@UserType
insert  into dbo.UserInfo (Login, Password, UserType, ID) 
VALUES (@Login,@Password,@UserType, @ID)
插入语法为:

INSERT INTO table (column1, column2) VALUES (value1, value2)
INSERT INTO table (column1, column2) VALUES (value1, value2)
您的查询应该是

"insert  into dbo.UserInfo (Login, Password, UserType, ID) values (@Login, @Password, @UserType)"

我不知道为什么在表中插入一条记录时使用where。下面是要插入的正确代码

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
            {
                connection.Open();
                string sql = "INSERT INTO UserInfo(Login, Password, UserType) VALUES(@Login,@Password,@Type)";
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.AddWithValue("@Login", TextBox1.Text);
                cmd.Parameters.AddWithValue("@Password", TextBox2.Text + ".123");
                cmd.Parameters.AddWithValue("@Type", DropDownList1.SelectedValue);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                connection.Close();
            }
插入语法为:

INSERT INTO table (column1, column2) VALUES (value1, value2)
INSERT INTO table (column1, column2) VALUES (value1, value2)
只需检查插入语法即可得到答案:

"insert  into dbo.xyz(Login, Password, ID) values (@Login, @Password)"

dbo:xyz=您的表名

关键字“where”附近的语法不正确。如果您试图更新行或离开where子句,请使用update而不是insert。如果您没有写入值,则只定义列,因此会在where处出现错误。只需在前面添加值,谢谢您的帮助,您的解决方案确实对我有用;)