ASP 用GridView控件连接SQL Server数据库(2)
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.
ConnectionStrings["connection"].ConnectionString);
sqlcon.Open();
SqlDataAdapter adsa = new SqlDataAdapter("select * from news", sqlcon);
DataSet ds = new DataSet();
adsa.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
sqlcon.Close();
}
protected void 添加新闻_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "" || TextBox2.Text == "")
{
this.Page.RegisterStartupScript("ss", "<script>alert('请输入编号')</script>");
}
else
{
SqlConnection con = new SqlConnection(ConfigurationManager.
ConnectionStrings["connection"].ConnectionString);
string insert = "insert into news(新闻编号,新闻类别,新闻题目,新闻内容)
values(@_id,@_leibie,@_timu,@_neirong)";
SqlCommand cmd=new SqlCommand (insert,con);
cmd.Parameters.Add("@_id", SqlDbType.Int);
cmd.Parameters["@_id"].Value = TextBox1.Text.Trim();
cmd.Parameters.Add("@_leibie", SqlDbType.VarChar);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>