【问题标题】:unable to insert data in MS access database using c# windows application无法使用 c# windows 应用程序在 MS access 数据库中插入数据
【发布时间】:2015-08-23 19:31:47
【问题描述】:

我尝试在 MS access 数据库中插入数据,但数据未添加到数据库中,并且未给出错误。

  private void btnsubmit_Click(object sender, EventArgs e)
    {

        int row = dataGridView1.RowCount;
        for (int i = 0; i < row - 1; i++)
        {
            String str = "INSERT INTO JDS_Data(job_no,order_no,Revision,DesignSpec,Engine_Type,Record_date,LE_IN_Designer,CPH_Designer,Exp_Del_Week,Action_code,Rev_Description,Ref_pattern,Name_of_mock_up,EPC_Drawing,Turbocharger_no_Type,Engine_Specific_Requirement,Draft_sketch_with_details,Air_cooler_type,Description_of_Job,SF_No,Standard,Prority_Sequence,Remark,Part_family,Modified_Date,User)  values('" + txtjobno.Text + "','" + txtorderno.Text + "','" + txtrevison.Text + "','" + txtds.Text + "','" + txtenginetype.Text + "','" + dateTimePicker1.Text + "','" + txtleindesigner.Text + "','" + txtcphdesigner.Text + "','" + txtexpweek.Text + "','" + txtactioncode.Text + "','" + txtrevdescription.Text + "','" + txtrefpatern.Text + "','" + txtmockup.Text + "','" + txtepcdwg.Text + "','" + txtturbono.Text + "','" + txtenginereq.Text + "','" + txtdraft.Text + "','" + txtaircolertype.Text + "','" + txtdespjob.Text + "','" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "','" + txtremark.Text + "','" + dataGridView1.Rows[i].Cells[3].Value.ToString() + "','" + DateTime.Today + "','" + mdlconnection.user_name + "')";

            int dd = mdlconnection.excuteQuery(str);
            MessageBox.Show(str);
            //if (dd > 0)
            {
                MessageBox.Show("Data Saved Successfully..!!!");

            }

        }

    }   

【问题讨论】:

  • 能否请您也显示mdlconnection.excuteQuery?您是否调试过代码并查看发生了什么?你确定你的连接字符串是正确的吗?您的列类型是什么?顺便说一句,您应该始终使用parameterized queries。这种字符串连接对SQL Injection 攻击开放。

标签: c# ms-access-2010 oledb sql-insert insert-into


【解决方案1】:

嘿,您的查询有Syntax 错误使用下面的行

String str = "INSERT INTO JOB_Quality_Rating(Team,JOB_Type,Designer_Name,AUR_NO,Task_No,Sub_Function_no,Severity_Level,Checkpoints,Points_Deducted,Total_Points,Submitted_By,Submitted_Date) values('" + comboBox1.SelectedItem + "','"+comboBox7.SelectedItem+"','" + comboBox3.SelectedItem + "','" + comboBox6.SelectedItem + "','" + cmbtaskno.SelectedItem + "','" + comboBox2.SelectedItem + "','" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "','" + txtTotal.Text + "','" + mdlconnection.user_name + "','" + dateTimePicker1.Text + "')";

代替

String str = "INSERT INTO JOB_Quality_Rating(Team,JOB_Type,Designer_Name,AUR_NO,Task_No,Sub_Function_no,Severity_Level,Checkpoints,Points_Deducted,Total_Points,Submitted_By,Submitted_Date) values('" + comboBox1.SelectedItem + "',,'"+comboBox7.SelectedItem+"','" + comboBox3.SelectedItem + "','" + comboBox6.SelectedItem + "','" + cmbtaskno.SelectedItem + "','" + comboBox2.SelectedItem + "','" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "','" + txtTotal.Text + "','" + mdlconnection.user_name + "','" + dateTimePicker1.Text + "')";

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    可能您的语法不正确。用字符串连接构造查询总是一个坏主意。通过OdbcCommand.Parameters.Add(...) 使用带有内置参数值处理的参数化查询字符串。

    【讨论】:

      【解决方案3】:

      cyberj0g 是对的,你的语法不正确。特别是,您的值列表中缺少一个值:...comboBox1.SelectedItem + "',,'"+comboBox7.SelectedItem...。您不能以这种方式传递空值。要么在此处传递 NULL,要么从字段列表子句中删除相应的字段:...Team,Designer_Name...(以及额外的逗号,或者当然)。

      【讨论】:

        【解决方案4】:

        您可以尝试以下方法 - - 尝试在访问中手动执行查询字符串(在 str 变量中形成的内容),以确保语法没有错误并且成功执行 - 将执行语句包装在 try..catch 块中以获取错误详细信息 - 确保连接指向正确的数据库实例

        【讨论】:

          猜你喜欢
          • 2011-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多