|
本帖最后由 xiaomu 于 2013-9-29 13:43 编辑
看大家都在发,我也发出我写的,小学毕业,自学成渣,欢迎指教。- class SqlHelper//by.穆建情 2013-09-28
- {
- #region //SqlConnectionStr
- /// <summary>
- /// 数据库连接字符串
- /// </summary>
- private static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
- #endregion
- #region //ExecteQuery
- /// <summary>
- /// 执行 Transact-SQL 查询语句并返回DataTable.
- /// </summary>
- /// <param name="sqlStr">存储过程或者T-SQL语句</param>
- /// <param name="Parameters">以数组形式提供SqlCommand命令中的参数</param>
- /// <returns>返回一个查询结果集DataTable</returns>
- public static DataTable ExecteTable(string sqlStr, params SqlParameter[] Parameters)
- {
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = sqlStr.Trim();
- cmd.Parameters.AddRange(Parameters);
- using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
- {
- DataTable dt = new DataTable();
- adapter.Fill(dt);
- return dt;
- }
- }
- }
- }
- /// <summary>
- /// 执行 Transact-SQL 查询语句并返回DataTable.
- /// </summary>
- /// <param name="sqlStr">存储过程或者T-SQL语句</param>
- ///<param name="cmdType">SqlCommand命令类型 (存储过程,T-SQL语句,等等.)</param>
- /// <param name="Parameters">以数组形式提供SqlCommand命令中的参数</param>
- /// <returns>返回一个查询结果集DataTable</returns>
- public static DataTable ExecteTable(string sqlStr, CommandType cmdType, params SqlParameter[] Parameters)
- {
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = sqlStr.Trim();
- cmd.CommandType = cmdType;
- cmd.Parameters.AddRange(Parameters);
- using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
- {
- DataTable dt = new DataTable();
- adapter.Fill(dt);
- return dt;
- }
- }
- }
- }
- /// <summary>
- /// 执行 Transact-SQL 查询语句并返回DataSet.
- /// </summary>
- /// <param name="cmdText">存储过程或者T-SQL语句</param>
- /// <param name="Parameters">以数组形式提供SqlCommand命令中的参数</param>
- /// <returns>返回一个查询结果集DataSet</returns>
- public static DataSet ExecteDataSet(string sqlStr, params SqlParameter[] Parameters)
- {
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = sqlStr.Trim();
- cmd.Parameters.AddRange(Parameters);
- using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- adapter.Fill(ds);
- return ds;
- }
- }
- }
- }
- /// <summary>
- /// 执行 Transact-SQL 查询语句并返回DataSet.
- /// </summary>
- /// <param name="cmdText">存储过程或者T-SQL语句</param>
- /// <param name="cmdType">SqlCommand命令类型 (存储过程,T-SQL语句,等等.)</param>
- /// <param name="Parameters">以数组形式提供SqlCommand命令中的参数</param>
- /// <returns>返回一个查询结果集DataSet</returns>
- public static DataSet ExecteDataSet(string sqlStr, CommandType cmdType, params SqlParameter[] Parameters)
- {
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- conn.Open();
- using (SqlCommand cmd = conn.CreateCommand())
- {
- cmd.CommandText = sqlStr.Trim();
- cmd.CommandType = cmdType;
- cmd.Parameters.AddRange(Parameters);
- using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- adapter.Fill(ds);
- return ds;
- }
- }
- }
- }
- /// <summary>
- /// 执行 Transact-SQL 查询语句并返回SqlDataReader.
- /// </summary>
- /// <param name="cmdText">存储过程或者T-SQL语句</param>
- /// <param name="Parameters">以数组形式提供SqlCommand命令中的参数</param>
- /// <returns>返回一个SqlDataReader</returns>
- public static SqlDataReader ExecteReader(string sqlStr, params SqlParameter[] Parameters)
- {
- SqlConnection conn = new SqlConnection(connStr);
复制代码 |
|