2013-12-18 53 views
0

我創建了一些簡單的代碼,但它看起來像是我的插入工作出錯我得到有關「where」的錯誤。我做錯了什麼?插入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 [email protected] and [email protected] and [email protected] ", 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

錯誤是什麼? – Gajendra

+0

關鍵字'where'附近的語法錯誤。 – user2121038

+0

如果您嘗試更新一行或使用where子句,請使用update而不是insert – insomniac

回答

3

SQL Insert進入語句是

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

我認爲,

insert into dbo.UserInfo (Login, Password, UserType, ID) 
where Login [email protected] and [email protected] and [email protected] " 

嘗試將代碼改變了這一點。

+0

感謝您的幫助,您的解決方案對我來說確實有用;) – user2121038

1

INSERT聲明沒有WHERE條款,UPDATE報表做的。

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

感謝您的幫助 – user2121038

1

如果有實際的select語句,那麼只會使用WHERE子句。

喜歡的東西

insert into dbo.UserInfo (Login, Password, UserType, ID) 
SELECT Login, Password, UserType, ID 
FROM Table 
where Login [email protected] 
and  [email protected] 
and  [email protected] 

否則你只是使用的值。像

insert into dbo.UserInfo (Login, Password, UserType, ID) 
VALUES (@Login,@Password,@UserType, @ID) 
1

插入語法的東西是:

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

您的查詢可能應該是

"insert into dbo.UserInfo (Login, Password, UserType, ID) values (@Login, @Password, @UserType)" 
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(); 
      } 
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(); 
} 
0

插入語法:

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

只檢查您的插入語句,你會得到答案:

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

DBO:XYZ =您的表名