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. android &lt;application&gt; 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客仅仅要没有注明"转",那么均为原创.转贴请注明本博客链接链接 <application>语法:    <appl ...

  2. Tcl学习之--列表|字典

    [列表|字典] Tcl使用列表来处理各种集合,比方一个目录中的全部文件,以及一个组件的全部选项.最简单的列表就是包括由随意个空格.制表符.换行符.分隔的随意多个元素的字符串.比方: JerryAlic ...

  3. HDOJ 4455 Substrings 递推+树状数组

    pre[i]第i位数往前走多少位碰到和它同样的数 dp[i]表示长度为i的子串,dp[i]能够由dp[i-1]加上从i到n的pre[i]>i-1的数减去最后一段长度为i-1的断中的不同的数得到. ...

  4. HiWorkV1.3版震撼公布,今日起正式公开測试!

    今天HiWork迎来了公开測试和V1.3大版本号更迭,HiWork集成的机器人达到20种,未读消息提醒亦可从不同维度进行设置,不断变好真是件振奋人心的事儿呢. 在这个看重颜值(kan lian)的互联 ...

  5. 5 微信票据 access_token--开发微信的第二道坎儿

    一 access_token基本概念 定义:access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存. 时效性:access_ ...

  6. 生成随机string

    转自:http://blog.csdn.net/yaodong_y/article/details/8115250 字母与数字的ASCII码 目 前计算机中用得最广泛的 字符集及其编码,是由美国国家标 ...

  7. 2016/05/25 empty() 与 isset()的区别

    对于初学php的人来说,empty()和和isset()用法的区别是很难搞清楚的,他们的用法的差别不仔细去琢磨的话确实很难弄清楚. 先说一下他们的共同点: 都可以判定一个变量是否为空: 都返回bool ...

  8. Codeforces 768 E. Game of Stones 博弈DP

    E. Game of Stones   Sam has been teaching Jon the Game of Stones to sharpen his mind and help him de ...

  9. plink 与 ssh 远程登录问题

    plink 是一种 putty-tools,ubuntu 环境下,如果没有安装 plink,可通过如下方法进行安装: $ echo y | sudo apt-get install plink 1. ...

  10. 【POJ 1155】TELE

    [题目链接] 点击打开链接 [算法] 树形DP f[i][j]表示以i为根的子树中,选了j个叶子节点,所能带来的最大收益 不难发现这就是一个经典的背包问题,不过是在树上做背包罢了 最后,判断f[1][ ...