下面的代码在运行时总是在insert into 处报错,但我却查不出哪有错,希望大家帮忙看看,谢谢了!
private void button1_Click(object sender, EventArgs e)
{
DbClass db1 = new DbClass();
db1.oleDbConnection1.Open();
string account_m = textBox1.Text;
string name_m = textBox2.Text;
string class_m = textBox3 .Text ;
string sex_m = comboBox2 .Text ;
string password_m = comboBox1 .Text ;
string money_m = "0.00";
string load_m = "0";
string sql2 = string.Format("select * from reader where account='{0}'", account_m);
OleDbCommand cm2 = new OleDbCommand(sql2, db1.oleDbConnection1);
if (null != cm2.ExecuteScalar())
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear(); db1.oleDbConnection1.Close();
MessageBox.Show(" 编号重复,添加新读者失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string sql = string.Format("insert into reader (account , name , sex , class , Loads , password , money )values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", account_m, name_m, sex_m, class_m, load_m, password_m, money_m);
OleDbCommand cm1 = new OleDbCommand(sql, db1.oleDbConnection1);
cm1.ExecuteNonQuery();
db1.oleDbConnection1.Close();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear(); MessageBox.Show(" 新读者添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void button1_Click(object sender, EventArgs e)
{
DbClass db1 = new DbClass();
db1.oleDbConnection1.Open();
string account_m = textBox1.Text;
string name_m = textBox2.Text;
string class_m = textBox3 .Text ;
string sex_m = comboBox2 .Text ;
string password_m = comboBox1 .Text ;
string money_m = "0.00";
string load_m = "0";
string sql2 = string.Format("select * from reader where account='{0}'", account_m);
OleDbCommand cm2 = new OleDbCommand(sql2, db1.oleDbConnection1);
if (null != cm2.ExecuteScalar())
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear(); db1.oleDbConnection1.Close();
MessageBox.Show(" 编号重复,添加新读者失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string sql = string.Format("insert into reader (account , name , sex , class , Loads , password , money )values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", account_m, name_m, sex_m, class_m, load_m, password_m, money_m);
OleDbCommand cm1 = new OleDbCommand(sql, db1.oleDbConnection1);
cm1.ExecuteNonQuery();
db1.oleDbConnection1.Close();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear(); MessageBox.Show(" 新读者添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
解决方案 »
- C#的IO里那个类可以获得文件大小的信息
- 在C#中当我右键点击TreeView节点时,不先点击左键,怎么才能选中右键的节点?
- 我想学C#。NET 但是不知道买那本书好,请大虾们指点迷津!
- dataset的指针移动下一条
- 春节送礼,做过打印的请进!谢谢
- FileInfo[]我想移除其中一个元素,该怎么做?
- c# between附近语法有错误?
- 一个新手的迷惘,请大家给点意见,来者有分...
- 用C#能写出EXE文件吗?就是用C#能开发应用程序吗?
- EF mvc4 ORACLE 配置问题 在线等! 连接数据库
- 怎样触发一个事件,使winform中的checkedListBox1的所有项都选择
- 转义字符问题 ‘\’
单步跟踪查看sql
还有单引号 与双引号的区别
这些错误都应该考虑
写一个
try
{
}
catch(Exeception)
{
}
试试看,如果还没出来,那只能说明你的RP有问题了,呵呵
一般在表的名称最好在前加上前缀 如:tbl_User ,表的字段名也加上前缀 如:表tbl_User的字段可以加上
u_name 这样虽然麻烦但是养成习惯了就非常的好,可以防止不同表的字段重名等.
你可以看一下VS或者微软生成的所有sql语句的字段都有加上[]的,这样能防止错误