C# MSSQL数据库类

北战南征 提交于 2020-03-23 10:34:13

嘿嘿,今晚无聊写了个MSSQL操作类,算是C#征途的一个起步,博客园的各位朋友尽量拍砖!我的进步靠大家,希望大家不吝赐教!!

 1     class MsSql
 2     {
 3         private string connectString;
 4 
 5         public MsSql(string connString)
 6         {
 7             connectString = connString;   
 8         }
 9 
10         /// <summary>
11         /// 查询数据库,并返回DataSet集
12         /// </summary>
13         /// <param name="cmdString">查询语句</param>
14         /// <returns>返回DataSet</returns>
15         public DataSet GetDataSet(string cmdString)
16         {
17             SqlConnection sqlConn = new SqlConnection(connectString);
18 
19             SqlDataAdapter da = new SqlDataAdapter(cmdString, sqlConn);
20             DataSet ds = new DataSet();
21             try
22             {
23                 sqlConn.Open();
24                 
25                 da.Fill(ds);
26                 //if (ds != null || ds.Tables.Count > 0)
27             }
28             catch (SqlException sqlex)
29             {
30                 Console.WriteLine(sqlex.Message);
31             }
32             sqlConn.Close();
33             return ds;
34         }
35 
36         /// <summary>
37         /// 取得表
38         /// </summary>
39         /// <param name="cmdString">查询字符串</param>
40         /// <returns>DataTable</returns>
41         public DataTable GetDataTable(string cmdString)
42         {
43             SqlConnection sqlConn = new SqlConnection(connectString);
44 
45             SqlDataAdapter da = new SqlDataAdapter(cmdString, sqlConn);
46             DataSet ds = new DataSet();
47             try
48             {
49                 sqlConn.Open();
50                 da.Fill(ds);
51             }
52             catch (SqlException sqlex)
53             {
54                 Console.WriteLine(sqlex.Message);
55             }
56             DataTable MyDataTable = ds.Tables[0];
57             sqlConn.Close();
58             return MyDataTable;
59         }
60 
61         /// <summary>
62         /// 执行sql语句
63         /// </summary>
64         /// <param name="cmdString">查询,更新,删除语句</param>
65         /// <returns>成功返回true,失败返回false</returns>
66         public bool ExecuteQuery(string cmdString)
67         {
68             SqlConnection conn = new SqlConnection(connectString);
69 
70             SqlCommand cmd = new SqlCommand(cmdString,conn);
71             cmd.CommandText = cmdString;
72             try
73             {
74                 conn.Open();
75 
76                 if (cmd.ExecuteNonQuery() > 0)
77                 {
78                     conn.Close();
79                     return true;
80                 }
81                 else
82                 {
83                     return false;
84                 }
85             }
86             catch (SqlException Sqlex)
87             {
88                 Console.WriteLine(Sqlex.Message);
89                 return false;
90             }

91         } 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!