第一种方法:
<table>
<tr><td>账号</td><td>密码</td></tr>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><%#Eval("aa") %></td>-- 后台创建的列的名称
<td><%#Eval("bb") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("aa",System.Type.GetType("System.Int32"));//最上边的那一行的所有字段
DataColumn dc1 = new DataColumn("bb", System.Type.GetType("System.String"));
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=123456;database=StudentInfo");
conn.Open();
string sql = "select * from Users";
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
DataRow dr =dt.NewRow();//dr代表行头 NewRow是一行
dr["aa"]=reader["ID"];
dr["bb"] = reader["Pwd"];
dt.Rows.Add(dr);
}
reader.Close();
conn.Close();
Repeater1.DataSource = dt;
Repeater1.DataBind();
-------------------------------------------------------------------------------------------------------------------------
第二种方法:
<%=ss()%>
public string ss()
{
StringBuilder table = new StringBuilder();
table.Append("<table>");
table.Append("<tr><td>序号</td><td>姓名</td><td>密码</td></tr>");
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=123456;database=StudentInfo");
conn.Open();
string sql = "select * from Users";
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
int id = reader.GetInt32(0);
string name = reader["UserName"].ToString();
string pwd = reader["Pwd"].ToString();
table.Append("<tr><td>" + id + "</td><td>" + name + "</td><td>" + pwd + "</td></tr>");
// table.Append("<tr><td>"+reader["ID"]+"</td><td>"+reader["USerName"]+"</td><td>"+reader["Pwd"]+"</td></tr>");
reader[0] 但是建议使用字段名
}
reader.Close();
table.Append("</table>");
return table.ToString();
}
1.Command 2.ExecteReader() 3.read()方法 4.DataReader使用完毕后一定要关闭
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=123456;database=StudentInfo")
string sql = "select * from Users";
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
da.Fill(ds);
Repeater1.DataSource=ds.Tables[0];
Repeater1.DataBind();
-----------------------------------------------------------------------------------------------------------
一个英文字母(不分大小写)占一个字节的空间,一个中文汉字占两个字节(B)的空间 1KB=1024B=512个汉字
1B= 8 bit
1KB=1024B
1MB=1024KB
一个字节由八个二进制位构成,即1个字节等于8个比特(1Byte=8bit)