欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 数据库的连接和datatable类

程序员文章站 2024-03-09 14:14:05
using system; using system.data; using system.configuration; using system.web; using s...
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
/// <summary>
/// sqlconnections 的摘要说明
/// </summary>
namespace system.mysqlconnection
{
public class sqlconnections
{
//当前连接的是第几个。
static int count = 0;
//最大连接量
static int maxcount = 30;
static sqlconnection[] sqlconns = new sqlconnection[maxcount];
//自己创建自己
static sqlconnections myconn = new sqlconnections();
//初始化
private sqlconnections()
{
for (int i = 0; i < maxcount; i++)
{
sqlconns[i] = new sqlconnection(system.configuration.configurationsettings.appsettings["adostr"].tostring());
}
}
//根据条件来获取不同的表,与字段。
public static datatable getdatatable(string sqltest, string tablename)
{
try
{
dataset myds = new dataset();
sqldataadapter myadapater = new sqldataadapter(sqltest, sqlconnections.getsqlconnection());
myadapater.fill(myds, tablename);
return myds.tables[tablename];
}
catch (sqlexception sqle)
{
return null;
}
}
// 获取数据库连接
public static sqlconnection getsqlconnection()
{
try
{
try
{
sqlconns[count].close();
}
catch (exception eex)
{
}
return sqlconns[count];
}
finally
{
//当前个数自加。。
if (count == (maxcount - 1))
{
count = 0;
}
else
{
count++;
}
}
}
//返回一个参数
public static string getnumonefield(string sqltxt)
{
sqlconnection sqlconntemp = sqlconnections.getsqlconnection();
try
{
sqlcommand sqlcommt = new sqlcommand(sqltxt, sqlconntemp);
sqlconntemp.open();
return sqlcommt.executescalar().tostring();
}
catch (exception sqle)
{
return "没有";
}
finally
{
sqlconntemp.close();
}
}
//返回一个参数
public static string getnumonefield(sqlconnection sqlconntemp,sqltransaction tempsqltran ,string sqltxt)
{
try
{
sqlcommand sqlcommt = new sqlcommand(sqltxt, sqlconntemp);
sqlcommt.transaction = tempsqltran;
return sqlcommt.executescalar().tostring();
}
catch (exception sqle)
{
return "没有";
}
finally
{
//sqlconntemp.close();
}
}
/// <summary>
/// 做 ,修改,删除,
/// </summary>
/// <param name="sqltxt">执行的sql语句</param>
/// <returns>返回的结果影响多少行</returns>
public static int updateordelete(string sqltxt)
{
sqlconnection sqlconntemp = sqlconnections.getsqlconnection();
try
{
sqlcommand sqlcommt = new sqlcommand(sqltxt, sqlconntemp);
sqlconntemp.open();
return sqlcommt.executenonquery();
}
catch (sqlexception sqle)
{
return 0;
}
finally
{
//sqlconntemp.close();
}
}
public static int updateordelete(commandtype _commandtype, sqlparameter[] _sqlparas, string sqltxt)
{
sqlconnection sqlconntemp = sqlconnections.getsqlconnection();
try
{
sqlcommand sqlcommt = new sqlcommand(sqltxt, sqlconntemp);
sqlcommt.commandtype = _commandtype;
sqlcommt.parameters.clear();
for (int a = 0; a < _sqlparas.length; a++)
{
sqlcommt.parameters.add(_sqlparas[a]);
}
sqlconntemp.open();
return sqlcommt.executenonquery();
}
catch (sqlexception sqle)
{
return 0;
}
finally
{
//sqlconntemp.close();
}
}
/// <summary>
/// 带事务的增,删,改数据执行方法(警告:该方法必须 手动开关数据,而且还有手动的提交事务。否则不能更新数据)
/// </summary>
/// <param name="_sqlconn">数据库连接对象</param>
/// <param name="_sqltran">事务对象</param>
/// <param name="sqltxt">执行的sql语句</param>
/// <returns></returns>
public static int updateordelete(sqlconnection _sqlconn,sqltransaction _sqltran, string sqltxt)
{
sqlconnection sqlconntemp = _sqlconn;
try
{
sqlcommand sqlcommt = new sqlcommand(sqltxt, sqlconntemp);
sqlcommt.transaction = _sqltran;
sqlconntemp.open();
return sqlcommt.executenonquery();
}
catch (sqlexception sqle)
{
return 0;
}
finally
{
//sqlconntemp.close();
}
}
public static int updateordelete(sqlconnection _sqlconn, sqltransaction _sqltran, commandtype _commandtype, sqlparameter [] _sqlparas, string sqltxt)
{
sqlconnection sqlconntemp = _sqlconn;
try
{
sqlcommand sqlcommt = new sqlcommand(sqltxt, sqlconntemp);
sqlcommt.transaction = _sqltran;
sqlcommt.commandtype = _commandtype;
sqlcommt.parameters.clear();
for (int a = 0; a < _sqlparas.length; a++)
{
sqlcommt.parameters.add(_sqlparas[a]);
}
return sqlcommt.executenonquery();
}
catch (sqlexception sqle)
{
return 0;
}
finally
{
// sqlconntemp.close();
}
}
/// <summary>
/// 有存储过的查询
/// </summary>
/// <param name="storedprocedurename">存储过程的名字</param>
/// <param name="sqlparas">所有参数</param>
/// <param name="tablename">表名</param>
/// <returns>datatable的结果集</returns>
public static datatable getdatatable(string storedprocedurename, sqlparameter [] sqlparas,string tablename)
{
try
{
dataset myds = new dataset();
sqldataadapter myadapater = new sqldataadapter(storedprocedurename, sqlconnections.getsqlconnection());
myadapater.selectcommand.commandtype = commandtype.storedprocedure;
for (int i = 0; i < sqlparas.length; i++)
{
//if (myadapater.selectcommand.parameters.contains(sqlparas[i]))
//myadapater.selectcommand.parameters.removeat(sqlparas[i].parametername);
myadapater.selectcommand.parameters.add(sqlparas[i]);
}
myadapater.fill(myds, tablename);
myadapater.selectcommand.parameters.clear();
myadapater.dispose();
return myds.tables[tablename];
}
catch (sqlexception sqle)
{
return null;
}
}
}
}

上一篇: Java实现简单邮件发送

下一篇: