SQLHelper.cs
/// <summary> ///連接數據庫字符串 ///塗聚文 2011-09-25 ///締友計算機信息技術有限公司 /// </summary> private static string connectionString; /// <summary> /// /// </summary> static DBHelper() { DataSetingConfig dbconnection = new DataSetingConfig(); dbconnection.GetConString();//讀文件內空中的連按字符串 connectionString = DataSetingConfig.conStr;//@"Data Source=.;Initial Catalog=GeovinDuHr;User ID=sa;Password=8888888"; } /// <summary> /// /// </summary> /// <param name="hasOpen"></param> /// <returns></returns> public static SqlConnection GetConnection(bool hasOpen) { SqlConnection conn = new SqlConnection(connectionString); if (hasOpen) conn.Open(); return conn; } /// <summary> /// ///塗聚文 2011-09-25 ///締友計算機信息技術有限公司 /// </summary> /// <param name="sql"></param> /// <param name="cmdType"></param> /// <param name="sqlParams"></param> /// <returns></returns> public static SqlDataReader GetReader(string sql, CommandType cmdType, params SqlParameter[] sqlParams) { SqlCommand cmd = new SqlCommand(); cmd.CommandType = cmdType; cmd.CommandText = sql; if (sqlParams != null) cmd.Parameters.AddRange(sqlParams); SqlConnection conn = GetConnection(true); cmd.Connection = conn; return cmd.ExecuteReader(CommandBehavior.CloseConnection); } /// <summary> /// ///塗聚文 2011-09-25 ///締友計算機信息技術有限公司 /// </summary> /// <param name="sql"></param> /// <param name="cmdType"></param> /// <param name="sqlParams"></param> /// <returns></returns> public static Object GetObject(string sql, CommandType cmdType, params SqlParameter[] sqlParams) { object ret = null; SqlCommand cmd = new SqlCommand(); cmd.CommandType = cmdType; cmd.CommandText = sql; if (sqlParams != null) cmd.Parameters.AddRange(sqlParams); SqlConnection conn = GetConnection(true); cmd.Connection = conn; try { ret = cmd.ExecuteScalar(); } finally { conn.Close(); } return ret; } /// <summary> /// ///塗聚文 2011-09-25 ///締友計算機信息技術有限公司 /// </summary> /// <param name="sql"></param> /// <param name="cmdType"></param> /// <param name="sqlParams"></param> /// <returns></returns> public static int ExecuteSql(string sql, CommandType cmdType, params SqlParameter[] sqlParams) { int ret = 0; SqlCommand cmd = new SqlCommand(); cmd.CommandType = cmdType; cmd.CommandText = sql; if (sqlParams != null) cmd.Parameters.AddRange(sqlParams); SqlConnection conn = GetConnection(true); cmd.Connection = conn; try { ret = cmd.ExecuteNonQuery(); } finally { conn.Close(); } return ret; } /// <summary> /// ///塗聚文 2011-09-25 ///締友計算機信息技術有限公司 /// </summary> /// <param name="sql"></param> /// <param name="cmdType"></param> /// <param name="sqlParams"></param> /// <returns></returns> public static DataTable GetTable(string sql, CommandType cmdType, params SqlParameter[] sqlParams) { DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = cmdType; cmd.CommandText = sql; if (sqlParams != null) cmd.Parameters.AddRange(sqlParams); SqlConnection conn = GetConnection(true); cmd.Connection = conn; SqlDataAdapter da = new SqlDataAdapter(cmd); try { da.Fill(dt); } finally { conn.Close(); } return dt; } /// <summary> /// ///塗聚文 2011-09-25 ///締友計算機信息技術有限公司 /// </summary> /// <param name="sql"></param> /// <param name="cmdType"></param> /// <param name="sqlParams"></param> /// <returns></returns> public static DataSet GetDataSet(string sql, CommandType cmdType, params SqlParameter[] sqlParams) { DataSet dt = new DataSet(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = cmdType; cmd.CommandText = sql; if (sqlParams != null) cmd.Parameters.AddRange(sqlParams); SqlConnection conn = GetConnection(true); cmd.Connection = conn; SqlDataAdapter da = new SqlDataAdapter(cmd); try { da.Fill(dt); } finally { conn.Close(); } return dt; }
參考: http://www.koders.com/csharp/fidD4121D6E4BCA2DAB656D770903FECBFF7427D242.aspx
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)