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. Rust 1.7.0 语法基础 sep_token 和 non_special_token

    一.分隔符 sep_token 指的是分隔符, 是除了 * 和 + 之外的不论什么符号,通常情况下是使用 , 逗号. 比如: 宏的多个參数分隔,以下代码中的逗号就是 sep_token (target ...

  2. CRF图像切割简单介绍

    这里主要是讲Conditional Random Fields(CRF)用于pixel-wise的图像标记(事实上就是图像切割).CRF经经常使用于 pixel-wise的label 预測.当把像素的 ...

  3. 【iOS-Tips】-工具Tip

    [iOS-Tips]-工具Tip 1.Xcode自带头文件的路径 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulato ...

  4. 在webkit中如何避免触发layout(重排)

    很多web开发者都已经意识到,在脚本执行中,DOM操作的用时可能比js本身执行时间要长很多,其中潜在的消耗基本上是由于触发了layout(即重排reflow:由DOM树构建为Render渲染树的过程) ...

  5. CXF 中自定义SOAPHeader

    Interceptor是CXF架构中一个很有特色的模式.你可以在不对核心模块进行修改的情况下,动态添加很多功能.这对于CXF这个以处理消息为中心的服务框架来说是非常有用的,CXF通过在Intercep ...

  6. css中vertical-align和line-height的用法

    css中vertical-align和line-height的用法 1.先来看一种现象: (1).将一个图片放入一个div块中,div块背景颜色设置为aquamarine.将会发现图片与div块下边沿 ...

  7. adbi命令【转】

    本文转载自:https://zmywly8866.github.io/2015/01/24/all-adb-command.html   ADB很强大,记住一些ADB命令有助于提高工作效率. 获取序列 ...

  8. ubuntu php5.6源码安装

    本系列的lnmp的大框架基本上是按照http://www.linuxzen.com/lnmphuan-jing-da-jian-wan-quan-shou-ce-si-lnmpda-jian-yuan ...

  9. IIS application pool access desktop denied

    https://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permiss ...

  10. .NETFramework:MemoryStream

    ylbtech-.NETFramework:MemoryStream 1.返回顶部 1. #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, ...