分类:
.NET
code:
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=oa"); conn.Open(); SqlCommand com = new SqlCommand("select * from Users", conn); //返回SqlDataReader SqlDataReader reader = com.ExecuteReader(); while (reader.Read()) { string name = reader["UserName"].ToString(); string number = reader["number"].ToString(); Console.WriteLine(name + "\t\t" + number); } //DataTable SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(com); DataSet dataSet = new DataSet(); sqlDataAdapter.Fill(dataSet); DataTable dataTable = dataSet.Tables[0]; //解析表格方法1 foreach (DataRow item in dataTable.Rows) { string id = item[0].ToString(); string usernamee = item["username"].ToString(); Console.WriteLine(usernamee); } //解析表格方法2 for (int i = 0; i < dataTable.Rows.Count; i++) { string number = dataTable.Rows[i]["number"].ToString(); Console.WriteLine(number); }
评价