我能够返回表查询以及rowcount吗?我想返回我的表,但我也想通知用户我的存储过程是否没有返回任何内容。
这是我到目前为止所做的:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[pwp_getCouponRedemption]
(
@couponCode varchar(max)
)
AS
SELECT CouponCode, CouponValue
FROM pwpCouponCodes cc
WHERE cc.CouponCode = @couponCode
ORDER BY cc.CouponCode
这是我的asp代码:
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("pwp_getCouponRedemption", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("@couponCode", SqlDbType.VarChar);
comm.Parameters["@couponCode"].Value = couponCode;
try
{
conn.Open();
reader = comm.ExecuteReader();
while (reader.Read())
{
sqlCouponCode = reader[0].ToString();
sqlCouponValue = float.Parse(reader[1].ToString());
}
}
blah blah catch final code...
我对这一切都很陌生,所以任何帮助都会非常感激。谢谢!
答案 0 :(得分:1)
你可以这样做:
首先将存储过程更改为
ALTER PROCEDURE [dbo].[pwp_getCouponRedemption]
(
@couponCode varchar(max)
)
AS
-- This is the first result returned by your SqlDataReader
SELECT COUNT(*)
FROM pwpCouponCodes cc
WHERE cc.CouponCode = @couponCode
ORDER BY cc.CouponCode
-- This is the second result available if the call to NextResult() returns true
SELECT CouponCode, CouponValue
FROM pwpCouponCodes cc
WHERE cc.CouponCode = @couponCode
ORDER BY cc.CouponCode
现在将您的调用代码更改为此类
string connectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
using(SqlConnection conn = new SqlConnection(connectionString))
using(SqlCommand comm = new SqlCommand("pwp_getCouponRedemption", conn))
{
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("@couponCode", SqlDbType.VarChar);
comm.Parameters["@couponCode"].Value = couponCode;
try
{
conn.Open();
using(SqlDataReader reader = comm.ExecuteReader())
{
if (reader.Read())
{
int countRecord = Convert.ToInt32(reader[0]);
if (reader.NextResult())
{
while (reader.Read())
{
sqlCouponCode = reader[0].ToString();
sqlCouponValue = float.Parse(reader[1].ToString());
}
}
}
}
}
}
答案 1 :(得分:0)
将存储过程更改为:
PROCEDURE [dbo].[pwp_getCouponRedemption]
(
@couponCode varchar(max)
)
AS
SELECT CouponCode, CouponValue, 1 AS FoundACouponForYou
FROM pwpCouponCodes cc
WHERE cc.CouponCode = @couponCode
ORDER BY cc.CouponCode;
将您的C#代码更改为:
try
{
bool foundACouponCode = false;
conn.Open();
using(reader = comm.ExecuteReader())
{
if (reader.Read())
{
int countRecord = Convert.ToInt32(reader(0));
if (reader.NextResult())
{
while (reader.Read())
{
sqlCouponCode = reader[0].ToString();
sqlCouponValue = float.Parse(reader[1].ToString());
foundACouponCode = reader[2].ToString() == "1";
}
}
}
if (!foundACouponCode)
MessageBox.Show("Failed to find a coupon code!");
}
}
TBH你甚至不需要改变你的存储过程如果你不想,你可以将foundACouponCode设置为true,如果它通过那段代码,因为它只会到那里有优惠券代码。
答案 2 :(得分:0)
如果您只是想知道存储过程是否返回行,您可以在
上进行测试var zeroRows = reader.HasRows ? "Yes" : "No";
答案 3 :(得分:0)
或者你可以在循环结束后检查sqlCouponCode。它只是在循环内部设置。
if(sqlCouponCode != null)
{
//There are no rows...do something
}