决定学习ASP.NET,开始学习感觉像在用Delphi,很多东西封装的太高层了,想实现一些原始些的功能都很麻烦,最简单的一个查询数据库,很多都用的 SqlDataSource 不过经过2个小时的研究,还是研究出来了。
- protected void Button1_Click(object sender, EventArgs e)
- {
- string connectionString = "Data Source=.;Initial Catalog=tempMyDB;Integrated Security=True";
- SqlConnection conn = new SqlConnection(connectionString);
- conn.Open();
- string Sql = "select * from InfTable";
- SqlCommand cmd = new SqlCommand(Sql, conn);
- SqlDataReader myData = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
- while (myData.Read())
- {
- Response.Write(myData["name"]);
- Response.Write("<br/>");
- }
- myData.Close();
- conn.Close();
- }
- //方法二
- SqlDataAdapter dataApt = new SqlDataAdapter(Sql, conn);
- DataSet ds = new DataSet();
- dataApt.Fill(ds);
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- Response.Write(ds.Tables[0].Rows[i]["name"].ToString());
- Response.Write("<br/>");
- }
在这里做个笔记,以后方便找。
发表于 @ 2008年12月01日 16:52:00|评论(loading...)|收藏