我在從Visual Studio中的SQL數據庫中檢索值時遇到問題。我在GUEST表中有列(GuestName,GuestPassportNo,GuestCountry,RoomID,HotelPackageID),其中HotelPackageID包含一個NULL值,其餘值都是值。當HotelPackageID爲NULL時,我總是被拋入else語句(找不到記錄)。任何人都有解決方法嗎?這裏是我的代碼:從SQL數據庫檢索空值和其他值到文本框
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 [email protected]
AND HotelPackages.PackageID=Guest.HotelPackageID
AND [email protected];
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();
}
我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –