您的位置:程序门 -> .net技术 -> c#



c# 类库访问数据库出错,求助.


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


c# 类库访问数据库出错,求助.
发表于:2007-01-29 15:26:19 楼主
我做了一个数据库sql连接类(condb),主要负责与数据库端的通信
然后我的主程序(test)引用其访问数据库,这步是正常的
接下来我再写一个winform的程序(leave)编译成类库(leave.dll),其中引用了condb类对数据库进行操作.然后我在主程序中实例化leave类,结果无论如何都访问不到数据库.在open方法的时候直接报错,说没有将对象实例化.
求助~希望各位能解答.谢谢
发表于:2007-01-29 15:31:011楼 得分:0
贴代码   或者要不要看我的?
发表于:2007-01-29 15:52:242楼 得分:0
你可以调试看看阿。或者把代码贴出来。
发表于:2007-01-29 15:58:083楼 得分:0
贴代码?
呵呵~我都不知道从那里贴比较好啊,太长了..
.......能看看你的吗?
condb就是里封装了几个command以及adapter的方法
在leave这个类里调用condb封装的方法进行了数据库的读写
在test里调用leave,并show了出来..结果就造成了上述情况

///condb   start///
using   system;
using   system.data;
using   system.data.sqlclient;
using   system.web;

namespace   condb
{
///   <summary>
///   class1   的摘要说明。
///   </summary>
public   class   connectdb
{
private   sqlconnection   sqlcon;
private   sqlcommand   cmd;
private   sqldataadapter   dap;
dataset   ds;
string   sqlconstr   =   " ";

public   connectdb()
{
                sqlconstr   =   getconstr(path1);

}

protected   string   getconstr(string   path)
{
string   constr;
constr   =   @ "server=.;database=fff;user   id=f;password=f; ";
return   constr;
}

//跟踪代码就是报错在这里,但是我想不出这里有什么问题
public   bool   condb()
{
try
{
sqlcon   =   new   sqlconnection(sqlconstr);
if(sqlcon.state.tostring()   ==   "closed "   ¦ ¦   sqlcon.state.tostring()   ==   "closed ")
{
sqlcon.open();
}
return   true;
}
catch(exception   err)
{
try
{
sqlcon   =   new   sqlconnection(getconstr(@ "server=.;database=fff;user   id=f;password=f; "));
if(sqlcon.state.tostring()   ==   "closed "   ¦ ¦   sqlcon.state.tostring()   ==   "closed ")
{
sqlcon.open();
}
return   true;
}
catch
{
this.close();
return   false;
}
}
}

public   datatable   querydata(string   sqlstr)
{
try
{
dap   =   new   sqldataadapter(sqlstr,sqlcon);
ds   =   new   dataset();
dap.fill(ds, "tab ");
sqlcon.close();
return   ds.tables[ "tab "];
}
catch
{
sqlcon.close();
return   null;
}

}

public   void   close()
{
if(sqlcon.state.tostring()   !=   "closed "   ¦ ¦   sqlcon.state.tostring()   !=   "closed ")
{
sqlcon.close();
}
}
}
}

//////////////test
这个的代码没有什么意义,本来就是用来测试的窗体
using   leave;

private   void   button1_click(object   sender,   system.eventargs   e)
{
// leave.form1   a=new   leave.form1();
// a.show();

}

////////leave///////////
using   condb;

private   void   button1_click(object   sender,   system.eventargs   e)
{
            cdb.condb();//这步不能通过
            this.dtpass=cdb.querydata(@ "select   *   from   leave   where   leave_id= ' "+txtleave.text+ " ' ");
            this.dtpass.tablename= "leave ";
            dgidetail.datasource=this.dtpass;
            cdb.close();
}

发表于:2007-01-29 17:25:594楼 得分:0
打开连接之前加上
if(sqlcon.state   ==   connectionstate.open)
{
          sqlcon.close();
}
发表于:2007-02-10 16:09:295楼 得分:0
我跟踪过。。
sqlcon.state在报错前是close的
发表于:2007-02-11 00:06:276楼 得分:0
别管它是不是close,加上试试吧。:)
发表于:2007-02-11 11:01:117楼 得分:0
对,不行你就在用的时候,打开,用完马上关闭连接.
发表于:2007-02-11 11:50:238楼 得分:0
编译成dll


快速检索

最新资讯
热门点击