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. JS 省市区三级联动

    JS 省市区三级联动: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  2. soapUI系列之—-04 问题解决 获取接口返回报文response报错

    1. SoapUI+Groovy中"org.apache.xmlbeans.XmlException: error: Unexpected element: CDATA" 通过So ...

  3. .net面试整理

    NET程序员的层次:http://blog.csdn.net/dinglang_2009/article/details/6913852 .NET牛人应该知道些什么http://www.douban. ...

  4. android 自己主动拒接后再取消自己主动拒接,该联系人来电界面无图标显示,且点击挂断无反应

    1.    设置一个联系人为自己主动拒接 2.    该联系人来电 3.    取消该联系人的自己主动拒接 4.    该联系人来电 Error: 来电界面无头像显示,直接显示黑屏,且点击拒接butt ...

  5. mysql 中 unix_timestamp和from_unixtime 时间戳函数

    1.unix_timestamp 将时间转化为时间戳.(date 类型数据转换成 timestamp 形式整数) 没传时间参数则取当前时间的时间戳 mysql> select unix_time ...

  6. 通过GhostDoc实现自定义方法概要(summary)

    首先是下载GhostDoc 来自园友:http://www.cnblogs.com/VAllen/p/GhostDocPro49.html 修改模板 安装好后,修改下模板,工具>GhostDoc ...

  7. hosts所在文件夹以及***

    hosts所在文件夹: Windows 系统hosts位于 C:\Windows\System32\drivers\etc\hosts Android(安卓)系统hosts位于 /etc/hosts ...

  8. ajax跨域问题解决(spring boot)

    之前用的服务器响应头部添加Access-Control-Allow-Origin: *来解决的 public static void setResp(HttpServletResponse resp) ...

  9. SAP事务码 一

    SE80 -- edit source code. SE24 -- class create or display. SFP -- created and maintained independent ...

  10. UVA315 Network —— 割点

    题目链接:https://vjudge.net/problem/UVA-315 A Telephone Line Company (TLC) is establishing a new telepho ...