SQL SERVER2005数据库,VS2008,添加一个BUTTON控件,一个TEXTBOX控件,一个DATA GRIVEW控件。

在TEXTBOX中输入查询条件,点击BUTTON,如果查询条件在数据库中某表中存在,则在DATAGRIVEW中显示该条件对应列的内容

protected void Button3_Click(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select * from userInfo where userId='" + txtSelect.Text + "'", strConString);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}追问

添加和删除也发我看看,谢谢了,
strConString是随便写的,还是有什么意义啊。

追答

static string strConString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
是连接字符串

添加:

protected void Button2_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(strConString))
{
con.Open();
string sql = "insert into userInfo values(@userName)";
SqlCommand com = new SqlCommand(sql, con);
com.Parameters.Add("@userName", txtUserName.Text.Trim());
com.ExecuteNonQuery();
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('添加成功!');location.href='Default6.aspx'", true);
}
}

删除:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
using (SqlConnection con = new SqlConnection(strConString))
{
con.Open();
string sql = "delete from userInfo where userId='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand com = new SqlCommand(sql, con);
com.ExecuteNonQuery();
ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功!');location.href='Default6.aspx'", true);
}
}

追问

那个删除我也是要写在一个BUTTON的方法里的

追答

这个的话不好做哦,一般是在GridView1_RowDeleting这个事件里面做,或者在rowcommand事件里面做

追问

哦,那谢谢了。果断把分给你先、

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-03-24
说的很清楚啊, 问题是什么呢? 不会拉控件, 不会连接数据库,还是不会把数据在gridview中显示出来?
相似回答
大家正在搜