码迷,mamicode.com
首页 > 移动开发 > 详细

封装数据库配置文件App配置文件

时间:2015-06-24 22:35:10      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

 <connectionStrings>
   <add name="strCon" connectionString="Data Source=.;Initial Catalog=DataManager;User ID=sa;Passwoed=lifatshit"/>
</connectionStrings>

封装表格

 public static DataTable ExecuteDateTable(string sql, params SqlParameter[] param)
        {
            DataTable dt = null;
            using(SqlConnection con=new SqlConnection(strCon))
            {
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddRange(param);
                using(SqlDataAdapter adapter=new SqlDataAdapter(cmd))
                {
                    dt = new DataTable();
                    adapter.Fill(dt);
                }
            }
            return dt;
        }

//执行增删改 返回受影响的行数

public static int ExecuteNonquery(string sql, params SqlParameter[] param)
        {
            int n = -1;
            using (SqlConnection con = new SqlConnection(strCon))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    con.Open();
                    cmd.Parameters.AddRange(param);
                    n = cmd.ExecuteNonQuery();
                }
            }
            return n;
 
        }

 //执行查询 负责返回首行首列

public static object ExecuteDcalar(string sql, params SqlParameter[] param)
        {
            object o = null;
            using(SqlConnection con=new SqlConnection (strCon))
            {
                using(SqlCommand cmd=new SqlCommand (sql,con))
                {
                    con.Open();
                    cmd.Parameters.AddRange(param);
                    o = cmd.ExecuteScalar();
                }
            }
            return o;

}

 //读取数据库

 public  static SqlDataReader ExecuteReader(string sql,params SqlParameter[] param)
        {
            SqlDataReader reader = null;
            using (SqlConnection con = new SqlConnection(strCon))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    con.Open();
                    cmd.Parameters.AddRange(param);
                    reader = cmd.ExecuteReader();
                }
            }
            return reader;
        }

 

封装数据库配置文件App配置文件

标签:

原文地址:http://www.cnblogs.com/hsha/p/4598749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!