我正在编写此代码,通过从SQL中的2个表派生值,从前端将数据插入到SQL中。我收到了一个错误:
')'
附近的语法不正确
代码:
try
{
sqlconn.Open();
string PersonalDetailsquery = "insert into PersonalDetails(@FirstName,@LastName,@Email,@Phone,@Address)";
SqlCommand sqlcomm1 = new SqlCommand(PersonalDetailsquery, sqlconn);
sqlcomm1.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
sqlcomm1.Parameters.AddWithValue("@LastName", txtLastName.Text);
sqlcomm1.Parameters.AddWithValue("@Email", txtMailId.Text);
sqlcomm1.Parameters.AddWithValue("@Phone", txtPhoneNum.Text);
sqlcomm1.Parameters.AddWithValue("@Address", txtAddress.Text);
sqlcomm1.ExecuteNonQuery();
sqlcomm1.Parameters.Clear();
string LoginDetailsquery = "insert into LoginDetails(@Username, @Password, @ConfirmPassword, @[Hint Q1], @Answer1, @[Hint Q2], @Answer2, @[Hint Q3], @Answer3)";
SqlCommand sqlcomm2 = new SqlCommand(LoginDetailsquery, sqlconn);
sqlcomm2.Parameters.AddWithValue("@Username", txtUserName.Text);
sqlcomm2.Parameters.AddWithValue("@Password", txtPassword.Text);
sqlcomm2.Parameters.AddWithValue("@ConfirmPassword", txtConfirmPsw.Text);
sqlcomm2.Parameters.AddWithValue("@[Hint Q1]", ddlHintQ1.SelectedItem.Text);
sqlcomm2.Parameters.AddWithValue("@Answer1", txtHintQ1.Text);
sqlcomm2.Parameters.AddWithValue("@[Hint Q2]", ddlHintQ2.SelectedItem.Text);
sqlcomm2.Parameters.AddWithValue("@Answer2", txtHintQ2.Text);
sqlcomm2.Parameters.AddWithValue("@[Hint Q3]", ddlHintQ3.SelectedItem.Text);
sqlcomm2.Parameters.AddWithValue("@Answer3", txtHintQ3.Text);
sqlcomm2.ExecuteNonQuery();
Label1.Text = "data Inserted successfully";
}
catch (Exception ex)
{
Label1.Text = ex.Message;
Label1.ForeColor = System.Drawing.Color.Red;
}
finally
{
sqlconn.Close();
}
答案 0 :(得分:2)
您的查询必须有VALUES
string PersonalDetailsquery = "insert into PersonalDetails(here your columns) VALUES(@FirstName,@LastName,@Email,@Phone,@Address)";
....
string LoginDetailsquery = "insert into LoginDetails(here your columns) VALUES(@Username,@Password,@ConfirmPassword,@[Hint Q1],@Answer1,@[Hint Q2],@Answer2,@[Hint Q3],@Answer3)";
答案 1 :(得分:2)
错误的SQL语法:
试试这个;
class PostList(ArchiveIndexView):
allow_empty = True
allow_future = True
context_object_name = 'post_list'
date_field = 'pub_date'
make_object_list = True
model = Post
paginate_by = 5
template_name = 'blog/post_list.html'
OR
INSERT INTO PersonalDetails (column1,column2,column3,...) VALUES (@FirstName,@LastName,@Email,@Phone,@Address);
答案 2 :(得分:1)
您的SQL语法错误:
"insert into PersonalDetails VALUES (@FirstName,@LastName,@Email,@Phone,@Address)"
和
"insert into LoginDetails VALUES (@Username,@Password,@ConfirmPassword,@[Hint Q1],@Answer1,@[Hint Q2],@Answer2,@[Hint Q3],@Answer3)"
来自w3schools:
可以用两种形式编写INSERT INTO语句。
第一个表单没有指定数据所在的列名 插入,只有他们的价值观:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
第二种形式指定列名和值 插入:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
答案 3 :(得分:-1)
请再次检查您的查询语句:
你可以尝试:
string PersonalDetailsquery ="INSERT INTO PersonalDetails(FirstName,LastName,Email,Phone,Address)
values('"+txtFirstName.Text+"','"+txtLastName.Text+"','"+txtMailId.Text+"','"+txtPhoneNum.Text+"','"+txtAddress.Text+"')";