Access 数据库 Insert into 语句的语法错误 10
OleDbConnectioncoon=newOleDbConnection(Form2.odbcConnStr);coon.Open();stringsql="sele...
OleDbConnection coon = new OleDbConnection(Form2.odbcConnStr);
coon.Open();
string sql = "select * from tbuser where name='"+textBox1.Text+"'";
OleDbCommand cmd = new OleDbCommand(sql,coon);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
MessageBox.Show("已存在该用户!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
dr.Close();
string a = textBox1.Text;
string b = textBox2.Text;
string c = textBox3.Text;
string d = textBox4.Text;
sql = "insert into tbuser(name,password,sex,email) values('" + a + "','" + b + "','" + c + "','" + d + "')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
MessageBox.Show("注册成功", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} 展开
coon.Open();
string sql = "select * from tbuser where name='"+textBox1.Text+"'";
OleDbCommand cmd = new OleDbCommand(sql,coon);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
MessageBox.Show("已存在该用户!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
dr.Close();
string a = textBox1.Text;
string b = textBox2.Text;
string c = textBox3.Text;
string d = textBox4.Text;
sql = "insert into tbuser(name,password,sex,email) values('" + a + "','" + b + "','" + c + "','" + d + "')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
MessageBox.Show("注册成功", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
} 展开
推荐于2017-05-17
展开全部
今天使用ACCESS数据库中出现了错误,经过很长时间才解决问题。插入的代码相当简单。查了一些资料后才发现原来是我的ACCESS数据表的字段名使用了SQL的保留关键字。原来语句是:string str = "insert into admain( name,password) values ('" + TextBox1.Text + "','" + TextBox2.Text + "')";将插入数据的SQL语句更改为string str = "insert into admain( [name],[password]) values ('" + TextBox1.Text + "','" + TextBox2.Text + "')";即解决"INSERT INTO 语句的语法错误"的问题.将保留关键字加上中括号([]).在学习中收获分享给大家,以便共同学习。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |