数据库文件 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 ()); } } }