-4

我在 C# 项目上的数据库连接确实有问题。我在 MS Access 中创建了我的数据库,每当我运行查询命令(方法已分配给特定按钮)时,我都会在代码行中收到一个错误 ( OledbException was unhandled)adapter.Fill(dataset);

没有巧合的 los tipos de datos en la expresión de criterios。

或者

条件表达式中的数据类型不匹配

namespace Repuesto
{
    public partial class Form2 : Form
    {
        OleDbConnection connection = new OleDbConnection();
        OleDbCommand command = new OleDbCommand();
        DataSet dataset = new DataSet();
        OleDbDataAdapter adapter = new OleDbDataAdapter();

        string direction = Application.StartupPath + "\\Database1.accdb";

        public Form2()
        {
            InitializeComponent();
            InitializeData();
            FillDataGrid();
        }

        public void InitializeData()
        {
            connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + direction;
            connection.Open();
            command = connection.CreateCommand();
            connection.Close();
        }

        private void FillDataGrid()
        {
            dataGridView1.DataSource = null;
            dataset = new DataSet();
            connection.Open();
            command.CommandText = "select * from Consulta1 order by Id";
            adapter = new OleDbDataAdapter(command);
            adapter.Fill(dataset);
            connection.Close();
            dataGridView1.DataSource = dataset.Tables[0];
        }

        public void DeleteRow()
        {
            dataset = new DataSet();
            connection.Open();
            command.CommandText = "delete from Consulta1 where Id = '" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
            adapter = new OleDbDataAdapter(command);
            adapter.Fill(dataset);
            connection.Close();
            for (int i = 1; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].Visible = true;
            }

            FillDataGrid();
        }

        public void AddRow()
        {
            Form1 form1 = new Form1();

            var dateRecibido = DateTime.Parse(form1.dateTimePickerFechaRecibida.Value.ToString()).ToShortDateString();
            var dateSalido = DateTime.Parse(form1.dateTimePickerFechaSalida.Value.ToString()).ToShortDateString();

            dataset = new DataSet();
            connection.Open();
            command.CommandText = "insert into Consulta1([Fecha Recepcion], [Cantidad Recibido], [Denominación], [Ubicación], [Nº Referencía], [Proveedor], [Fecha Salida], [Cantidad Salida]) values "
                + "('" + dateRecibido + "','" + form1.textBoxCantidadRecibido.Text + "','" + form1.textBoxDenominacion.Text
                + "','" + form1.textBoxUbicacion.Text + "','" + form1.textBoxNoReferencia.Text + "','" + form1.textBoxProveedor.Text + "','" + dateSalido + "','" + form1.textBoxCantidadSalido.Text + "')";

            adapter = new OleDbDataAdapter(command);         
            adapter.Fill(dataset);
            connection.Close();
            for (int i = 1; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].Visible = true;
            }

            FillDataGrid();
        }
    }
}

谁能帮助解释我为什么会收到此错误?谢谢!

4

1 回答 1

0

谁能帮助解释我为什么会收到此错误?谢谢!

如果您使用的是 Microsoft Access(除非他们在不同的版本中更改它以告诉 Access 它的日期,您需要使用 # 而不是 '。在您的代码中dateRecibido,您前面和结尾都以 '(撇号)。尝试更改一个 #

如您所见,当它可能是日期字段时,您正在传递一个字符串:

var dateRecibido = DateTime.Parse(form1.dateTimePickerFechaRecibida.Value.ToString()).ToShortDateString();
            var dateSalido = DateTime.Parse(form1.dateTimePickerFechaSalida.Value.ToString()).ToShortDateString();
于 2013-07-05T23:19:14.343 回答