我正在创建一个酒店管理系统,我应该根据特定原因取出房间。我使用同一个表中的room_NO
字段从预订表中获取tourist_CNIC
。但我总是得到0,而不是得到适当的房间号码,我分配给所有的房间。贝娄是我的代码:
private int Fetch_Room()
{
int number = 0;
try
{
String str = "SELECT room_NO FROM booking WHERE tourist_CNIC='" + cnic.Content.ToString() + "'";
MySqlDataAdapter da = new MySqlDataAdapter(str, con);
DataTable dt = new DataTable();
da.Fill(dt);
foreach(DataRow row in dt.Rows)
number = Convert.ToInt32(row.ItemArray[0]);
return number;
}
catch (Exception x) { MessageBox.Show("Error: " + x.Message); return number; }
}
答案 0 :(得分:0)
您可以在这种情况下使用MySqlDataReader
。尝试以下解决方案
rivate int Fetch_Room()
{
int number = 0;
try
{
String str = "SELECT room_NO FROM booking WHERE tourist_CNIC='" + cnic.Content.ToString() + "'";
MySqlCommand cmd = new MySqlCommand(str, con);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
number = (int)rdr.GetValue(0);
}
return number;
}
catch (Exception x) { MessageBox.Show("Error: " + x.Message); return number; }
}