自己写的SqlHelper

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Web;



namespace resume.Helper

{

    public static class SqlHelper

    {

        public static readonly string connstr =

           ConfigurationManager.ConnectionStrings["liuyan"].ConnectionString;



        public static SqlConnection OpenConnection()

        {

            SqlConnection conn = new SqlConnection(connstr);

            conn.Open();

            return conn;

        }

        /// <summary>

        /// 执行非查询语句,并返回受影响的行数

        /// </summary>

        /// <param name="cmdText"></param>

        /// <param name="parameters"></param>

        /// <returns></returns>

        public static int ExecuteNonQuery(string cmdText,

            params SqlParameter[] parameters)

        {

            using (SqlConnection conn = new SqlConnection(connstr))

            {

                conn.Open();

                return ExecuteNonQuery(conn, cmdText, parameters);

            }

        }



        public static object ExecuteScalar(string cmdText,

            params SqlParameter[] parameters)

        {

            using (SqlConnection conn = new SqlConnection(connstr))

            {

                conn.Open();

                return ExecuteScalar(conn, cmdText, parameters);

            }

        }



        public static DataTable ExecuteDataTable(string cmdText,

            params SqlParameter[] parameters)

        {

            using (SqlConnection conn = new SqlConnection(connstr))

            {

                conn.Open();

                return ExecuteDataTable(conn, cmdText, parameters);

            }

        }



        /// <summary>

        /// 执行非查询语句,并返回受影响的行数

        /// </summary>

        /// <param name="conn"></param>

        /// <param name="cmdText"></param>

        /// <param name="parameters"></param>

        /// <returns></returns>

        public static int ExecuteNonQuery(SqlConnection conn, string cmdText,

           params SqlParameter[] parameters)

        {

            using (SqlCommand cmd = conn.CreateCommand())

            {

                cmd.CommandText = cmdText;

                cmd.Parameters.AddRange(parameters);

                return cmd.ExecuteNonQuery();

            }

        }



        public static object ExecuteScalar(SqlConnection conn, string cmdText,

            params SqlParameter[] parameters)

        {

            using (SqlCommand cmd = conn.CreateCommand())

            {

                cmd.CommandText = cmdText;

                cmd.Parameters.AddRange(parameters);

                return cmd.ExecuteScalar();

            }

        }



        public static DataTable ExecuteDataTable(SqlConnection conn, string cmdText,

            params SqlParameter[] parameters)

        {

            using (SqlCommand cmd = conn.CreateCommand())

            {

                cmd.CommandText = cmdText;

                cmd.Parameters.AddRange(parameters);

                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))

                {

                    DataTable dt = new DataTable();

                    adapter.Fill(dt);

                    return dt;

                }

            }

        }



        public static object ToDBValue(this object value)

        {

            return value == null ? DBNull.Value : value;

        }



        public static object FromDBValue(this object dbValue)

        {

            return dbValue == DBNull.Value ? null : dbValue;

        }

    }

}
View Code

 

下面是数据库连接字符串,写程序的时候经常要用不可能我们自己总是写,这样的习惯不是很好的。

<connectionStrings>

    <add name="连接字符串" connectionString="server =数据库地址; initial catalog =数控库名; uid = sa; pwd =123456789"

      providerName="System.Data.SqlClient" />

  </connectionStrings>

 

你可能感兴趣的:(help)