程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net 數據庫的連接和datatable類

asp.net 數據庫的連接和datatable類

編輯:ASP.NET基礎
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;
}
}
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved