asp.net 資料庫的連線和datatable類

NO IMAGE

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;
}
}
}
}

您可能感興趣的文章:

一個ASP.NET的MYSQL的資料庫操作類自己封裝的asp.net Oracle資料庫訪問操作類asp.net下使用DbProviderFactories的資料庫操作類ASP.NET對SQLServer的通用資料庫訪問類asp.net下Oracle,SQL Server,Access萬能資料庫通用類asp.net 資料庫連線類程式碼(SQL)ASP.NET封裝的SQL資料庫訪問類ASP.NET web.config中資料庫連線字串connectionStrings節的配置方法asp.net連線查詢SQL資料庫並把結果顯示在網頁上(2種方法)ASP.NET 6種常用資料庫的連線方法ASP.NET2.0 SQL Server資料庫連線詳解Asp.net把圖片存入資料庫和讀取圖片的方法ASP.NET資料庫操作類例項