zoukankan      html  css  js  c++  java
  • ADO.NET通用数据库访问类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using System.Data;
    using System.Data.SqlClient;

    namespace Test
    {
        public class DBHelper
        {
            public static string ConString = "Data Source=.;Initial Catalog=bankdb;User id=sa;Password=123;";

            //执行增删改的方法
            public static int RunNoQuery(string cmdText, CommandType cmdType, params SqlParameter[] pars)
            {
                SqlConnection con = new SqlConnection(ConString);
                con.Open();
                SqlCommand cmd = new SqlCommand(cmdText, con);
                cmd.CommandType = cmdType;
                if (pars != null && pars.Length > 0)
                {
                    foreach (SqlParameter p in pars)
                    {
                        cmd.Parameters.Add(p);
                    }
                }
                int rows = cmd.ExecuteNonQuery();
                con.Close();
                return rows;
            }

            //执行查询(DataSet)的方法
            public static DataSet RunSelect(string cmdText, CommandType cmdType, params SqlParameter[] pars)
            {
                SqlConnection con = new SqlConnection(ConString);
              
                SqlDataAdapter da = new SqlDataAdapter(cmdText, con);
                da.SelectCommand.CommandType = cmdType;
                if (pars != null && pars.Length > 0)
                {
                    foreach (SqlParameter p in pars)
                    {
                        da.SelectCommand.Parameters.Add(p);
                    }
                }
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }

            //执行查询得到一个值
            public static object RunOneValue(string cmdText, CommandType cmdType, params SqlParameter[] pars)
            {
                SqlConnection con = new SqlConnection(ConString);
                con.Open();
                SqlCommand cmd = new SqlCommand(cmdText, con);
                cmd.CommandType = cmdType;
                if (pars != null && pars.Length > 0)
                {
                    foreach (SqlParameter p in pars)
                    {
                        cmd.Parameters.Add(p);
                    }
                }
                object obj = cmd.ExecuteScalar();
                con.Close();
                return obj;
            }
        }
    }
  • 相关阅读:
    Kubernetes stateful set讲解以及一个基于postgreSQL的具体例子
    如何在Kubernetes里给PostgreSQL创建secret
    如何使用Kubernetes的configmap通过环境变量注入到pod里
    使用Gardener在Google Cloud Platform上创建Kubernetes集群
    通过describe命令学习Kubernetes的pod属性详解
    使用describe命令进行Kubernetes pod错误排查
    一个简单的例子理解Kubernetes的三种IP地址类型
    不同编程语言在发生stackoverflow之前支持的调用栈最大嵌套层数
    (十)golang--运算符
    (九)golang--标识符的命名规则
  • 原文地址:https://www.cnblogs.com/turingchang/p/5287135.html
Copyright © 2011-2022 走看看