我在C#项目上遇到数据库连接问题。我在MS Access中创建了我的数据库,每当我运行查询命令(方法已分配给特定按钮)时,我在OledbException was unhandled
代码行上出现错误(adapter.Fill(dataset);
)1} p>
没有重要的数据和表达能力。
OR
标准表达式
中的数据类型不匹配
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();
}
}
}
任何人都可以帮忙解释为什么我收到此错误?谢谢!
答案 0 :(得分:0)
任何人都可以帮忙解释为什么我收到此错误?谢谢!
如果您使用的是Microsoft Access(除非他们在另一个版本中更改此内容,以告知Access您需要在dateRecibido
的代码中使用#而不是'。的日期,您前面和后面都是'(撇号)。尝试改为#
正如您所看到的那样,当它可能是一个日期字段时,您传入一个字符串:
var dateRecibido = DateTime.Parse(form1.dateTimePickerFechaRecibida.Value.ToString()).ToShortDateString();
var dateSalido = DateTime.Parse(form1.dateTimePickerFechaSalida.Value.ToString()).ToShortDateString();