无法使用 C# windows 应用程序在 MS Access 数据库中插入数据

unable to insert data in MS access database using c# windows application

我尝试在 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..!!!");

            }

        }

    }   

可能是您的语法不正确。使用字符串连接构造查询始终不是一个好主意。通过 OdbcCommand.Parameters.Add(...).

使用带有内置参数值处理的参数化查询字符串

您好,您的查询在

行下方使用 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 + "')";

希望对您有所帮助。

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

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