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


     <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;
            }

    重来没有接触过,零基础学习软件编程,一个字累
  • 相关阅读:
    Java学习:Scanner类
    Java学习:Object类
    js 方法
    ToDictionary
    goto
    删除多列同时重复数据
    linq中使用case when
    sql 学习笔记
    VS2012发布网站详细步骤
    由于权限不足而无法读取配置文件出现的HTTP 500.19解决办法
  • 原文地址:https://www.cnblogs.com/hsha/p/4598749.html
Copyright © 2020-2023  润新知