用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

连接数据库、对数据库的增、删、改、查

2013-01-30 作者: Mr宇举报

[c#]代码库

 public class SqlHelper
    {
        //数据库连接词
        private static string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ToString();

        /// <summary>
        /// SQL语句 无参查询
        /// </summary>
        /// <param name="sql">select 语句</param>
        /// <returns>返回SqlDataReader</returns>
        public static SqlDataReader Query(string sql)
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                if(conn.State==ConnectionState.Open)
                {
                    conn.Close();
                }
                throw;
            }
        }

        /// <summary>
        /// SQL语句 有参查询
        /// </summary>
        /// <param name="sql">select 语句</param>
        /// <param name="para">SQL参数</param>
        /// <returns>返回SqlDataReader</returns>
        public static SqlDataReader Query(string sql, SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(constr);
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddRange(para);
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                if(conn.State==ConnectionState.Open)
                {
                    conn.Close();
                }
                throw;
            }
        }

        /// <summary>
        /// SQL语句 单一结果的无参查询
        /// </summary>
        /// <param name="sql">select 语句</param>
        /// <returns>返回 object</returns>
        public static object QueryScalar(string sql)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                return cmd.ExecuteScalar();
            }
        }

        /// <summary>
        /// SQL语句 单一结果的有参查询
        /// </summary>
        /// <param name="sql">select 语句</param>
        /// <param name="para">SQL参数</param>
        /// <returns>返回 object</returns>
        public static object QueryScalar(string sql,SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddRange(para);
                return cmd.ExecuteScalar();
            }
        }

        /// <summary>
        /// SQL语句 无参增,删,改
        /// </summary>
        /// <param name="sql">insert,update,delete语句</param>
        /// <returns>返回影响的行数</returns>
        public static int NonQuery(string sql)
        {
            SqlConnection conn = new SqlConnection(constr);
            SqlTransaction tran = null;
            try
            {
                conn.Open();
                tran = conn.BeginTransaction();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Transaction = tran;
                int i = cmd.ExecuteNonQuery();
                tran.Commit();
                return i;
            }
            catch
            {
                if (tran != null)
                    tran.Rollback();
                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
        }

        /// <summary>
        /// SQL语句 有参增,删,改
        /// </summary>
        /// <param name="sql">insert,update,delete语句</param>
        /// <param name="para">SQL参数</param>
        /// <returns>返回影响的行数</returns>
        public static int NonQuery(string sql,SqlParameter[] para)
        {
            SqlConnection conn = new SqlConnection(constr);
            SqlTransaction tran = null;
            try
            {
                conn.Open();
                tran = conn.BeginTransaction();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddRange(para);
                cmd.Transaction = tran;
                int i = cmd.ExecuteNonQuery();
                tran.Commit();
                return i;
            }
            catch (Exception e)
            {
                if (tran != null)
                    tran.Rollback();
                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
        }
    }


网友评论    (发表评论)

共5 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...