国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 數(shù)據(jù)庫連接類代碼(SQL)

2024-07-10 12:42:38
字體:
供稿:網(wǎng)友
代碼如下:
public class SqlOperation
{
#region 屬性
/// <summary>
/// 保存在Web.config中的連接字符串
/// </summary>
protected static string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["hao"].ConnectionString;
/// <summary>
/// SqlConnection對象
/// </summary>
protected static SqlConnection conn = new SqlConnection();
/// <summary>
/// SqlCommand對象
/// </summary>
protected static SqlCommand comm = new SqlCommand();
#endregion

#region 內(nèi)部函數(shù)
/// <summary>
/// 打開數(shù)據(jù)庫連接
/// </summary>
private static void ConnectionOpen()
{
if (conn.State != ConnectionState.Open)
{
conn.Close();
conn.ConnectionString = connectionstring;
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}

/// <summary>
/// 關(guān)閉數(shù)據(jù)庫連接
/// </summary>
private static void ConnectionClose()
{
conn.Close();
conn.Dispose();
comm.Dispose();
}

#endregion

/// <summary>
/// 執(zhí)行SQL語句
/// </summary>
/// <param name="SqlString">要執(zhí)行的SQL語句</param>
public static void ExecuteSQL(string SqlString)
{
try
{
ConnectionOpen();
comm.CommandType = CommandType.Text;
comm.CommandText = SqlString;
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
}

/// <summary>
/// 執(zhí)行存儲過程
/// </summary>
/// <param name="ProcedureName">存儲過程名稱</param>
/// <param name="coll">存儲過程需要的參數(shù)集合</param>
public static void ExecuteProcedure(string ProcedureName, params SqlParameter[] coll)
{
try
{
ConnectionOpen();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = ProcedureName;
comm.Parameters.Clear();
for (int i = 0; i < coll.Length; i++)
{
comm.Parameters.Add(coll[i]);
}
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
try
{
ConnectionClose();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
throw new Exception(ex.Message);
}
finally
{
ConnectionClose();
}
}

/// <summary>
/// 執(zhí)行Sql查詢并返回第一行的第一條記錄,返回object,使用時需要拆箱 -> unbox
/// </summary>
/// <param name="sqlstr">傳入的Sql語句</param>
/// <returns>返回object類型的第一行第一條記錄</returns>
public static object ExecuteScalar(string SqlString)
{
object obj = new object();
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 海南省| 北票市| 巨野县| 轮台县| 武夷山市| 格尔木市| 囊谦县| 旌德县| 莱芜市| 卢氏县| 安平县| 舒兰市| 本溪| 澄迈县| 交口县| 读书| 扶风县| 泽州县| 平江县| 黔东| 会理县| 保亭| 友谊县| 吴忠市| 满城县| 宿松县| 寻乌县| 鹰潭市| 香港| 云南省| 新竹县| 汶川县| 雷波县| 宜春市| 张北县| 岳普湖县| 泰和县| 乌兰察布市| 丽江市| 正定县| 宁强县|