zoukankan      html  css  js  c++  java
  • 数据的增删改查

    namespace DAL
    {
        class SqlHelper
        {
    
            static string conString = "server=.;database=SQLSchool;uid=sa;pwd=sasa";
    
            /// <summary>
            /// 执行查询返回结果集
            /// </summary>
            /// <param name="safeSql">存储过程名(SQL语句)</param>
            /// <param name="ps">SQL参数对象集合</param>
            /// <returns>table</returns>
            public static DataTable ExecuteTable(string safeSql,  SqlParameter[] ps)
            {
                //using 块
                using (SqlConnection con = new SqlConnection(conString))
                {
                    SqlCommand cmd = new SqlCommand(safeSql, con);
                    //设置命令的类型是:存储过程。如果不设,默认是执行的文本,也就是普通的SQL语句
                    cmd.CommandType = CommandType.StoredProcedure;
                    //默认是执行普通的SQL文本语句如:select * from student
                    //cmd.CommandType = CommandType.Text;
    
                    if (ps != null && ps.Length > 0)
                    {
                        //把参数添加到命令对象的集合中
                        cmd.Parameters.AddRange(ps);
                    }
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();//查询数据:ExecuteReader
                    DataTable table = new DataTable();
                    table.Load(reader);
    
                    reader.Close();
                    con.Close();
    
                    return table;
                }
            }
    
            public static object ExecuteSingle(string safeSql, SqlParameter[] ps)//单个对象:ExecuteSingle
            {
                using (SqlConnection con = new SqlConnection(conString))
                {
                    SqlCommand cmd = new SqlCommand(safeSql, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    if (ps != null && ps.Length > 0)
                    {
                        cmd.Parameters.AddRange(ps);
                    }
                    con.Open();
                    object result = cmd.ExecuteScalar();
                    con.Close();
                    return result;
                }
            }
    
            public static int ExecuteInsertUpdateDelete(string safeSql, SqlParameter[] ps)
            {
                using (SqlConnection con = new SqlConnection(conString))
                {
                    SqlCommand cmd = new SqlCommand(safeSql, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    if (ps != null && ps.Length > 0)
                    {
                        cmd.Parameters.AddRange(ps);
                    }
                    con.Open();
                    int effectedRows = cmd.ExecuteNonQuery();//增、删、改:ExecuteNonQuery
                    con.Close();
                    return effectedRows;  //返回受影响行数(1为成功,0为失败)
                }
            }
        }
    }
  • 相关阅读:
    Allegro PCB转换成PADS方法
    Altium Designer只显示某一层,隐藏其他层
    DCDC功率电感(Inductor)选型
    DDR布线教程
    DDR地址、容量计算、Bank理解
    DDR3中的ODT(On-die termination)
    LINUX文件系统操作指令之四
    linux系统之间通过nfs网络文件系统挂载设置方法
    linux消息队列编程实例
    system()函数
  • 原文地址:https://www.cnblogs.com/Tianxf815/p/8858815.html
Copyright © 2011-2022 走看看