zoukankan      html  css  js  c++  java
  • SQLHelper 工具类的使用

    新建一个winfrom窗体应用程序,为程序添加配置文件App.config,然后在配置文件中添加数据库连接字符串
    
    <connectionStrings>
    <add name ="constr" connectionString="server=PC-20171113RBMO;database=StudentDB;Trusted_Connection = True"/>
    </connectionStrings>
    
    
    
    
     
    
    

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    
    /// <summary>
    /// SQLHelper 的摘要说明
    /// </summary>
    public class SQLHelper
    {
        public SQLHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        //在程序中引用连接字符串
        static string str = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        public static object ExecuteScalar(string sql, params SqlParameter[] paras)
        {
    
            //返回第一行第一列
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            if (paras != null)
            {
                cmd.Parameters.AddRange(paras);
            }
            object o = cmd.ExecuteScalar();
            con.Close();
            return o;
        }
    
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] paras)
        {
    
            //查询
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            if (paras != null)
            {
                cmd.Parameters.AddRange(paras);
            }
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
            return dt;
        }
    
        public static int NonExQuery(string sql, params SqlParameter[] paras)
        {
    
            //增删改
    
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
    
            if (paras != null)
            {
                cmd.Parameters.AddRange(paras);
            }
            int i = cmd.ExecuteNonQuery();
            con.Close();
            return i;
    
        }
    
        public static SqlDataReader ExecReader(string sql, params SqlParameter[] paras)
        {
            
            SqlConnection con = new SqlConnection(str);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
    
            if (paras != null)
            {
                cmd.Parameters.AddRange(paras);
            }
            SqlDataReader dr = cmd.ExecuteReader();
            con.Close();
            return dr;
    
        }
    }


    
    
    
    
    
     
    
    
    
     
  • 相关阅读:
    用make编译openCV报错:ts_gtest.cpp:(.text._ZN7testing8internal2RED2Ev+0xf): undefined reference to 'regfreeA'
    Makefile:160: recipe for target 'all' failed (Ubuntu 16.06 + Opencv3.2)解决办法
    Linux常用命令汇总
    深度图像检测算法总结与对比(4)
    深度图像检测算法总结与对比(3)
    深度图像检测算法总结与对比(2)
    深度图像检测算法总结与对比(1)
    Caffe SSD的resize过程解析
    caffe 生成检测框并绘图
    Caffe中deploy.prototxt 和 train_val.prototxt 区别
  • 原文地址:https://www.cnblogs.com/zhang1997/p/8026534.html
Copyright © 2011-2022 走看看