public DataTable read()
{
string str = "select id,UserName,UserPwd from Users"; //SQL语句
SqlCommand cmd = new SqlCommand(str, conn);
DataTable table = new DataTable();//创建表
DataColumn dc=null;//定义
dc=table.Columns.Add("编号",Type.GetType("System.Int32")); //创建列、列名和类型
dc.AutoIncrement = true; //自动列
dc.AutoIncrementSeed = 1; //起始为1
dc.AutoIncrementStep = 1;//步长为1
dc=table.Columns.Add("用户名",Type.GetType("System.String"));//创建第二列
dc=table.Columns.Add("密码",Type.GetType("System.String"));//创建第三列
DataRow newRow;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
newRow = table.NewRow(); //创建一新行
newRow["编号"] = reader["id"];//给行赋值
newRow["用户名"] = reader["username"].ToString();
newRow["密码"] = reader["userpwd"].ToString();
table.Rows.Add(newRow);//把行添加到表中
}
return table;
}