-----------------C#问题非高手务进-------------------

编程小妞儿 2010-01-28 10:07:34
下面是原是C#+SqlServer
我想把数据库换成 C#+Access 但Access没有SqlTypes这个方法,怎么把中间注释的部分换成跟Access数据库相关的方法

有好的回答再加分
引用原文:http://blog.csdn.net/CodingMouse/archive/2008/12/14/3515969.aspx

using (OleDbConnection conn = new OleDbConnection(connectionString))
{

OleDbCommand cmd = new OleDbCommand(sqltxt, conn);
conn.Open();
//MessageBox.Show(" // 执行 SQL 命令");
using (OleDbDataReader myReader = cmd.ExecuteReader(
CommandBehavior.CloseConnection |
CommandBehavior.SingleResult |
CommandBehavior.SingleRow))
{
if (myReader.Read())
{
//将数据集转换成实体集合
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.Equals(""))
//{
//tmpOperator.RightsCollection = (bf.Deserialize(bytes.Stream) as Dictionary<string, Model.Rights>);
//}
//else
// throw new Exception(string.Format("操作员 [{0}] 没有任何权限,禁止登录!", tmpOperator.ModelName));


}
else
//如果没有读取到内容则抛出异常
throw new Exception("登录名称或用户密码不正确!");
}
...全文
663 100 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
100 条回复
切换为时间正序
请发表友善的回复…
发表回复
编程小妞儿 2010-02-03
  • 打赏
  • 举报
回复
引用 98 楼 jingliuting 的回复:
读取数据的时候直接读到 字节数组中
byte[] b = new byte[1024];//设成字段长度
myReader.GetBytes(3,0,b,0,b.Length);//读取字段内容
反序列化的时候使用内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream(b);//生成读取流
bf.Deserialize(ms);//反序列化


报错:指定的转换无效!
关键是:tmpOperator.RightsCollection = (bf.Deserialize(ms) as Dictionary <string, Model.Rights>);
不好匹配!!
Rights->存放权限的实体类.跟RightsCollection (权限组)主外键关系.
编程小妞儿 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 98 楼 jingliuting 的回复:]
读取数据的时候直接读到 字节数组中
byte[] b = new byte[1024];//设成字段长度
myReader.GetBytes(3,0,b,0,b.Length);//读取字段内容
反序列化的时候使用内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream(b);//生成读取流
bf.Deserialize(ms);//反序列化
[/Quote]
报错:指定的转换无效!
关键是:tmpOperator.RightsCollection = (bf.Deserialize(ms) as Dictionary<string, Model.Rights>);
不好匹配!!
Rights->存放权限的实体类.跟RightsCollection (权限组)主外键关系.
じоνё靁〃 2010-02-02
  • 打赏
  • 举报
回复
读取数据的时候直接读到 字节数组中
byte[] b = new byte[1024];//设成字段长度
myReader.GetBytes(3,0,b,0,b.Length);//读取字段内容
反序列化的时候使用内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream(b);//生成读取流
bf.Deserialize(ms);//反序列化
yyz985 2010-02-02
  • 打赏
  • 举报
回复
直接用GetBytes不行么?
bytes = dr.GetBytes(0);
using(MemroyStream ms = new MemroyStream(bytes)){
bf.Deser..(ms);}
abaochan 2010-02-02
  • 打赏
  • 举报
回复
引用 55 楼 wanghui0380 的回复:
恩,我是来看高手的



你的话,让我感觉很不舒服,我觉得你在逛动物园,
我认为凡是能进来的,都是高手.
高手就是高手,是工作经验积累的结晶,不是动物.
ccsflyjj 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 fdh120 的回复:]
每天回帖即可获得10分可用分!
[/Quote]
..
mawering 2010-02-02
  • 打赏
  • 举报
回复
只能跟着学习一下
swornccc 2010-02-02
  • 打赏
  • 举报
回复
路过
yo_yokel 2010-02-02
  • 打赏
  • 举报
回复
首先将 sql。access类继承此接口,实现各个数据库的方法。
应用的时候,实例化接口,并调用接口的方法,就达到你想要的效果了。

/// <summary>
/// 数据工厂接口
/// </summary>
public interface IDBFactory
{

IDbConnection GetConnection();
IDbConnection GetConnection(string strConn);
IDbCommand GetCommand();
IDbDataAdapter GetDataAdapter();
IDataReader GetDataReader(IDbCommand myDbCommand);
IDataParameter GetDataParameter(string strColumn, object strValue,
int size, int dbtype, int direct);
}
HooverHuang 2010-02-02
  • 打赏
  • 举报
回复
使用工厂模式,定义相同的接口,再用不同的数据库来实现都可以了
huyou1983218 2010-02-02
  • 打赏
  • 举报
回复
DbType通用可是,上面写多了
huyou1983218 2010-02-02
  • 打赏
  • 举报
回复
SqlTypes通用,哥们
bnuepwyl 2010-02-02
  • 打赏
  • 举报
回复
都说了 不是高手不让进 你看看 你们都进来干嘛

我是进来带你们出去的
sklkitty 2010-02-02
  • 打赏
  • 举报
回复
偶在这里还遇到高手呀,来看看高手的,学习。
51Crack 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 huminghua 的回复:]
看了咋的一定要高手才能进呢?疑问?那我拿10分走人咯!
[/Quote]
非高手必进,所以我进来看看!
CqCoder 2010-02-02
  • 打赏
  • 举报
回复
up
m_struggle 2010-02-02
  • 打赏
  • 举报
回复
没有真正的高手
Justin-Liu 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 40 楼 goscan 的回复:]
LZ叫非高手务(必)进,我就来了
[/Quote]
v
iStringTheory 2010-02-02
  • 打赏
  • 举报
回复
不是高手,飘过~
vconan 2010-02-02
  • 打赏
  • 举报
回复
我进来看看高手的。。。
加载更多回复(79)

111,112

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