[英]retrieving data from databases in C#
编程新手,现在正在C#中工作,现在正在尝试一些东西。 我在ms Access中创建了一个包含行和列数据的数据库。 我还创建了一个包含文本框的Windows窗体应用程序。 我现在想做的是编写一个代码,该代码从数据库表中的单个列和行收集每个数据,并将其插入Windows窗体的每个文本框中。 这就是我所做的:
try
{
//...
command.CommandText = "SELECT (Table1.matricule, Table1.name, Table1.department, Table1.specialty, Table1.session) FROM Table1 WHERE (((Table1.matricule)=[textBox6.Text]))";
command.CommandType = CommandType.Text;
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Person p = new Person();
textBox1.Text = reader["Table1.matricule"];
textBox2.Text = reader["Table1.name"];
textBox3.Text = reader["Table1.department"];
textBox4.Text = reader["Table1.specialty"];
textBox5.Text = reader["Table1.session"];
personsList.Add(p);
}
return personsList;
}
catch (Exception)
{
throw;
}
您实际上需要在代码中设置参数值,而不是在字符串中指定参数值:
// Parameterize the query
command.CommandText = "SELECT matricule, name, department, specialty, session FROM Table1 WHERE matricule = ?";
然后在执行之前使用AddWithValue()
方法设置参数:
connection.Open();
// Set your parameter prior to executing the query
command.Parameters.AddWithValue("@matricule",textBox6.Text);
// Now execute your query
OleDbDataReader reader = command.ExecuteReader();
最后,您可能需要在Person
对象上填充属性,而不是设置文本框值(因为Person
对象将为空):
// Populate person properties within your loop
Person p = new Person()
{
Matricule = reader["matricule"],
Name = reader["name"],
Department = reader["department"],
Specialty = reader["specialty"],
}
考虑的重构
您可以与其他一些更改一起实施这些更改(包括使用using
语句来确保正确关闭和处置对象):
// Define your list of people
var people = new List<Person>();
// Create your connection
using(var connection = new OleDbConnection("{your-connection-string}"))
{
try
{
// Define your query (and parameters)
var query = "SELECT matricule, name, department, specialty, session FROM Table1 WHERE matricule = ?";
// Define a using statement
using(var command = new OleDbCommand(query, connection))
{
connection.Open();
// Set your parameter prior to executing the query
command.Parameters.AddWithValue("@matricule",textBox6.Text);
// Execute your query
using(var reader = command.ExecuteReader())
{
// While you have rows, read them
while(reader.Read())
{
people.Add(new Person()
{
Matricule = reader["matricule"],
Name = reader["name"],
Department = reader["department"],
Specialty = reader["specialty"],
});
}
// Return your collection
return people;
}
}
}
catch(Exception ex)
{
// Something blew up, handle accordingly
}
}
给定的代码很少有错误:
1.使用参数化查询代替纯文本:
command.CommandText = "SELECT matricule, name, department, specialty, session FROM Table1 WHERE matricule = ?";
connection.Open();
command.Parameters.Add("@matricule",OleDbType.VarChar).Value= textBox6.Text;
OleDbDataReader reader = command.ExecuteReader();
2.我想不通为什么要使用personsList
这里:如果预期的查询结果将包含多个行,将只得到最后的值显示在文本框。 如果不是多行,则需要向人员对象添加详细信息。
personsList.Add(new Person()
{
Matricule = reader["matricule"],
Name = reader["name"],
Department = reader["department"],
Specialty = reader["specialty"],
});
3.您不需要使用Table1.
每次从读取器对象访问值时。 只需使用以下命令即可阅读它们:
textBox1.Text = reader["matricule"];
// load rest of textboxes
4,整个过程如下所示:
command.Parameters.Add("@matricule",OleDbType.VarChar).Value= textBox6.Text;
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
textBox1.Text = reader["matricule"];
textBox2.Text = reader["name"];
textBox3.Text = reader["department"];
textBox4.Text = reader["specialty"];
textBox5.Text = reader["session"];
personsList.Add(new Person()
{
Matricule = reader["matricule"],
Name = reader["name"],
Department = reader["department"],
Specialty = reader["specialty"],
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.