数据库得操作无非是增删查改,增删改由ExecuteNonQuery实现,查由SqlDataReader实现,dataset主要用于绑定表格数据。
SqlDataReader
public static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership) { SqlCommand sqlCommand = new SqlCommand(); SqlDataReader result; try { connection.Open(); PrepareCommand(sqlCommand, connection, transaction, commandType, commandText, commandParameters); SqlDataReader sqlDataReader; if (connectionOwnership == SqlConnectionOwnership.External) { sqlDataReader = sqlCommand.ExecuteReader(); } else { sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection); } bool flag2 = true; foreach (SqlParameter sqlParameter in sqlCommand.Parameters) { if (sqlParameter.Direction != ParameterDirection.Input) { flag2 = false; } } if (flag2) { sqlCommand.Parameters.Clear(); } result = sqlDataReader; } catch(Exception e) { throw e; } finally { } return result; }
SqlParameter
public static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters) { if (command == null) { throw new ArgumentNullException("command"); } if (commandParameters != null) { for (int i = 0; i < commandParameters.Length; i++) { SqlParameter sqlParameter = commandParameters[i]; if (sqlParameter != null) { if ((sqlParameter.Direction == ParameterDirection.InputOutput || sqlParameter.Direction == ParameterDirection.Input) && sqlParameter.Value == null) { sqlParameter.Value = DBNull.Value; } command.Parameters.Add(sqlParameter); } } } } public static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters) { if (command == null) { throw new ArgumentNullException("command"); } if (commandText == null || commandText.Length == 0) { throw new ArgumentNullException("commandText"); } if (connection.State != ConnectionState.Open) { connection.Open(); } command.Connection = connection; command.CommandText = commandText; if (transaction != null) { if (transaction.Connection == null) { throw new ArgumentException("打开状态的事务允许数据操作回滚或者提交。", "事务"); } command.Transaction = transaction; } command.CommandType = commandType; if (commandParameters != null) { AttachParameters(command, commandParameters); } }
DataSet
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) { if (connection == null) { throw new ArgumentNullException("connection"); } SqlCommand sqlCommand = new SqlCommand(); PrepareCommand(sqlCommand, connection, null, commandType, commandText, commandParameters); DataSet result; using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand)) { DataSet dataSet = new DataSet(); sqlDataAdapter.Fill(dataSet); sqlCommand.Parameters.Clear(); result = dataSet; } return result; }
源码
https://git.oschina.net/zkzk945/AdoNetSqlServer.git 源码实现了数据库得增删查改。