我在计算带有内部连接的数据时遇到问题。
我想计算有多少小屋可用,这是我的表格:
这是我在课堂上获取小屋号码的代码。
public void CheckCottages()
{
con.Close();
SqlCommand comUmbrella = new SqlCommand("CountCottages", con);
comUmbrella.CommandType = CommandType.StoredProcedure;
comUmbrella.Parameters.Add("@CottageType", SqlDbType.NVarChar).Value = "Umbrella";
comUmbrella.Parameters.Add("@ReservedDate", SqlDbType.DateTime).Value = this.ARRIVAL;
con.Open();
comUmbrella.ExecuteNonQuery();
drUmbrella = comUmbrella.ExecuteReader();
if (drUmbrella.Read())
{
this.UMBRELLA = drUmbrella.GetInt32(drUmbrella.GetOrdinal("Rows"));
}
con.Close();
SqlCommand comNativeKubo = new SqlCommand("CountCottages", con);
comNativeKubo.CommandType = CommandType.StoredProcedure;
comNativeKubo.Parameters.Add("@CottageType", SqlDbType.NVarChar).Value = "Native Kubo";
comNativeKubo.Parameters.Add("@ReservedDate", SqlDbType.DateTime).Value = this.ARRIVAL;
con.Open();
comNativeKubo.ExecuteNonQuery();
drKubo = comNativeKubo.ExecuteReader();
if (drKubo.Read())
{
this.NATIVEKUBO = drKubo.GetInt32(drKubo.GetOrdinal("Rows"));
}
con.Close();
SqlCommand comTreeHouse = new SqlCommand("CountCottages", con);
comTreeHouse.CommandType = CommandType.StoredProcedure;
comTreeHouse.Parameters.Add("@CottageType", SqlDbType.NVarChar).Value = "Tree house";
comTreeHouse.Parameters.Add("@ReservedDate", SqlDbType.DateTime).Value = this.ARRIVAL;
con.Open();
comTreeHouse.ExecuteNonQuery();
drTree = comTreeHouse.ExecuteReader();
if (drTree.Read())
{
this.TREEHOUSE = drTree.GetInt32(drTree.GetOrdinal("Rows"));
}
con.Close();
SqlCommand comPavillion = new SqlCommand("CountCottages", con);
comPavillion.CommandType = CommandType.StoredProcedure;
comPavillion.Parameters.Add("@CottageType", SqlDbType.NVarChar).Value = "Pavillion";
comPavillion.Parameters.Add("@ReservedDate", SqlDbType.DateTime).Value = this.ARRIVAL;
con.Open();
comPavillion.ExecuteNonQuery();
drPavillion = comPavillion.ExecuteReader();
if (drPavillion.Read())
{
this.PAVILLION = drPavillion.GetInt32(drPavillion.GetOrdinal("Rows"));
}
}
这是我的存储过程:
ALTER PROCEDURE dbo.CountCottages
(
@CottageType nvarchar(50),
@ReservedDate datetime
)
AS
SELECT count(dbo.Cottages.CottageName)
FROM dbo.Cottages INNER JOIN
dbo.ResortTransactions ON dbo.Cottages.CottageID = dbo.ResortTransactions.CottageID
where dbo.Cottages.CottageType=@CottageType and dbo.ResortTransactions.Status != 'Cancelled' and dbo.ResortTransactions.ReservedDate != @ReservedDate
RETURN
我的代码有什么问题?我希望有一个人可以帮助我 :)
提前致谢!