zoukankan      html  css  js  c++  java
  • SqlHelper

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    namespace Demo2017
    {
        public class SqlHelper
        {
            public static SqlDataReader ExecuteReader(string constr, string cmdstr, CommandType type, params SqlParameter[] ps)
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.CommandType = type;
                if (ps.Length > 0)
                {
                    cmd.Parameters.AddRange(ps);
                }
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;
            }

            public static int ExecuteNonQuery(string constr, string cmdstr,CommandType type, params SqlParameter[] ps)
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.CommandType = type;
                if (ps.Length > 0)
                {
                    cmd.Parameters.AddRange(ps);
                }
               int i= cmd.ExecuteNonQuery();
               return i;
            }

            public static DataSet ExecuteDataSet(string constr, string cmdstr,CommandType type, params SqlParameter[] ps)
            {
                SqlConnection conn = new SqlConnection(constr);
          
                SqlDataAdapter sda = new SqlDataAdapter(cmdstr, conn);      
                sda.SelectCommand.CommandType=type;
                if (ps.Length > 0)
                {
                    sda.SelectCommand.Parameters.AddRange(ps);
                }
                DataSet ds = new DataSet();
                sda.Fill(ds);              
                return ds;
            }

            public static object ExecuteScalar(string constr, string cmdstr,CommandType type, params SqlParameter[] ps)
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.CommandType = type;
                if (ps.Length > 0)
                {
                    cmd.Parameters.AddRange(ps);
                }
                object o = cmd.ExecuteScalar();
                return o;
            }
        }
    }
  • 相关阅读:
    Fragment生命周期详解
    VS常用快捷键
    转载:C#中的泛型
    Vue 引入代码代码编辑器monacoeditor并自定义语法提示
    Monaco Editor 参数配置详解
    关于uniapp生成证书数字签名(.keystore)文件
    C#上传到FTP Server
    Linux下静态编译glib
    简单谈谈haskell
    hi,大家
  • 原文地址:https://www.cnblogs.com/wantnewlive/p/9868996.html
Copyright © 2011-2022 走看看