使用gridview控件实现操作问题。首先从数据库中高出数据使其显示在gridview上。。
string str = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(str))
{
using (SqlCommand sqlcmm = sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "select * from gonggao";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}
}
然后进行对它的增加与其增加
protected void Button1_Click(object sender, EventArgs e)
{
int i = 0;
string str = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(str))
{
using (SqlCommand sqlcmm = sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "insert into gonggao values(@themes,@name,@date)";
sqlcmm.Parameters.AddWithValue("@themes", this.txtthemes.Text);
sqlcmm.Parameters.AddWithValue("@name", this.txtname.Text);
sqlcmm.Parameters.AddWithValue("@date", this.txtdate.Text);
sqlcnn.Open();
i= sqlcmm.ExecuteNonQuery();
}
}
if (i == 1)
{
ClientScript.RegisterStartupScript(GetType(),"t","<javascript>alert('ok')</javascript>");
}
else
Response.Write("<script>alert('插?入¨?失º¡ì败㨹!ê?');</script>");
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("企¨®业°¦Ì公?告?.aspx");
}
查询:
string search_name = this.txtchakan.Text;
int leixing = Convert.ToInt32(this.DropDownList1.SelectedValue);
string sql = "select * from gonggao where themes like '%" + search_name + "%'";
DataTable dt = sqlhelp.ExecuteTable(sql);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
使用超链接的方法,点击所需项目的内空在另一个页面显示出来用到了datanavigateurlfields与datanavigateurlformatstring
string str = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(str))
{
using (SqlCommand sqlcmm = sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "select * from gonggao";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}
}
然后进行对它的增加与其增加
protected void Button1_Click(object sender, EventArgs e)
{
int i = 0;
string str = ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(str))
{
using (SqlCommand sqlcmm = sqlcnn.CreateCommand())
{
sqlcmm.CommandText = "insert into gonggao values(@themes,@name,@date)";
sqlcmm.Parameters.AddWithValue("@themes", this.txtthemes.Text);
sqlcmm.Parameters.AddWithValue("@name", this.txtname.Text);
sqlcmm.Parameters.AddWithValue("@date", this.txtdate.Text);
sqlcnn.Open();
i= sqlcmm.ExecuteNonQuery();
}
}
if (i == 1)
{
ClientScript.RegisterStartupScript(GetType(),"t","<javascript>alert('ok')</javascript>");
}
else
Response.Write("<script>alert('插?入¨?失º¡ì败㨹!ê?');</script>");
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("企¨®业°¦Ì公?告?.aspx");
}
查询:
string search_name = this.txtchakan.Text;
int leixing = Convert.ToInt32(this.DropDownList1.SelectedValue);
string sql = "select * from gonggao where themes like '%" + search_name + "%'";
DataTable dt = sqlhelp.ExecuteTable(sql);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
使用超链接的方法,点击所需项目的内空在另一个页面显示出来用到了datanavigateurlfields与datanavigateurlformatstring