{
//SQL命令
string sqltxt = "Select Id, OperatorName, Password, RightsList, State From Operator";
//创建操作员实体集合
Dictionary<string, Model.Operator> operatorCollection = new Dictionary<string, Model.Operator>();
//定义操作员实体
Model.Operator tmpOperator = null;
// 转换数据库存储的 二进制数据为 Byte[] 数组 以便进而转换为操作员权限集合
string connectionString = ConfigurationManager.ConnectionStrings["SQLSERVER"].ConnectionString;
// 执行 SQL 命令
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(sqltxt, conn);
conn.Open();
using (SqlDataReader myReader = cmd.ExecuteReader(
CommandBehavior.CloseConnection))
{
while (myReader.Read())
{
// 创建操作员实体
tmpOperator = new Model.Operator();
//将数据集转换成实体集合
tmpOperator.Id = Convert.ToInt32(myReader["Id"]);
tmpOperator.ModelName = Convert.ToString(myReader["OperatorName"]);
tmpOperator.Password = Convert.ToString(myReader["Password"]);
tmpOperator.State = Convert.ToBoolean(myReader["State"]);
// 读取权限集合
System.Data.SqlTypes.SqlBytes bytes = myReader.GetSqlBytes(3); // 只能指定列序号
// 将流反序列化为权限集合对象
BinaryFormatter bf = new BinaryFormatter();
if (!bytes.IsNull)
tmpOperator.RightsCollection = (bf.Deserialize(bytes.Stream) as Dictionary<string, Model.Rights>);
// 添加到操作员实体集合
operatorCollection.Add(tmpOperator.ModelName, tmpOperator);
}
}
}
这是sqlserver数据的将二进制转为流然后再反序列化,数据库是mysql的话,不存在GetSqlBytes()这个方法,请问该如何实现??
3 个解决方案
#1
没有GetSqlBytes()这个方法
你只需要把读取出来的转换为bytes就行了
你只需要把读取出来的转换为bytes就行了
#2
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, dt.Rows[i]["USER_RIGHTSlIST"]); “USER_RIGHTSlIST”为varbinary类型
ms.Close();
byte[] bytes = ms.ToArray();
MemoryStream ms1 = new MemoryStream(bytes);
if (bytes != null)
tmpoperator.RightsCollection = (bf.Deserialize(ms) as Dictionary<string, Modal.SysRx.RxRights>);
//添加到操作员实体集合
operatorCollection.Add(tmpoperator.USER_NAME, tmpoperator);
改成了这样,然后报错是流不可读,bytes写到流里有问题啊,请指教?
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, dt.Rows[i]["USER_RIGHTSlIST"]); “USER_RIGHTSlIST”为varbinary类型
ms.Close();
byte[] bytes = ms.ToArray();
MemoryStream ms1 = new MemoryStream(bytes);
if (bytes != null)
tmpoperator.RightsCollection = (bf.Deserialize(ms) as Dictionary<string, Modal.SysRx.RxRights>);
//添加到操作员实体集合
operatorCollection.Add(tmpoperator.USER_NAME, tmpoperator);
改成了这样,然后报错是流不可读,bytes写到流里有问题啊,请指教?
#3
求高手指点下啊?
#1
没有GetSqlBytes()这个方法
你只需要把读取出来的转换为bytes就行了
你只需要把读取出来的转换为bytes就行了
#2
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, dt.Rows[i]["USER_RIGHTSlIST"]); “USER_RIGHTSlIST”为varbinary类型
ms.Close();
byte[] bytes = ms.ToArray();
MemoryStream ms1 = new MemoryStream(bytes);
if (bytes != null)
tmpoperator.RightsCollection = (bf.Deserialize(ms) as Dictionary<string, Modal.SysRx.RxRights>);
//添加到操作员实体集合
operatorCollection.Add(tmpoperator.USER_NAME, tmpoperator);
改成了这样,然后报错是流不可读,bytes写到流里有问题啊,请指教?
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, dt.Rows[i]["USER_RIGHTSlIST"]); “USER_RIGHTSlIST”为varbinary类型
ms.Close();
byte[] bytes = ms.ToArray();
MemoryStream ms1 = new MemoryStream(bytes);
if (bytes != null)
tmpoperator.RightsCollection = (bf.Deserialize(ms) as Dictionary<string, Modal.SysRx.RxRights>);
//添加到操作员实体集合
operatorCollection.Add(tmpoperator.USER_NAME, tmpoperator);
改成了这样,然后报错是流不可读,bytes写到流里有问题啊,请指教?
#3
求高手指点下啊?