gpt4 book ai didi

c# - SqlException '?' 附近的语法不正确

转载 作者:行者123 更新时间:2023-11-30 19:24:10 32 4
gpt4 key购买 nike

我在查询中遇到 sql 异常:

[SqlException (0x80131904): Incorrect syntax near '?'. Incorrect syntax near the keyword 'User'.]

我做错了什么?这个异常(exception)是什么意思?

protected void Submit_Click(object sender, EventArgs e)
{
string myConnection = @"Data Source=REDDEVIL;Initial..."

SqlConnection conn = new SqlConnection(myConnection);

HttpPostedFile postedFile = FileUpload1.PostedFile;
string fileName = Path.GetFileName(postedFile.FileName);
string fileExtension = Path.GetExtension(fileName);

if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".bmp" ||
fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".png")
{
Stream stream = postedFile.InputStream;
BinaryReader binaryReader = new BinaryReader(stream);
byte[] bytes = binaryReader.ReadBytes((int)stream.Length);

string query2 = "INSERT INTO Manager (ID,Name,Address,Phone,Cell,Email,DOB,Commission,Comments,Photo,User ID,IsActive) VALUES (?ID,?Name,?Address,?Phone,?Cell,?Email,?DOB,?Commission,?Comments,?Photo,?User_ID,?IsActive)";
SqlCommand cmd2 = new SqlCommand(query2, conn);
cmd2.Parameters.AddWithValue("?ID", mgrID.Text);
cmd2.Parameters.AddWithValue("?Name", Name.Text);
cmd2.Parameters.AddWithValue("?Address", address.Text);
cmd2.Parameters.AddWithValue("?Phone", phoneNo.Text);
cmd2.Parameters.AddWithValue("?Cell", CellNo.Text);
cmd2.Parameters.AddWithValue("?Email", email.Text);
cmd2.Parameters.AddWithValue("?DOB", dob.Text);
cmd2.Parameters.AddWithValue("?Commission", commission.Text);
cmd2.Parameters.AddWithValue("?Comments", comments.Text);
cmd2.Parameters.AddWithValue("?Photo", bytes);
cmd2.Parameters.AddWithValue("?User_ID", System.DBNull.Value);
cmd2.Parameters.AddWithValue("?IsActive", System.DBNull.Value);
conn.Open();
cmd2.ExecuteNonQuery();
conn.Close();

Response.Redirect("~/Views/Portal/Dashboard.aspx");
}
else
{
}
}

任何帮助将不胜感激。

最佳答案

要指定它是一个参数,您应该在 sql 查询中使用 @ 而不是 ?。然后在创建参数时,您也不需要 ?

string query2 = "INSERT INTO Manager (ID,Name,Address,Phone,Cell,Email,DOB,Commission,Comments,Photo,User_ID,IsActive) VALUES (@ID,@Name,@Address,@Phone,@Cell,@Email,@DOB,@Commission,@Comments,@Photo,@User_ID,@IsActive)";

SqlCommand cmd2 = new SqlCommand(query2, conn);
cmd2.Parameters.AddWithValue("ID", mgrID.Text);

正如蒂姆在本节中注意到的那样,您正在指定您拥有的字段 ...USER ID,.... - 我假设您缺少一个 _他们俩。 (如果它确实被称为 user 那么请参阅 Tim 对 []

的建议

关于c# - SqlException '?' 附近的语法不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39703478/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com