数据库文件提取 dataset 和 SqlDataReader 的使用
2013-10-16 16:30
519 查看
数据库文件 dataset 离线数据代码,用来把服务器拿到的数据加载在本地服务器当中,
// 数据库列查询代码
using ( SqlConnection conn = new SqlConnection ("Data Source=.; Initial Catalog=Title;User ID=sa;Password= new SqlConnection ("Data Source=.; Initial Catalog=Title;User ID=sa;Password= new SqlConnection ("Data Source=.; Initial Catalog=Title;User ID=sa;Password=mima")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { //写数据库的命令 cmd.CommandText = "select *from T_Student where Age > @age"; cmd.Parameters.Add(new SqlParameter ("@age" , 60)); //创建数据库接合器 把cmd查询到的结构给adpater SqlDataAdapter adapter = new SqlDataAdapter(cmd); //创建本地的一个集合进行接收 //dataSet类表示数据在内存中缓存 DataSet list = new DataSet(); // 把command的结果 填到 到list中 adapter.Fill(list); //下面是数据的遍历 //DeataTable 表示内存当中的数据表 DataTable table = list.Tables[0]; // 获取当中行的 集合 DataRowCollection rows = table.Rows; //遍历当中的行 for (int i = 0; i < rows.Count; i++) { //获取当rows当中一行的数据 DataRow row = rows[i]; int age = (int ) row["Age"]; string name = (string)row["Name"]; MessageBox.Show(age + " " + name); } } }
// 数据库列查询代码
using (SqlCommand cmd = conn.CreateCommand()) { //给查询询命令提供参数 cmd.CommandText = "select age from T_Student where Name =@name"; cmd.Parameters.Add(new SqlParameter( "@name",txtShow.Text)); using ( SqlDataReader read = cmd.ExecuteReader()) { //数据库查询 while (read.Read()) { int age = read.GetInt32(0); MessageBox.Show(age.ToString ()); } } }
相关文章推荐
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- ADO内存数据对象SqlDataAdapter类使用SqlDataAdapter在数据库和DataSet之间架起桥梁:
- 使用OleDbDataReader 或sqldatareader 连数据库
- ASP.NET优化连载(三)只读数据访问用SqlDataReader,不要使用DataSet
- 使用Visual Studio自带数据库配合SqlDataReader 读取数据库数据
- 使用SqlDataReader来操作数据库
- ADO.NET访问数据库-SqlCommand的ExecuteReader方法一般配合sqldatareader使用
- 菜鸟学习C#数据库实例练习使用SqlDataReader,SqlCommand SqlConnection
- asp.net中SqlDataReader使用时关闭数据库连接的问题(转)
- 有关数据库查询返回为空与取特定值dataset,datatable,sqldatareader
- C#中DataSet、SqlDataAdapter的使用-关于数据库操作
- c#数据库访问返回值类型为SqlDataReader时使用using时注意的问题
- c#学习笔记(数据库连接以及SqlDataReader、SqlCommand的使用)
- sqldataReader,sqldataAdapter,DataSet的使用
- 使用SqlDataReader应该注意的问题,使用datareader没有获取到分页的output值【来自Mist .NET blog】
- SqlDataReader 与 DataSet读取sql中的数据
- SqlDataReader 与SqlDataAdapter+DataSet 的区别
- SqlDataReader使用
- 数据读取器对象SqlDataReader与数据适配器对象SqlDataAdapter的使用
- sqlconnection,sqlcommand,sqldataadapter,sqldatareader,dataset