在ASP.Net窗口中,进行数据库的查询,添加,删除,修改操作
在网页中使用GridView进行数据库表单的显示,以及基本的数据库操作,SqlCommand和SqlDataAdapter
查询功能,插入数据功能:(在Textbox中输入内容,点击按钮执行查询插入功能)
protected void Button1_Click(object sender, EventArgs e) //查询功能 { if (this.TextBox1.Text != "") //判断输入框是否为空 { //与数据库进行连接 SqlConnection myConn = new SqlConnection("Data Source=WIN8;Initial Catalog=SqlDataTest01;Persist Security Info=True;User ID=sa;Password=123456"); myConn.Open(); //查询操作 string sqlStr = "select * from UserTable where UserName=@UserName"; SqlCommand myCmd = new SqlCommand(sqlStr, myConn); myCmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = this.TextBox1.Text.Trim(); SqlDataAdapter myDa = new SqlDataAdapter(myCmd); //将查询的结果添加到DataSet中 DataSet myDs = new DataSet(); myDa.Fill(myDs); //在GridView中显示查询到的结果 if (myDs.Tables[0].Rows.Count > 0) { GridView1.DataSource = myDs; GridView1.DataBind(); } else { Response.Write("<script>alert('查询结果为空')</script>"); } myDa.Dispose();