我在从 Visual Studio 的 SQL 数据库中检索值时遇到问题。我在 GUEST 表中有列(GuestName、GuestPassportNo、GuestCountry、RoomID、HotelPackageID),其中 HotelPackageID 包含 NULL 值,而其余的则带有值。当 HotelPackageID 为 NULL 时,我总是被抛出 else 语句(未找到记录)。请问有人有解决办法吗?这是我的代码:
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "SELECT Guest.GuestPassportNo,
Guest.GuestName,
Guest.GuestCountry,
Guest.RoomID,
Room.RoomDescription,
Room.RoomPrice,
HotelPackages.PackageName,
HotelPackages.PackagePrice ";
strCommandText += " FROM Guest, Room, HotelPackages";
strCommandText +=" WHERE Guest.RoomID=Room.RoomID
AND Guest.GuestPassportNo=@guestpno
AND HotelPackages.PackageID=Guest.HotelPackageID
AND Guest.GuestCountry=@guestcountry;
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@guestpno", txtPassportNo.Text);
cmd.Parameters.AddWithValue("@guestcountry", txtGuestCountry.Text);
try
{
myConnect.Open();
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
txtGuestName.Text = reader["GuestName"].ToString();
txtRoomDescription.Text = reader["RoomDescription"].ToString();
txtRoomPrice.Text = reader["RoomPrice"].ToString();
txtHotelPackage.Text = reader["PackageName"].ToString();
txtPackagePrice.Text = reader["PackagePrice"].ToString();
MessageBox.Show("Guest Record Found!");
}
else
{
Messagebox.Show("No record Found");
}
reader.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.message);
}
finally
{
myConnect.Close();
}