using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data; namespace Students.DAL
{
public class DBHelper
{
public static readonly string conn = ConfigurationManager.ConnectionStrings["ClassRoomConnectionString"].ToString();
public static SqlConnection connection = new SqlConnection(DBHelper.conn); /// <summary>
/// 增删改数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql, SqlParameter[] par)
{
try
{
connection.Open(); //打开数据库连接
SqlCommand comm = new SqlCommand(sql, connection);
//comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.Parameters.AddRange(par);
return comm.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
connection.Close();
}
}
/// <summary>
/// 增删改数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql, SqlParameter[] par, string sql2, SqlParameter[] par2)
{
connection.Open(); //打开数据库连接
SqlTransaction tra = connection.BeginTransaction();
try
{
SqlCommand comm = new SqlCommand(sql, connection);
SqlCommand comm2 = new SqlCommand(sql2, connection);
comm.Parameters.AddRange(par);
comm2.Parameters.AddRange(par2);
comm.Transaction = tra;
comm2.Transaction = tra;
int num1 = comm.ExecuteNonQuery();
int num2 = comm2.ExecuteNonQuery();
int num = comm.ExecuteNonQuery() + comm2.ExecuteNonQuery();
tra.Commit();
return num;
}
catch
{
tra.Rollback();
throw;
}
finally
{
connection.Close();
}
}
/// <summary>
/// 查询数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string sql, SqlParameter[] par)
{
try
{
connection.Open(); //打开数据库连接
SqlCommand comm = new SqlCommand(sql, connection);
//comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.Parameters.AddRange(par);
return comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
throw;
}
}
/// <summary>
/// 查询数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string sql)
{
try
{
connection.Open(); //打开数据库连接
SqlCommand comm = new SqlCommand(sql, connection);
return comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
throw;
}
}
/// <summary>
/// 返回单个值
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static object ExecuteScalar(string sql, SqlParameter[] par)
{
try
{
connection.Open(); //打开数据库连接
SqlCommand comm = new SqlCommand(sql, connection);
//comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.Parameters.AddRange(par);
return comm.ExecuteScalar();
}
catch
{
throw;
}
finally
{
connection.Close();
}
}
}
}

然后再App.config文件中写连接语句

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name ="ClassRoomConnectionString"
connectionString="Data Source=.;Initial Catalog=StudentDB;User ID=sa;Password=sa"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

C#连接数据库_使用读取配置文件的方式的更多相关文章

  1. Java_JDBC连接数据库_使用读取配置文件的方式

    package com.homewoek3_4.dao; import java.io.IOException; import java.io.InputStream; import java.sql ...

  2. python读取配置文件的方式

    python读取配置文件的方式 1.从config.ini中读取,后缀无所谓,文件名字也无所谓,不过config.ini是常用写法,所谓见名知意 config.ini内容: [global] ip = ...

  3. Java读取配置文件的方式

    Java读取配置文件的方式-笔记 1       取当前启动文件夹下的配置文件   一般来讲启动java程序的时候.在启动的文件夹下会有配置文件 classLoader.getResource(&qu ...

  4. python中读取配置文件的方式

    方式1:argparse argparse,是Python标准库中推荐使用的编写命令行程序的工具.也可以用于读取配置文件. 字典样式的配置文件*.conf 配置文件test1.conf { " ...

  5. JavaWeb中servlet读取配置文件的方式

    我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...

  6. Spring中@Value注解使用——一种新的直接读取配置文件的方式

    1.@Value注解作用 该注解的作用是将我们配置文件的属性读出来,有@Value(“${}”)和@Value(“#{}”)两种方式. 2.@Value注解作用的两种方式 场景 假如有以下属性文件de ...

  7. SpringBoot两种读取配置文件的方式

    方式一 @Value("${custom.group}") private String customGroup; 方式二 @Autowired private Environme ...

  8. Spring读取配置文件的方式总结

    一.基于XML配置的方式 1.使用 PropertyPlaceholderConfigurer - 在 applicationContext.xml 中配置: <context:property ...

  9. scala读取jar包外配置文件的方式

    在scala的开发过程中,经常会修改程序的参数,将这些参数放到配置文件中避免了重复编译,打包的过程 这里给出读取配置文件的三种方式 方式一: 这是最常见的读取配置文件方式 val postgprop ...

随机推荐

  1. 嵌入式驱动开发之--- 虚拟磁盘SBULL块设备驱动程序分析

     #define SBULL_MINORS  16         /* 每个sbull设备所支持的次设备号的数量 */  #define KERNEL_SECTOR_SIZE 512  // 本地定 ...

  2. grails Domian对象转JSON去class以及自己定义字段的最佳方式

    grails:2.4.x IDE:Intellij IDEA 13.x grails的Domain对象之间假设存在环形引用.直接使用as JSON仅仅会输出关联对象的id.而且假设使用deep也会报错 ...

  3. ZOJ3261 Connections in Galaxy War —— 反向并查集

    题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...

  4. YTU 1007: Redraiment猜想

    1007: Redraiment猜想 时间限制: 1000 Sec  内存限制: 10 MB 提交: 83  解决: 23 题目描述 redraiment在家极度无聊,于是找了张纸开始统计素数的个数. ...

  5. 比特币钱包的bitcoin-cli 命令全集

    A.一般性的命令 help ( "command" ) stopgetinfopinggetnettotalsgetnetworkinfogetpeerinfogetconnect ...

  6. 七.OC基础加强--1.内存管理 2.野指针,内存泄露 3.set方法的内存管理 4.@property参数 5.@class和循环retain的使用 6.NSString的内存管理

    1,内存管理简单介绍 1,为什么要有内存管理? malloc selloc dealloc```需要回头复习 一般的内存 4s 是512m内存:6 是1024m内存: 当内存过大时,会耗尽内存.出现程 ...

  7. kafka-net

    基于kafka-net实现的可以长链接的消息生产者 今天有点时间,我就来说两句.最近接触的Kafka相关的东西要多一些,其实以前也接触过,但是在项目使用中的经验不是很多.最近公司的项目里面使用了Kaf ...

  8. bzoj1046 [HAOI2007]上升序列——LIS

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1046 倒序求最长下降子序列,则得到了每个点开始的最长上升子序列: 然后贪心输出即可. 代码如 ...

  9. "cannot be resolved or is not a field"问题解决 (转载)

    转自:http://blog.csdn.net/liranke/article/details/16803295 在修改了资源文件后,出现“"cannot be resolved or is ...

  10. Swift4 函数, 元组, 运算符

    创建: 2018/02/19 完成: 2018/02/19 更新: 2018/02/25 修改标题 [Swift4 函数] -> [Swift4 函数, 元组, 运算符] 更新 :2018/03 ...