【发布时间】:2013-03-01 00:54:27
【问题描述】:
我对如何从 access 数据库中获取数据感到有些困惑。先在列表中收集数据然后从列表中获取这些数据是否合适,或者直接在数据库中获取数据是否合适?
我的代码运行良好,但我想知道是否有更好的方法来做到这一点? :
private void button3_Click(object sender, EventArgs e)
{
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\redgabanan\Desktop\Gabanan_Red_dbaseCon\Red_Database.accdb");
connection.Open();
OleDbDataReader reader = null;
OleDbCommand command = new OleDbCommand("SELECT * from Users WHERE LastName='"+textBox8.Text+"'", connection);
reader = command.ExecuteReader();
listBox1.Items.Clear();
while (reader.Read())
{
listBox1.Items.Add(reader[1].ToString()+","+reader[2].ToString());
}
connection.Close();
*我直接从数据库中获取我的记录,然后将其显示在列表框中。
【问题讨论】:
-
为未处理的异常做好准备。