插入sql错误(“where”)

时间:2013-12-18 04:47:55

标签: c# asp.net sql

我创建了一些简单的代码,但看起来我的插入工作有问题我得到关于“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(); 
}

8 个答案:

答案 0 :(得分:3)

SQL Insert进入声明是

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 "

尝试将代码更改为此。

答案 1 :(得分:2)

 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(); 
            }

答案 2 :(得分:1)

INSERT语句没有WHERE条款,UPDATE语句可以。

答案 3 :(得分:1)

如果存在实际的select语句,则只能使用WHERE子句。

这样的东西
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)

答案 4 :(得分:1)

插入语法为:

INSERT INTO table (column1, column2) VALUES (value1, value2)

您的查询应该是

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

答案 5 :(得分:1)

我不确定为什么在将单个记录插入表时使用的位置。下面是插入

的正确代码
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();
            }

答案 6 :(得分:1)

Please, follow below syntex: 
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

=============
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(
  "insert  into dbo.UserInfo (Login, Password, UserType) 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(); 
}

答案 7 :(得分:0)

插入语法为:

INSERT INTO table (column1, column2) VALUES (value1, value2)

只检查您的插入语法,您将得到答案:

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

dbo:xyz =您的表名