将字节从c#传递给sql server [英] Pass byte from c# to sql server
本文介绍了将字节从c#传递给sql server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有UserAgent二进制列的sql表,其中我存储了一个标识用户的位掩码。
例如
00000000默认值(所有使用者)。
00000010支持Safari
00000100支持Opera
等
当我将一个字节值传递给sp时,得到消息:
无法将参数值从字节转换为字节[]。
任何想法为什么会这样什么不行?
谢谢,
罗伯特
I have sql table with column UserAgent binary where I store a bitmask that identifies a useragent.
For instance
00000000 default (all useragents).
00000010 supports Safari
00000100 supports Opera
etc
When I pass a byte value to the sp, get the message:
"Failed to convert parameter value from a Byte to a Byte[]."
Any idea why this does not work?
Thanks,
Robert
TABLE:
create table Menu(MenuId int, ParentId int, MenuText varchar(50), UserAgent binary)
SP:
alter stored procedure GetMenu(userAgent binary=null)
declare @BitPos char
if @UserAgent is null or @UserAgent = 00000000
select @BitPos = 0
else
select @BitPos = charindex(@UserAgent,1)
declare @SQL VarChar(1000)
select @SQL =
'select
MenuID,ParentID,MenuText
from
Menu
where
UserAgent &' + @BitPos + '= ' + @BitPos
exec (@SQL)
METHOD:
public static DataSet GetMenu(byte UserAgent)
{
SqlParameter[] parms = new SqlParameter[1];
parms[0] = new SqlParameter("@UserAgent", SqlDbType.Binary);
parms[0].Value = UserAgent;
cmd = new SqlCommand("GetMenu", conn);
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
return ds;
}
CALL:
Default.aspx.cs:
DataSet ds = GetMenu(00000000);
推荐答案
尝试:
Try:
public static DataSet GetMenu(byte UserAgent)
{
cmd = new SqlCommand("GetMenu", conn);
cmd.Parameters.AddWithValue("@UserAgent", new byte[] {UserAgent});
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
return ds;
}
这篇关于将字节从c#传递给sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文