protected void Button1_Click1(object sender, EventArgs e)
{
try
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/WEB/App_Data/mydata.accdb");
OleDbCommand com = new OleDbCommand("insert into player_reg (p_name,f_name,dob,pob,sex,marital,nation,address,address,state,mob,email,course,college,y_year,sports,voter) values ('" + TextBox1.Text + "', '" + TextBox2.Text + "','" + TextBox3.Text + "', '" + TextBox4.Text + "','" + TextBox5.Text + "', '" + TextBox6.Text + "','" + TextBox7.Text + "', '" + TextBox8.Text + "','" + TextBox9.Text+ "', '" + TextBox10.Text + "','" + TextBox11.Text + "', '" + TextBox12.Text+ "','" + TextBox13.Text+ "', '" + TextBox14.Text + "''" + TextBox15.Text + "', '" + TextBox16.Text + "')", con);
con.Open();
com.CommandType = CommandType.Text;
com.ExecuteNonQuery();
Response.Write("values inserted successfully");
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
3 个解决方案
#1
1
You have adress
2 times in your query, delete it.
您在查询中有2次地址,请将其删除。
OleDbCommand com = new OleDbCommand("insert into player_reg (p_name,f_name,dob,pob,sex,marital,nation, address,address,state,mob,email,course,college, y_year,sports,voter) values ...
OleDbCommand com = new OleDbCommand(“插入到player_reg(p_name,f_name,dob,pob,性别,婚姻,国家,地址,地址,州,暴徒,电子邮件,课程,大学,y_year,体育,选民)价值......
#2
1
There are 17 items in the columns list but only 16 items in the VALUES list. In the columns list you repeated address
twice.
列列表中有17个项目,但VALUES列表中只有16个项目。在列列表中,您重复地址两次。
While I have your attention, constructing SQL statements by "gluing together" raw user input (textbox.Text
values) is very bad practice. You should use a parameterized query instead.
虽然我引起了你的注意,但通过“粘合”原始用户输入(textbox.Text值)来构造SQL语句是非常糟糕的做法。您应该使用参数化查询。
con.Open();
OleDbCommand com = new OleDbCommand(
"insert into player_reg (p_name, f_name, dob, pob, sex, marital, nation, address, state, mob, email, course, college, y_year, sports, voter)" +
"values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", con);
com.Parameters.AddWithValue("?", TextBox1.Text); // p_name
com.Parameters.AddWithValue("?", TextBox2.Text); // f_name
com.Parameters.AddWithValue("?", TextBox3.Text); // dob
// ...and so on...
com.Parameters.AddWithValue("?", TextBox16.Text); // voter
com.ExecuteNonQuery();
con.Close();
#3
0
Field address is repeated in your query -- the number of columns must be equal to number of values in "VALUES" clause separated by ','.
在您的查询中重复字段地址 - 列数必须等于“VALUES”子句中由','分隔的值的数量。
#1
1
You have adress
2 times in your query, delete it.
您在查询中有2次地址,请将其删除。
OleDbCommand com = new OleDbCommand("insert into player_reg (p_name,f_name,dob,pob,sex,marital,nation, address,address,state,mob,email,course,college, y_year,sports,voter) values ...
OleDbCommand com = new OleDbCommand(“插入到player_reg(p_name,f_name,dob,pob,性别,婚姻,国家,地址,地址,州,暴徒,电子邮件,课程,大学,y_year,体育,选民)价值......
#2
1
There are 17 items in the columns list but only 16 items in the VALUES list. In the columns list you repeated address
twice.
列列表中有17个项目,但VALUES列表中只有16个项目。在列列表中,您重复地址两次。
While I have your attention, constructing SQL statements by "gluing together" raw user input (textbox.Text
values) is very bad practice. You should use a parameterized query instead.
虽然我引起了你的注意,但通过“粘合”原始用户输入(textbox.Text值)来构造SQL语句是非常糟糕的做法。您应该使用参数化查询。
con.Open();
OleDbCommand com = new OleDbCommand(
"insert into player_reg (p_name, f_name, dob, pob, sex, marital, nation, address, state, mob, email, course, college, y_year, sports, voter)" +
"values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", con);
com.Parameters.AddWithValue("?", TextBox1.Text); // p_name
com.Parameters.AddWithValue("?", TextBox2.Text); // f_name
com.Parameters.AddWithValue("?", TextBox3.Text); // dob
// ...and so on...
com.Parameters.AddWithValue("?", TextBox16.Text); // voter
com.ExecuteNonQuery();
con.Close();
#3
0
Field address is repeated in your query -- the number of columns must be equal to number of values in "VALUES" clause separated by ','.
在您的查询中重复字段地址 - 列数必须等于“VALUES”子句中由','分隔的值的数量。