我有一个SQL server 2008。包含数据的表(项)结构如下:
ID(int) Name(varchar(50)) Price(money)
1 ArchBook 1200.89
2 Freebie 0.00
3 Board 800.54这些列的数据类型在()中。我有一个选择存储的proc,它将价格作为参数。
create procedure dbo.GetItemByPrice
@price money
as
begin
if @price = '' OR @price is NULL
set @price = ''
select * from dbo.Items
where @price = '' OR price = @price
end当我从C#代码发送0的价格时,结果是不一致的。我在存储的proc中使用强制转换,在if子句(How do I convert from a money datatype in SQL server?)之后再次将其转换为货币。(),但它似乎不起作用。与0相比,我有什么建议吗?如果价格为空,则返回所有记录。C#码
cnn.Open();
string amount = "0";
var command = new SqlCommand("GetItemsByPrice", cnn);
command.CommandType = CommandType.StoredProcedure;
var param = new SqlParameter("@price", SqlDbType.Money);
param.Value = Convert.ToDecimal(amount);
command.Parameters.Add(param);
var dataReader = command.ExecuteReader();
while (dataReader.Read())
{
//printing columns
}
dataReader.Close();
command.Dispose();发布于 2014-12-26 09:22:52
,在我看来,你的逻辑坏了。
我想GetItemByPrize的价格800.54,我将收到与价格"“的项目。只需使用Price字段default value、、和not allow nulls即可。
更改过程:
CREATE PROCEDURE dbo.GetItemByPrice
@price money
as
BEGIN
select * from dbo.Items
where price = @price
END编辑:
如果将文本框中的价格值作为示例:下面是代码示例
decimal price = 0;
decimal.TryPrase(priceTxtBox.Text, out price);
cmd.Parameters.AddWithValue(@"price", price)这将保证当文本框值为"“或无效文本时,价格参数为0。
发布于 2014-12-26 09:14:37
尝尝这个
CREATE PROCEDURE dbo.GetItemByPrice
@price money
as
BEGIN
if @price = '' OR @price is NULL
SET @price = ''
select * from dbo.Items
where @price = '' OR price = @price
ENDhttps://stackoverflow.com/questions/27654953
复制相似问题