首页 > 开发 > 综合 > 正文

三个通用代码

2024-07-21 02:23:52
字体:
来源:转载
供稿:网友

public sqlconnection connection_database(string server,string database,string userid,string password)
{
string str_con;
str_con="server="+server+";"+"database="+database+";"+"user id="+userid+";"+"

password="+password;
try
{
sqlconnection sqlcon=new sqlconnection (str_con);
sqlcon.open();

return sqlcon;
}
catch
{
messagebox.show("数据库连接不成功!请正确填写好数据库设置","错误",messageboxbuttons.

ok,messageboxicon.stop);
application.exit();//应用程序退出
return null;
}

}
public dataview dvlist(string logstring,string tablename) //返回相应表的视图.
{
if( sqlconn.state == connectionstate.closed)
sqlconn.open();
sqldataadapter mycommand = new sqldataadapter(logstring, sqlconn);
dataset ds = new dataset();
try
{
mycommand.fill(ds, "+tablename+");
}
catch
{
messagebox.show("error");
}
dataview dv = new dataview();
dv = ds.tables["+tablename+"].defaultview;
return dv;
}

public void execute_sql(string sql,sqlconnection sqlconn)//通用执行sql函数
{
// 打开数据库连接
if( sqlconn.state == connectionstate.open)
sqlconn.close();
sqlconn.open();
sqlcommand cmd = new sqlcommand(sql, sqlconn);
try
{
cmd.executenonquery();
}
catch(sqlexception ae)
{
messagebox.show(ae.message.tostring());
}
cmd.dispose();
}
public void show_listview_data(string select_str,string tablename,listview lv)//通用的返回

树视图函数
{
//lv=new listview();
lv.clear();
this.sqldataadapter1=new sqldataadapter(select_str,this.sqlconn);
sqldataadapter1.fill(dataset21,"+tablename+");
//添加列名
for(int i=0;i<dataset21.tables["+tablename+"].columns.count;i++)
{
lv.columns.add(dataset21.tables["+tablename+"].columns[i].columnname.

tostring(),90,horizontalalignment.left);

}
//添加行(数据库数据)
for(int j=0;j<dataset21.tables["+tablename+"].rows.count;j++)
{
listviewitem item=lv.items.add(dataset21.tables["+tablename+"].rows[j][0].

tostring());
item.text=dataset21.tables["+tablename+"].rows[j][0].tostring();
for(int i=1;i<dataset21.tables["+tablename+"].columns.count;i++)
item.subitems.add(dataset21.tables["+tablename+"].rows[j][i].

tostring());
}
lv.view=view.details;


}

}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表