我可以用什么从数据库中获取两个数据并为它们分配变量? [英] What to use for me to acquire two data from the database and assign variables for them?
本文介绍了我可以用什么从数据库中获取两个数据并为它们分配变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//我不能将查询的两个属性用作变量。只有一个属性。
//I can't use the two attributes of the query as variables. Only one attribute is accommodated.
string myConn = "Server=localhost; user id = root; database = a_project";
//Eventhough my program runs with no errors when I use this string as query, but I can't use both attributes on the query (dBuildings, dID).
//string dQuery = "select building as dBuildings, id as dId from buildings order by building";
string dQuery = "select building as dBuildings from buildings order by building";
MySqlConnection dConnect = new MySqlConnection(myConn);
MySqlCommand dCommand = new MySqlCommand(dQuery, dConnect);
MySqlDataReader dReader;
try
{
dConnect.Open();
dReader = dCommand.ExecuteReader();
while (dReader.Read())
{
string dBuilding = dReader.GetString("dBuildings");
comboBox1.Items.Add(dBuilding);
}
dConnect.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
推荐答案
在您的查询中,fieldname正在构建,因此GetString(building)如果要从中调用的字段名while(dReader.Read())部分
In your query the fieldname is building, so GetString("building") if the fieldname to call from the while (dReader.Read()) part
这篇关于我可以用什么从数据库中获取两个数据并为它们分配变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文