SQL Server是微軟開(kāi)發(fā)的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(RDBMS),是企業(yè)級(jí)應(yīng)用的主流數(shù)據(jù)庫(kù)解決方案之一,具有強(qiáng)大的數(shù)據(jù)處理能力和豐富的功能集。
SQL Server核心特點(diǎn):
企業(yè)級(jí)性能:支持TB級(jí)數(shù)據(jù)庫(kù),高性能事務(wù)處理
高可用性:提供Always On可用性組、故障轉(zhuǎn)移集群等方案
全面安全:行級(jí)安全、動(dòng)態(tài)數(shù)據(jù)掩碼、透明數(shù)據(jù)加密
商業(yè)智能:集成SSIS、SSAS、SSRS等BI工具
云集成:完美支持Azure云服務(wù)
多平臺(tái)支持:支持Windows、Linux和容器部署
SQL Server版本分類:
企業(yè)版(Enterprise):全功能版本,適合大型企業(yè)
標(biāo)準(zhǔn)版(Standard):中小型企業(yè)適用
開(kāi)發(fā)版(Developer):功能同企業(yè)版,僅用于開(kāi)發(fā)和測(cè)試
Express版:免費(fèi)版本,有功能限制
Web版:專為Web主機(jī)服務(wù)商設(shè)計(jì)
下面為SQLServer法:連接、增、刪、改、查實(shí)例:
1、數(shù)據(jù)庫(kù)調(diào)用函數(shù)如下:
using System.Data.SqlClient;string DbConnectionString = "data source=IP地址;initial catalog=數(shù)據(jù)庫(kù)名稱;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ù)庫(kù)連接狀態(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、查詢語(yǔ)句,返回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 編輯過(guò)