sqlconnection,sqldataadapter,sqlcommand究竟是什么意思

本文详细介绍了在Microsoft SQL Server数据库环境中使用的几种关键组件:SqlConnection用于建立与数据库的连接;SqlDataAdapter作为数据集和数据库之间的桥梁,实现数据的填充与更新;SqlCommand用于执行SQL语句或存储过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 【转】http://hi.baidu.com/starwfx/blog/item/7f21aec238335b33e5dd3baf.html

 

SqlConnection
表示 SQL Server 数据库的一个打开的连接。

System.Data.SqlClient.SqlConnection

SqlConnection 对象表示与 SQL Server 数据源的一个唯一的会话。对于客户端/服务器数据库系统,它相当于到服务器的网络连接。SqlConnection 与 SqlDataAdapter 和 SqlCommand 一起使用,以便在连接 Microsoft SQL Server 数据库时提高性能。对于所有第三方 SQL 服务器产品以及其他支持 OLE DB 的数据源,请使用 OleDbConnection。






SqlDataAdapter
表示用于填充 DataSet 和更新 SQL Server 数据库的一组数据命令和一个数据库连接。

System.Data.SqlClient.SqlDataAdapter

SqlDataAdapter 是 DataSet 和 SQL Server 之间的桥接器,用于检索和保存数据。SqlDataAdapter 通过对数据源使用适当的 Transact-SQL 语句映射 Fill(它可更改 DataSet 中的数据以匹配数据源中的数据)和 Update(它可更改数据源中的数据以匹配 DataSet 中的数据)来提供这一桥接。

SqlDataAdapter 与 SqlConnection 和 SqlCommand 一起使用,以便在连接到 Microsoft SQL Server 数据库时提高性能。若要访问其他数据源,可使用 OleDbDataAdapter 以及与之关联的 OleDbCommand 和 OleDbConnection。

SqlDataAdapter 还包括 SelectCommand、InsertCommand、DeleteCommand、UpdateCommand 和 TableMappings 属性,使数据的加载和更新更加方便。






SqlCommand
表示要对 SQL Server 数据库执行的一个 Transact-SQL 语句或存储过程。

System.Data.SqlClient.SqlCommand


SqlCommand 特别提供了以下对 SQL Server 数据库执行命令的方法:

ExecuteReader 执行返回行的命令。为了提高性能,ExecuteReader 使用 Transact-SQL sp_executesql 系统存储过程调用命令。因此,如果用于执行像 Transact-SQL SET 语句这样的命令,ExecuteReader 可能无法获得预期效果。

ExecuteNonQuery 执行如 Transact-SQL INSERT、DELELE、UPDATE 和 SET 语句等命令。

ExecuteScalar 从数据库中检索单个值(例如一个聚合值)。

ExecuteXmlReader 将 CommandText 发送到 Connection 并生成一个 XmlReader 对象。

