插入sql错误(“where”)
本文关键字:where sql 错误 插入 | 更新日期: 2024-10-22 14:09:38
我创建了一些简单的代码,但看起来我的Insert出现了问题。我做错了什么?
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();
}
SQL Insert
Into语句是
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 "
尝试将代码更改为此。
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
语句没有WHERE
子句,UPDATE
语句有。
只有当存在实际的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)
插入语法为:
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();
}
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();
}
插入语法为:
INSERT INTO table (column1, column2) VALUES (value1, value2)
只需检查你的插入语法,你就会得到答案:
"insert into dbo.xyz(Login, Password, ID) values (@Login, @Password)"
dbo:xyz=您的表名