1.DataAdapter 对象来龙去脉
前面我所介绍的ADO.NET对象中,例如Connection对象,Command对象以及DataReader对象,这些对象均属于Data Provider的一部分,而且都是基于连接的。如果,每次我们检索数据库中的表或者行都需要连接一次数据库,那么性能和效率是十分低下的。实际上,ADO.NET提供了基于非连接的核心组件:DataSet。那么存储的数据集合是从哪里来呢?ADO.NET就为DataSet提供了中介:DataApdater数据适配器。
定义:DataAdapter 对象提供连接 DataSet 对象和数据源的桥梁,DataAdapter 使用 Command 对象在数据源中执行 SQL 命令以向 DataSet 中加载数据,并将对 DataSet 中数据的更改协调回数据源。
2.DataAdapter的工作原理
下面我们以Customer表为例,来理解DataAdapter的工作原理。下图详细描述了一个DataAdapter的工作过程。
3.DataAdapter的属性和方法
3.1属性
尽管DataAdapter类包含很多属性和方法,但很可能每次只使用它们的某个子集。使用DataAdapter可对来自数据源的记录进行操作。通过使用4个DataAdapter属性(指定执行某条SQL语句或调用某个存储过程)中的一个,可以指定所要执行的操作。这些属性实际上是Command类的实例对象:
SelectCommand:引用从数据源中检索行的Command对象。
InsertCommand:引用将插入的行从DataSet写入数据源的Command对象。
UpdateCommand:引用将修改的行从DataSet写入数据源的Command对象。
DeleteCommand:引用从数据源中删除行的Command对象。
3.2方法
使用DataAdapter提供的方法,可以填充DataSet或将DataSet表中的更改传送到相应的数据存储区。这些方法包括:
Fill:使用DataAdapter的这个方法,从数据源增加或刷新行,并将这些行放到DataSet表中。Fill方法调用SelectCommand属性所指定的SELECT语句。
Update:使用DataAdapter对象的这个方法,将DataSet表的更改传送到相应的数据源中。该方法为DataSet的DataTable中每一指定的行调用相应的INSERT、UPDATE或DELETE命令。
4.入门例子
public void SqlAdapterDemo(string connStr){ SqlConnection conn = new SqlConnection(connStr);//连接对象 SqlCommand cmd = conn.CreateCommand();//sql命令对象 cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from products = @ID";//sql语句 cmd.Parameters.Add("@ID", SqlDbType.Int); cmd.Parameters["@ID"].Value = 1;//给参数sql语句的参数赋值 SqlDataAdapter adapter = new SqlDataAdapter();//构造SqlDataAdapter adapter.SelectCommand = cmd;//与sql命令对象绑定,这个必不可少 DataSet ds = new DataSet(); adapter.Fill(ds);//填充数据。第二个参数是数据集中内存表的名字,可以与数据库中的不同 //Fill方法其实是隐藏的执行了Sql命令对象的CommandText
5.SqlHelper类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Data; 6 using System.Data.SqlClient; 7 using System.Configuration; 8 9 ///10 ///SqlHelper create by TerryChan 2012-04-17 11 /// 12 public class SqlHelper 13 { 14 #region 全局配置 15 ///16 /// 连接字符串 17 /// 18 public readonly static string connectionString = ConfigurationManager.ConnectionStrings["connstring"].ToString(); 19 20 ///21 /// SqlConnection对象 22 /// 23 private static SqlConnection conn = new SqlConnection(connectionString); 24 25 #endregion 26 27 #region 打开数据库 28 ///29 /// 打开数据库 30 /// 31 public static void OpenConnection() 32 { 33 if (conn.State != ConnectionState.Open) 34 { 35 try 36 { 37 conn.Open(); 38 } 39 catch (Exception ex) 40 { 41 conn.Dispose(); 42 throw new Exception("打开数据库失败!" + ex.Message); 43 } 44 } 45 } 46 #endregion 47 48 #region 关闭数据库 49 ///50 /// 关闭数据库 51 /// 52 public static void CloseConnection() 53 { 54 if (conn.State == ConnectionState.Open) 55 { 56 try 57 { 58 conn.Close(); 59 } 60 catch (Exception ex) 61 { 62 conn.Dispose(); 63 throw new Exception("关闭数据库失败!" + ex.Message); 64 } 65 } 66 } 67 #endregion 68 69 #region ExecuteNoQuery 执行不返回数据行的操作,并返回一个int类型的数据 70 71 ///72 /// 执行不返回数据行的操作,返回一个int类型的数据 73 /// 74 /// 要执行的SQL文本命令 75 ///返回受影响的行数 76 public static int ExecuteNoQuery(string sql) 77 { 78 return ExecuteNoQuery(sql, CommandType.Text, null); 79 } 80 81 ///82 /// 执行不返回数据行的操作,返回一个int类型的数据 83 /// 84 /// 要执行的SQL文本命令或存储过程名称 85 /// 要执行的查询语句的类型,存储过程或SQL文本命令 86 ///返回受影响的行数 87 public static int ExecuteNoQuery(string sql, CommandType type) 88 { 89 return ExecuteNoQuery(sql, type, null); 90 } 91 92 ///93 /// 执行不返回数据行的操作,返回一个int类型的数据 94 /// 95 /// 要查询的SQL文本命令或存储过程名称 96 /// 要执行的查询语句的类型,存储过程或SQL文本命令 97 /// T-SQL语句或存储过程的参数数组 98 ///返回受影响的行数 99 public static int ExecuteNoQuery(string sql, CommandType type, SqlParameter[] sp)100 {101 try102 {103 OpenConnection();104 SqlCommand command = new SqlCommand(sql, conn);105 command.CommandType = type;106 if (sp != null)107 {108 foreach (SqlParameter parameter in sp)109 {110 command.Parameters.Add(parameter);111 }112 }113 int result = command.ExecuteNonQuery();114 return result;115 }116 catch (Exception ex)117 {118 throw new Exception("ExecuteNoQuery错误:" + ex);119 }120 finally121 {122 CloseConnection();123 }124 }125 126 #endregion127 128 #region ExecuteScalar 执行查询,并返回查询结果集中第一行的第一列129 130 ///131 /// 执行查询结果,返回第一行的第一列132 /// 133 /// 要执行的SQL文本命令134 ///返回第一行的第一列 135 public static object ExecuteScalar(string sql)136 {137 return ExecuteScalar(sql, CommandType.Text, null);138 }139 140 ///141 /// 执行查询结果,返回第一行的第一列142 /// 143 /// 要查询的SQL文本命令或存储过程名称144 /// 要执行的查询语句的类型,存储过程或SQL文本命令145 ///返回第一行的第一列 146 public static object ExecuteScalar(string sql, CommandType type)147 {148 return ExecuteScalar(sql, type, null);149 }150 151 ///152 /// 执行查询结果,返回第一行的第一列153 /// 154 /// 要查询的SQL文本命令或存储过程名称155 /// 要执行的查询语句的类型,存储过程或SQL文本命令156 /// T-SQL语句或存储过程的参数数组157 ///返回第一行的第一列 158 public static object ExecuteScalar(string sql,CommandType type,SqlParameter [] sp)159 {160 try161 {162 object result = null;163 OpenConnection();164 SqlCommand command = new SqlCommand(sql,conn);165 command.CommandType = type;166 if (sp != null)167 {168 foreach (SqlParameter parameter in sp)169 {170 command.Parameters.Add(parameter);171 }172 }173 result = command.ExecuteScalar();174 return result;175 }176 catch (Exception ex)177 {178 throw new Exception("ExecuteScalar错误:" + ex);179 }180 finally181 {182 CloseConnection();183 }184 }185 #endregion186 187 #region ExecuteReader 执行查询,并返回一个 DataReader 对象188 189 ///190 /// 执行查询,并返回一个 DataReader 对象191 /// 192 /// 要执行的SQL文本语句193 ///返回DataReader对象实例 194 public static SqlDataReader ExecuteReader(string sql)195 {196 return ExecuteReader(sql, CommandType.Text, null);197 }198 199 ///200 /// 执行查询,并返回一个 DataReader 对象201 /// 202 /// 要查询的SQL文本命令或存储过程名称203 /// 要执行的查询语句的类型,存储过程或SQL文本命令204 ///返回DataReader对象实例 205 public static SqlDataReader ExecuteReader(string sql,CommandType type)206 {207 return ExecuteReader(sql, type, null);208 }209 210 ///211 /// 执行查询,并返回一个 DataReader 对象212 /// 213 /// 要查询的SQL文本命令或存储过程名称214 /// 要执行的查询语句的类型,存储过程或SQL文本命令215 /// T-SQL语句或存储过程的参数数组216 ///返回DataReader对象实例 217 public static SqlDataReader ExecuteReader(string sql, CommandType type, SqlParameter[] sp)218 {219 try220 {221 OpenConnection();222 SqlCommand command = new SqlCommand(sql, conn);223 command.Parameters.Clear();224 if (sp != null)225 {226 foreach (SqlParameter parameter in sp)227 {228 command.Parameters.Add(parameter);229 }230 }231 SqlDataReader reader = command.ExecuteReader();232 return reader;233 }234 catch (Exception ex)235 {236 throw new Exception("ExecuteReader错误:" + ex);237 }238 finally239 {240 CloseConnection();241 }242 }243 244 #endregion245 246 #region ExecuteDataTable 执行查询,并返回结果集247 248 ///249 /// 执行查询,并返回结果集250 /// 251 /// 要执行的SQL文本语句252 ///查询结果集 253 public static DataTable ExecuteDataTable(string sql)254 {255 return ExecuteDataTable(sql, CommandType.Text, null);256 }257 258 ///259 /// 执行查询,并返回结果集260 /// 261 /// 要执行的SQL文本语句或存储过程262 /// 查询语句类型,存储过程或SQL文本命令263 ///查询结果集 264 public static DataTable ExecuteDataTable(string sql,CommandType type)265 {266 return ExecuteDataTable(sql, type, null);267 }268 269 ///270 /// 执行查询,并返回结果集271 /// 272 /// 要执行的SQL文本语句或存储过程273 /// 查询语句类型,存储过程或SQL文本命令274 /// T-SQL语句或存储过程的参数组275 ///查询结果集 276 public static DataTable ExecuteDataTable(string sql,CommandType type,SqlParameter [] sp)277 {278 try279 {280 DataTable dt = new DataTable();281 SqlCommand command = new SqlCommand(sql,conn);282 if (sp != null)283 {284 foreach (SqlParameter parameter in sp)285 {286 command.Parameters.Add(parameter);287 }288 }289 SqlDataAdapter adapter = new SqlDataAdapter(command);290 adapter.Fill(dt);291 return dt;292 }293 catch (Exception ex)294 {295 throw new Exception("ExecuteDataTable错误:" + ex);296 }297 finally298 {299 CloseConnection();300 }301 }302 #endregion303 }304 }