C#:資料庫SQL操作通用類
1: using System;
2: using System.Data;
3: using System.Data.SqlClient;
4:
5:
6: namespace QxtIntface
7: {
8: class DBCom
9: {
10: // 獲取與資料庫的連線
11: public static SqlConnection GetSqlConnection()
12: {
13: try
14: {
15: SqlConnection scon = new SqlConnection(Global.sConStr);
16: return scon;
17: }
18: catch(Exception ex)
19: {
20: System.Windows.Forms.MessageBox.Show("資料庫連線錯誤!"+ex.Message);
21: return null;
22: }
23: }
24: public static SqlConnection GetSqlConnection(string sConStr)
25: {
26: try
27: {
28: SqlConnection scon = new SqlConnection(sConStr);
29: return scon;
30: }
31: catch (Exception ex)
32: {
33: System.Windows.Forms.MessageBox.Show("資料庫連線錯誤!" + ex.Message);
34: return null;
35: }
36: }
37:
38: // 返回指定資料庫的資料集
39: public static DataSet GetExeQuery(string selectCmd)
40: {
41: SqlConnection con = GetSqlConnection();
42: con.Open();
43: DataSet ds = new DataSet();
44: SqlDataAdapter da = new SqlDataAdapter(selectCmd, con);
45: da.Fill(ds);
46: con.Close();
47: return ds;
48: }
49:
50: // 執行非查詢命令
51: public static int ExeNoneQuery(string sCmd)
52: {
53: int nResult;
54: SqlConnection con = GetSqlConnection();
55: con.Open();
56: SqlCommand cmd = new SqlCommand(sCmd, con);
57: nResult = cmd.ExecuteNonQuery();
58: return nResult;
59: }
60:
61: //返回資料查詢的第一個欄位值(查詢指定表的資料行數)
62: public static int ExeScalar(string selectTableName)
63: {
64: string selectCmd = "select count * from " + selectTableName;
65: SqlConnection con = GetSqlConnection();
66: con.Open();
67: SqlCommand cmd = new SqlCommand(selectCmd, con);
68: return Convert.ToInt32(cmd.ExecuteScalar());
69: }
70: }
71: }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-545255/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C# 操作 access 資料庫C#資料庫
- c#連線SQL Server資料庫C#SQLServer資料庫
- 資料庫常用操作SQL語句資料庫SQL
- T-SQL之資料庫操作SQL資料庫
- SQL資料庫操作語言DCLSQL資料庫
- 資料庫映象 (SQL Server)操作模式資料庫SQLServer模式
- 【SQL】18 SQL NULL 函式、SQL 通用資料型別、SQL 用於各種資料庫的資料型別SQLNull函式資料型別資料庫
- idea內建資料庫 + sql語句庫表操作Idea資料庫SQL
- Query.js - 類SQL前端資料查詢類庫JSSQL前端
- Python3資料庫操作基本類Python資料庫
- C#快速搭建模型資料庫SQLite操作C#模型資料庫SQLite
- C#快速入門教程(27)—— SQL Server資料庫C#SQLServer資料庫
- php簡單操作mysql資料庫的類PHPMySql資料庫
- .NET關於資料庫操作的類-囊括所有的操作資料庫
- C# 連線多種資料庫元件,類庫專案C#資料庫元件
- MySQL資料庫中SQL語句分幾類?MySql資料庫
- C# 資料庫併發的解決方案(通用版、EF版)C#資料庫
- Spark SQL:通用的load和save操作SparkSQL
- Golang原生sql操作Mysql資料庫增刪改查GolangMySql資料庫
- SQL的資料庫操作:新增、更新、刪除、查詢SQL資料庫
- 資料庫操作規範及SQL書寫建議資料庫SQL
- SQL資料庫SQL資料庫
- C#操作Microsoft.Office.Interop.Word類庫完整例子C#ROS
- 資料庫學習與複習筆記--資料庫概念和不同類資料庫CRUD操作(1)資料庫筆記
- 資料庫操作資料庫
- 資料庫操作·資料庫
- 【C#進階】LINQ和資料庫操作_2024-06-22C#資料庫
- Mybatis騷操作-通用查詢工具類MyBatis
- redis-13.資料庫通用指令Redis資料庫
- 【Falsk 使用資料庫】---- 資料庫基本操作資料庫
- MsSql資料庫使用SQL plus建立DDL和DML操作方法SQL資料庫
- 資料庫系統操作規範及SQL書寫建議資料庫SQL
- MySQL 資料庫操作MySql資料庫
- mongodb資料庫操作MongoDB資料庫
- 資料庫基本操作資料庫
- MongoDB 資料庫操作MongoDB資料庫
- laravel 資料庫操作Laravel資料庫
- c# Lambda操作類封裝C#封裝
- C#中PDF文件操作類C#