c#Access数据库读取错误
我想用这个代码从数据库中读取c#Access数据库读取错误,c#,ms-access,C#,Ms Access,我想用这个代码从数据库中读取 private void button1_Click(object sender, EventArgs e) { connection.Open(); OleDbCommand parancs = new OleDbCommand(); parancs.Connection = connection; parancs.CommandText= "select * from table where Nev='"+textBox1.Te
private void button1_Click(object sender, EventArgs e)
{
connection.Open();
OleDbCommand parancs = new OleDbCommand();
parancs.Connection = connection;
parancs.CommandText= "select * from table where Nev='"+textBox1.Text+"' and Jelszo='"+textBox2.Text+"'";
OleDbDataReader olvas = parancs.ExecuteReader();
MessageBox.Show("SIKERES BEJELENTKEZES");
connection.Close();
}
但我得到了这个错误:
System.Data.dll中发生类型为“System.Data.OleDb.OLEDBEException”的未处理异常
有什么想法吗?首先,您的
连接未定义
您希望真正使用来解决所有这些问题,使其变得更好,并在完成后为您关闭连接。使用Using语句块
,在该语句块中创建的对象被关闭、刷新和释放,然后允许其他进程再次使用资源。这确保了框架将为每个过程采取最佳措施
下面的操作将起作用,请确保在其中设置了连接字符串
private void button1_Click(object sender, EventArgs e)
{
using(SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "your connection_string";
conn.Open();
// Create the command
SqlCommand command = new SqlCommand("select * from table where Nev= @firstParmeter and Jelszo= @secondParameter", conn);
// Add the parameters.
command.Parameters.Add(new SqlParameter("firstParmeter", textBox1.Text));
command.Parameters.Add(new SqlParameter("secondParameter", textBox2.Text));
// Create new SqlDataReader object and read data from the command.
using (SqlDataReader reader = command.ExecuteReader())
{
// while there is another record present
while (reader.Read())
{
// set what you want here as it will be reading all the rows from the query example below
// write the data on to the screen
Console.WriteLine(String.Format("{0} \t | {1} \t | {2} \t | {3}",
// call the objects from their index
reader[0], reader[1], reader[2], reader[3]));
}
}
}
}