SQL Server是微軟開發(fā)的關系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS),是企業(yè)級應用的主流數(shù)據(jù)庫解決方案之一,具有強大的數(shù)據(jù)處理能力和豐富的功能集。
SQL Server核心特點:
企業(yè)級性能:支持TB級數(shù)據(jù)庫,高性能事務處理
高可用性:提供Always On可用性組、故障轉(zhuǎn)移集群等方案
全面安全:行級安全、動態(tài)數(shù)據(jù)掩碼、透明數(shù)據(jù)加密
商業(yè)智能:集成SSIS、SSAS、SSRS等BI工具
云集成:完美支持Azure云服務
多平臺支持:支持Windows、Linux和容器部署
SQL Server版本分類:
企業(yè)版(Enterprise):全功能版本,適合大型企業(yè)
標準版(Standard):中小型企業(yè)適用
開發(fā)版(Developer):功能同企業(yè)版,僅用于開發(fā)和測試
Express版:免費版本,有功能限制
Web版:專為Web主機服務商設計
下面為SQLServer法:連接、增、刪、改、查實例:
1、數(shù)據(jù)庫調(diào)用函數(shù)如下:
using System.Data.SqlClient;string DbConnectionString = "data source=IP地址;initial catalog=數(shù)據(jù)庫名稱;user id=用戶名;password=密碼;packet size=4096;pooling=true;min pool size=5;max pool size=512;persist security info=False";bool RetState = GetConnnectState(); DataTable dt = GetDataTable(SelectSqlStr);string SqlStr = "添加、更新、刪除SQL命令";int RetNum = ExecuteSqlTran(SqlStr);
2、獲取數(shù)據(jù)庫連接狀態(tài):
public bool GetConnnectState(){ bool RetState = false; try { if (DbConnectionString == "") return false; using (SqlConnection connection = new SqlConnection(DbConnectionString)) { if (connection.State != ConnectionState.Open) { connection.Open(); } if (connection.State == ConnectionState.Open) { RetState = true; } } } catch (Exception) { RetState = false; } return RetState;}
3、查詢語句,返回DataTable:
public DataTable GetDataTable(string sql, string szTableName = "DataTable"){ DataSet ds = new DataSet(); try { using (SqlConnection connection = new SqlConnection(DbConnectionString)) { connection.Open(); using (SqlCommand command = new SqlCommand(sql, connection)) { using (SqlDataAdapter adapter = new SqlDataAdapter(command)) { try { adapter.Fill(ds, szTableName); adapter.Dispose(); command.Dispose(); connection.Dispose(); } catch (Exception) { adapter.Dispose(); command.Dispose(); connection.Dispose(); } } } } } catch (Exception ex) { return null; } if (ds == null) return null; return ds.Tables[0];}
public int ExecuteSqlTran(string SQLString){ int rows = 0; using (SqlConnectionconn = new SqlConnection(DbConnectionString)) { if (conn.State != ConnectionState.Open) conn.Open(); using (SqlTransaction transaction = conn.BeginTransaction()) { using (SqlCommandcmd = conn.CreateCommand()) { try { cmd.Transaction = transaction; cmd.CommandText = SQLString; cmd.CommandTimeout = 5 * 1000 * 60; rows = cmd.ExecuteNonQuery(); transaction.Commit(); cmd.Dispose(); conn.Dispose(); } catch (Exception ex) { transaction.Rollback(); cmd.Dispose(); conn.Dispose(); } } } } return rows;}
該文章在 2025/8/11 15:02:35 編輯過