如何调用自己写的连接~~
我写了一个数据库连接类
public class sqlConn
{
public sqlConn()
{
SqlConnection Conn=new SqlConnection( "server=localhost;database=bookmanager;uid=sa ");
Conn.Open();
}
……
……
}
然后在一个窗体中我想用这个连接,并使用SqlCommand,那SqlCommand后面的参数怎么写啊,我的代码是这样的
private void Query_Load(object sender, System.EventArgs e)
{
sqlConn connSql=new sqlConn();
strSql= "select * from "+strTabname;
SqlCommand sqlTabname=new SqlCommand(strSql,?);
}
不知道实例化Sqlcommand后的参数怎么写呢,请大家指点~~
------解决方案--------------------public abstract SqlHelper
{
public static SqlConnection GetConn()
{
return new SqlConnection( "连接字符串 ");
}
}
.................
.......
窗体中
private void Query_Load(object sender, System.EventArgs e)
{
strSql= "select * from "+strTabname;
SqlCommand sqlTabname=new SqlCommand(strSql,SqlHelper.GetConn());
}
------解决方案--------------------你的类写的太乱了哦。。。类当然要返回一个Sqlconnection类型的才可以使用啊
public class sqlConn
{
private SqlConnection Conn;
public sqlConn()
{
Conn=new SqlConnection( "server=localhost;database=bookmanager;uid=sa ");
}
public SqlConnection getcon()
{
Conn.Open();
return Conn;
}
……
……
}
private void Query_Load(object sender, System.EventArgs e)
{
sqlConn connSql=new sqlConn();
strSql= "select * from "+strTabname;
SqlCommand sqlTabname=new SqlCommand(strSql,connSql.getcon());
}
建议你看下微软的sqlhelper类 你这样写有点累赘哦!
------解决方案-------------------- sqlConn cn = new sqlConn();
SqlCommand sqlTabname=new SqlCommand(strSql,cn);
or
SqlCommand sqlTabname=new SqlCommand(strSql,new sqlConn());