2013-12-18 4 views
0

간단한 코드를 만들었지 만, 뭔가 잘못되어있는 것처럼 보입니다. "Insert"에 대한 오류가 발생합니다. 나는 무엇을 잘못 했는가?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 절을 그대로 두려면 insert 대신 update를 사용하십시오. – 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

INSERTWHERE 조항이없는, 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

실제 선택 명령문이있는 경우에만 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를 = 귀하의 테이블 이름