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. 【spring+websocket的使用】

    一.spring配置文件Java代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...

  2. C++手稿:std::string

    字符串在非常多编程语言中已经成为基本数据类型,C语言中我们使用char*来手动申请和维护字符串, 在C++中,能够使用std::string来方便地创建和操作字符串. string是一个模板类.它有b ...

  3. [IT学习]转载python 项目 计算器

    这个是从网上搜到的Python小项目之计算器(原文地址:http://www.2cto.com/kf/201402/279637.html).但该段代码估计是Python 2 写的. 如果你使用的程序 ...

  4. Supporting IPv6-only Networks

    Supporting IPv6-only Networks - Support - Apple Developer https://developer.apple.com/support/ipv6/ ...

  5. RK3288 make otapackage 出错的问题【转】

    本文转载自:http://blog.csdn.net/u010439962/article/details/51734631 Installed file list: out/target/produ ...

  6. IOS UI 设计 技术

    AutoLayout AutoLayout是一种基于约束的,描述性的布局系统. 程序员—-(cgrect)—>frame(center+bounds)    =====>   程序员—(N ...

  7. 用dpkg命令制作deb包方法总结

    用dpkg命令制作deb包方法总结 如何制作Deb包和相应的软件仓库,其实这个很简单.这里推荐使用dpkg来进行deb包的创建.编辑和制作. 首先了解一下deb包的文件结构: deb 软件包里面的结构 ...

  8. svn问题:在eclipse里面使用SVN,怎么实现版本回滚呢?

    共有4个答案 我要回答» JustForFly 回答于 2012-04-27 10:20 举报   想回到SVN服务器端的最新版本就使用 team->还原.. 想回到SVN服务器端的其它版本使用 ...

  9. Nginx反向代理服务器、负载均衡和正向代理

    Nginx("engine x")是一个高性能的 HTTP 和反向代理服务器,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日.官方测试 nginx 能够支撑5万 ...

  10. 转3xian之所在 (一位ACM大牛的博文)

    3xian的经历和见解...我深思... 最后一天,漫天飘起了雪花,假装欢送我离去. 这次WF之战不太顺利,早期的C题大概花了1秒钟构思,然而由于输出格式多了一个空格直到两个半小时才逃脱Wrong A ...