SQL SERVER2005数据库,VS2008,添加一个BUTTON控件,一个TEXTBOX控件,一个DATA GRIVEW控件。
在TEXTBOX中输入查询条件,点击BUTTON,如果查询条件在数据库中某表中存在,则在DATAGRIVEW中显示该条件对应列的内容...
在TEXTBOX中输入查询条件,点击BUTTON,如果查询条件在数据库中某表中存在,则在DATAGRIVEW中显示该条件对应列的内容
展开
2个回答
展开全部
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();
}
{
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询