分类:
NET
放回datatable
//1:开打数据库的连接 using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=test")) { conn.Open(); //2:构建执行sql语句的对象,指定一个sql语句 using (SqlCommand com = new SqlCommand("select * from UserInfo", conn)) { //3:执行sql填充到DataTable SqlDataAdapter adapter = new SqlDataAdapter(com); DataTable table = new DataTable(); adapter.Fill(table); return table; } }
返回sqldatareader
//1:开打数据库的连接 using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=test")) { conn.Open(); //2:构建执行sql语句的对象,指定一个sql语句 using (SqlCommand com = new SqlCommand("select * from UserInfo", conn)) { //3:执行sql返回SqlDataReader SqlDataReader reader = com.ExecuteReader(); //读取(SqlDataReader一行一行的读取) while (reader.Read()) { string userAcount = reader[1].ToString(); string username = reader["username"].ToString(); string userSex = reader["userSex"].ToString(); } } }
评价