• C#之连接SqlServer数据库


     1 using System.Data;
     2 using System.Data.SqlClient;
     3 
     4 namespace SQLServer
     5 {
     6     public class SQLServerDataBase
     7     {
     8         string _connString = "server=127.0.0.1;database=User;uid=sa;pwd=123";
     9 
    10         public SQLServerDataBase(string connStr)
    11         {
    12             _connString = connStr;
    13         }
    14 
    15         public bool OpenDataBase()
    16         {
    17             try
    18             {
    19                 //创建数据库连接对象
    20                 using (SqlConnection sqlConn = new SqlConnection(_connString))
    21                 {
    22                     //打开连接
    23                     sqlConn.Open();
    24                     sqlConn.Close();
    25                     return true;
    26                 }
    27             }
    28             catch
    29             {
    30                 return false;
    31             }
    32         }
    33 
    34         ///<summary>
    35         ///定义函数根据传入的参数,执行SQL语句获取影响的行数
    36         ///</summary>
    37         public bool ExecuteNonQuery(string sql, out int iResult)
    38         {
    39             iResult = 0;
    40             //创建连接数据库对象
    41             using (SqlConnection sqlConn = new SqlConnection(_connString))
    42             {
    43                 //打开连接
    44                 sqlConn.Open();
    45                 //创建执行SQL语句对象
    46                 using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn))
    47                 {
    48                     iResult = sqlCmd.ExecuteNonQuery();
    49                     sqlConn.Close();
    50                     sqlCmd.Dispose();
    51                     return true;
    52                 }
    53             }
    54         }
    55 
    56         ///<summary>
    57         ///定义函数根据传入的参数,执行SQL语句获取数据集DataSet对象
    58         ///</summary>
    59         public bool ExecuteNonDataSet(string sql, out DataSet dataSet)
    60         {
    61             dataSet = null;
    62             //创建连接数据库对象
    63             using (SqlConnection sqlConn = new SqlConnection(_connString))
    64             {
    65                 //创建执行SQL语句对象
    66                 using (SqlDataAdapter sqlData = new SqlDataAdapter(sql, sqlConn))
    67                 {
    68                     //打开连接
    69                     sqlConn.Open();
    70                     dataSet = new DataSet();
    71                     sqlData.Fill(dataSet, "ds");
    72                     sqlConn.Close();
    73                     sqlData.Dispose();
    74                     return true;
    75                 }
    76             }
    77 
    78         }
    79 
    80     }
    81 }
  • 相关阅读:
    AI常用环境安装
    ubantu打开摄像头失败
    python 从ubantu环境迁移到windows环境
    mystar01 nodejs MVC 公共CSS,JS设置
    Golang数据类型之结构体-上篇
    Golang基准测试
    浅谈Prometheus的数据存储
    Golang单元测试
    Jenkins连接k8s的多种姿势
    Golang数据类型之指针
  • 原文地址:https://www.cnblogs.com/bridgew/p/11435895.html
Copyright © 2020-2023  润新知