SqlHelp

 
   

using System.Configuration;
using System.Data;

public class SqlHelp

    {

        private static string connection = ConfigurationManager.ConnectionStrings["db"].ConnectionString;



        // 执行insert、delete、update的方法

        public static int ExcuteNonQuery(string sql, params SqlParameter[] parameters)

        {

            using (SqlConnection con = new SqlConnection(connection))

            {

                if (con.State ==ConnectionState.Closed)

                {

                    con.Open();

                }

                using (SqlCommand cmd = new SqlCommand(sql,con))

                {

                    if (parameters != null)

                    {

                        cmd.Parameters.AddRange(parameters);

                    }



                    return cmd.ExecuteNonQuery();

                }

            }

        }



        // 执行sql语句,返回单个值。

        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)

        {

            using (SqlConnection con = new SqlConnection(connection))

            {

                con.Open();

                using (var cmd = new SqlCommand(sql,con))

                {

                    if (parameters != null)

                    {

                        cmd.Parameters.AddRange(parameters);

                    }

                    return cmd.ExecuteScalar();

                }

            }

        }



        //查询集合或实体

        public static SqlDataReader ExDataReader(string sql, params SqlParameter[] parameters)

        {

            SqlConnection con = new SqlConnection(connection);

            using (SqlCommand cmd = new SqlCommand(sql, con))

            {

                if (parameters != null)

                {

                    cmd.Parameters.AddRange(parameters);

                }

                con.Open();              

                return cmd.ExecuteReader(CommandBehavior.CloseConnection);

            }

        }





        //查询集合

        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)

        {

            SqlDataAdapter sqlAdapter = new SqlDataAdapter(sql, connection);

            if (parameters!=null)

            {

                sqlAdapter.SelectCommand.Parameters.AddRange(parameters);

            }

            DataTable dt = new DataTable();

            sqlAdapter.Fill(dt);

            return dt;

        }

    }

//它的辅助查询和Mysql和差不多,但是要把参数换一下
DbParameter换成
SqlParameter
把这些帮助类文件放在先建的“类库”中然后生成,就可以得到.Dll文件 



 

你可能感兴趣的:(help)