using System; using System.Collections; using System.Data; using System.Data.SqlClient; namespace RxjhServer.DbClss { public class DBA { public static void serlog(string txt) { string sqlJl = World.SqlJl; if (sqlJl.Length == 0) { return; } string text = sqlJl; char[] separator = new char[1] { '|' }; string[] array = text.Split(separator); string[] array2 = array; string[] array3 = array2; foreach (string text2 in array3) { if (txt.ToLower().IndexOf(text2.ToLower()) != -1) { Form1.WriteLine(99, txt); } } } public static void serlog(string txt, SqlParameter[] prams) { string sqlJl = World.SqlJl; if (sqlJl.Length == 0) { return; } string[] array = sqlJl.Split('|'); for (int i = 0; i < array.Length; i++) { if (txt.ToLower().IndexOf(array[i].ToLower()) != -1) { Form1.WriteLine(99, txt); } } for (int j = 0; j < array.Length; j++) { foreach (SqlParameter sqlParameter in prams) { if (sqlParameter.SqlValue.ToString().ToLower().IndexOf(array[j].ToLower()) != -1) { Form1.WriteLine(99, txt + " " + sqlParameter.SqlValue.ToString()); } } } } public static void Setlog(string txt, SqlParameter[] prams, Exception ex) { Form1.WriteLine(100, "-----------DBA数据层_错误-----------"); Form1.WriteLine(100, txt); if (prams != null) { foreach (SqlParameter sqlParameter in prams) { Form1.WriteLine(100, sqlParameter.SqlValue.ToString()); } } Form1.WriteLine(100, ex.Message); } public static string getstrConnection(string db) { try { if (db == null) { db = "GameServer"; } World.sql = (World.Db.TryGetValue(db, out var value) ? value.SqlConnect : null); DbClass value2; return World.Db.TryGetValue(db, out value2) ? value2.SqlConnect : null; } catch { return null; } } public static int ExeSqlCommand(string sqlCommand, SqlParameter[] prams) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, prams, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand, SqlParameter[] prams, string server) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlCommand sqlCommand2 = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, prams, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, null, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand, string server) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); int result = -1; try { sqlConnection.Open(); } catch { return -1; } try { result = sqlCommand2.ExecuteNonQuery(); } catch (Exception ex) { Setlog(sqlCommand, null, ex); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static int ExeSqlCommand(string sqlCommand, ref Exception exception, string db) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch (Exception ex) { Exception ex2 = (exception = ex); return -1; } int result = sqlCommand2.ExecuteNonQuery(); sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static DataTable GetDBToDataTable(string sqlCommand, SqlParameter[] prams) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand5 = (sqlDataAdapter.SelectCommand = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams)); SqlCommand sqlCommand2 = sqlCommand5; SqlCommand sqlCommand3 = sqlCommand2; using (sqlCommand3) { try { sqlConnection.Open(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex2) { Setlog(sqlCommand, prams, ex2); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataTable GetDBToDataTable(string sqlCommand, SqlParameter[] prams, string server) { serlog(sqlCommand, prams); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand5 = (sqlDataAdapter.SelectCommand = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams)); SqlCommand sqlCommand2 = sqlCommand5; SqlCommand sqlCommand3 = sqlCommand2; using (sqlCommand3) { try { sqlConnection.Open(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex2) { Setlog(sqlCommand, prams, ex2); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataTable GetDBToDataTable(string sqlCommand) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand5 = (sqlDataAdapter.SelectCommand = new SqlCommand(sqlCommand, sqlConnection)); SqlCommand sqlCommand2 = sqlCommand5; SqlCommand sqlCommand3 = sqlCommand2; using (sqlCommand3) { try { sqlConnection.Open(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex2) { Setlog(sqlCommand, null, ex2); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataTable GetDBToDataTable(string sqlCommand, string server) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(server)); using SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); SqlCommand sqlCommand5 = (sqlDataAdapter.SelectCommand = new SqlCommand(sqlCommand, sqlConnection)); SqlCommand sqlCommand2 = sqlCommand5; SqlCommand sqlCommand3 = sqlCommand2; using (sqlCommand3) { try { sqlConnection.Open(); } catch { return null; } DataTable dataTable = new DataTable(); try { sqlDataAdapter.Fill(dataTable); } catch (Exception ex) { Setlog(sqlCommand, null, ex); } sqlDataAdapter.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return dataTable; } } public static DataRowCollection GetDBValue(string sqlCommand, string db) { return GetDBToDataTable(sqlCommand).Rows; } public static ArrayList GetDBValue_1(string sqlCommand, string db) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader(); if (!sqlDataReader.HasRows) { sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return null; } ArrayList arrayList = new ArrayList(); if (sqlDataReader.Read()) { for (int i = 0; i < sqlDataReader.FieldCount; i++) { arrayList.Add(sqlDataReader[i]); } } sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); sqlCommand2.Dispose(); return arrayList; } public static ArrayList GetDBValue_2(string sqlCommand, string db) { serlog(sqlCommand); using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader(); if (!sqlDataReader.HasRows) { sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return null; } ArrayList arrayList = new ArrayList(); while (sqlDataReader.Read()) { arrayList.Add(sqlDataReader[0]); } sqlDataReader.Close(); sqlDataReader.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); sqlCommand2.Dispose(); return arrayList; } public static object GetDBValue_3(string sqlCommand) { serlog(sqlCommand); object result = null; using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } try { result = sqlCommand2.ExecuteScalar(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static object GetDBValue_3(string sqlCommand, SqlParameter[] prams) { serlog(sqlCommand, prams); object result = null; using SqlConnection sqlConnection = new SqlConnection(getstrConnection(null)); using SqlCommand sqlCommand2 = SqlDBA.CreateCommandSql(sqlConnection, sqlCommand, prams); try { sqlConnection.Open(); } catch { return null; } try { result = sqlCommand2.ExecuteScalar(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } public static object GetDBValue_3(string sqlCommand, string db) { serlog(sqlCommand); object result = null; using SqlConnection sqlConnection = new SqlConnection(getstrConnection(db)); using SqlCommand sqlCommand2 = new SqlCommand(sqlCommand, sqlConnection); try { sqlConnection.Open(); } catch { return null; } try { result = sqlCommand2.ExecuteScalar(); } catch (Exception ex) { Form1.WriteLine(100, "DBA数据层_错误" + ex.Message + " " + sqlCommand); } sqlCommand2.Dispose(); sqlConnection.Close(); sqlConnection.Dispose(); return result; } } } 这是我的DBA类
最新发布
07-19
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值