下面的代码在运行时总是在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);
            }
        }

解决方案 »

  1.   

    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);
    单步跟踪查看sql
      

  2.   

    sql,在调试状态下放查询分析器里跑一下
      

  3.   

    name 是关键字,请用标准的SQL语句
      

  4.   

    设端点 调试 再看下语句 语法 以及字段是否为系统默认(用[字段名])
    还有单引号 与双引号的区别 
    这些错误都应该考虑
    写一个
    try
    {
       
    }
    catch(Exeception)
    {
       
    }
     试试看,如果还没出来,那只能说明你的RP有问题了,呵呵
      

  5.   

    在sql语句中最好不要用sql语句的关键字作为表的字段或表的名称,如果要用就必须加上[] 如:[user]
    一般在表的名称最好在前加上前缀 如:tbl_User ,表的字段名也加上前缀 如:表tbl_User的字段可以加上 
    u_name 这样虽然麻烦但是养成习惯了就非常的好,可以防止不同表的字段重名等. 
      

  6.   

    name是sql server 的关键字。
    你可以看一下VS或者微软生成的所有sql语句的字段都有加上[]的,这样能防止错误