我有一個計數數據有內部連接的問題。從數據庫使用存儲過程計數數據使用內部連接
我要統計有多少別墅可供選擇,這裏是我的表:
這裏是我的代碼在課堂上讓山寨號碼。
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 [email protected] and dbo.ResortTransactions.Status != 'Cancelled' and dbo.ResortTransactions.ReservedDate != @ReservedDate
RETURN
什麼是錯我的代碼?我希望有人能幫助我:)
在此先感謝!
你的問題是什麼?你會得到例外,得到錯誤的號碼或什麼? –
錯誤/錯誤結果是什麼?你想得到什麼結果? –
我得到錯誤的號碼,我總是得到0! :( –