C#_JDBC连接数据库

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Data.SqlClient;
  7 using System.Data;
  8 
  9 namespace MySchool.DAL
 10 {
 11     public class DBHelper
 12     {
 13         //private const string conn = "Data Source=.;Initial Catalog=MySchool;User ID=GaoFei;Password=111111";
 14         private const string conn = "Data Source=.;Initial Catalog=MySchool;User ID=sa;Password=sa";
 15         private static SqlConnection connection = new SqlConnection(conn);
 16 
 17 
 18         /// <summary>
 19         /// 读取数据
 20         /// </summary>
 21         /// <returns></returns>
 22         public static SqlDataReader Reader(string sql)
 23         {
 24             try
 25             {
 26                 connection.Open();
 27                 SqlCommand comm = new SqlCommand(sql, connection);
 28                 return comm.ExecuteReader(CommandBehavior.CloseConnection);
 29             }
 30             catch (Exception ex)
 31             {
 32                 throw;
 33             }
 34         }
 35 
 36         /// <summary>
 37         /// 增删改数据
 38         /// </summary>
 39         /// <param name="sql"></param>
 40         /// <returns></returns>
 41         public static int ExecuteNonQuery(string sql)
 42         {
 43             try
 44             {
 45                 connection.Open();   //打开数据库连接
 46                 SqlCommand comm = new SqlCommand(sql, connection);
 47                 return comm.ExecuteNonQuery();
 48             }
 49             catch (Exception ex)
 50             {
 51                 throw;
 52             }
 53             finally
 54             {
 55                 connection.Close();
 56             }
 57         }
 58         /// <summary>
 59         /// 返回单个值
 60         /// </summary>
 61         /// <param name="sql"></param>
 62         /// <returns></returns>
 63         public static object ExecuteScalar(string sql)
 64         {
 65             try
 66             {
 67                 connection.Open();   //打开数据库连接
 68                 SqlCommand comm = new SqlCommand(sql, connection);
 69                 return comm.ExecuteScalar();
 70 
 71             }
 72             catch (Exception)
 73             {
 74                 throw;
 75             }
 76             finally
 77             {
 78                 connection.Close();
 79             }
 80         }
 81         /// <summary>
 82         /// 返回数据集
 83         /// </summary>
 84         /// <param name="sql"></param>
 85         /// <param name="tableName"></param>
 86         /// <returns></returns>
 87         public static DataSet Fill(string sql, string tableName)
 88         {
 89             try
 90             {
 91                 connection.Open();  //打开连接
 92                 //创建数据适配器对象
 93                 SqlDataAdapter sda = new SqlDataAdapter(sql, connection);
 94                 //创建数据集
 95                 DataSet ds = new DataSet();
 96                 sda.Fill(ds, tableName); //填充数据集
 97                 return ds;
 98             }
 99             catch (Exception ex)
100             {
101                 throw;
102                 //将异常引发出现
103                 //  throw new Exception(e.Message);
104             }
105             finally
106             {
107                 connection.Close();
108             }
109         }
110     }
111 }

 

转载于:https://www.cnblogs.com/gaofei-1/p/7345051.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值