【ASP.NET】用GridView控件连接SQL Server数据库
1 在新建的网站中添加一个Web窗体(默认窗体为default.aspx);
2 双击“对象资源管理器”中的Web.config对象,在该对象的设计代码窗口中添加如下代码,实现设置连接SQL Server数据库字符串的功能。
<connectionStrings><!--连接数据库-->
<add name="connection" connectionString="data source=.;integrated security=SSPI;initial catalog=newsfabu"></add>
</connectionStrings>
3 双击Default.aspx在设计的界面中添加一个GridView控件。
4 双击Default.aspx.cs在代码区添加如下代码。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind(); //调用绑定数据信息函数
}
}
public void GridViewBind()
{
this.Title = "新闻发布中心";
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();
}
附:ASP连接数据库,并实现增删改的功能:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind(); //调用绑定数据信息函数
}
}
public void GridViewBind()
{
this.Title = "新闻发布中心";
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);
cmd.Parameters["@_leibie"].Value = TextBox2.Text.Trim();
cmd.Parameters.Add("@_timu", SqlDbType.VarChar);
cmd.Parameters["@_timu"].Value = TextBox3.Text.Trim();
cmd.Parameters.Add("@_neirong", SqlDbType.VarChar);
cmd.Parameters["@_neirong"].Value = TextBox4.Text.Trim();
con.Open();
cmd.ExecuteNonQuery();
GridViewBind();
}
}
protected void 删除新闻_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
this.Page.RegisterStartupScript("ss", "<script>alert('请输入你想删除的编号')</script>");
}
else
{
SqlConnection con = new SqlConnection(ConfigurationManager.
ConnectionStrings["connection"].ConnectionString);
string delete = "delete news from news where 新闻编号='" + TextBox1.Text + "'";
SqlCommand com = new SqlCommand(delete, con);
con.Open();
com.ExecuteNonQuery();
con.Close();
GridViewBind();
}
}
protected void 修改新闻_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "")
{
this.Page.RegisterStartupScript("ss", "<script>alert('请输入你要删除的编号')</script>");
}
else
{
SqlConnection con = new SqlConnection(ConfigurationManager.
ConnectionStrings["connection"].ConnectionString);
string update = "update news set 新闻类别='" + TextBox2.Text + "',
新闻题目='" + TextBox3.Text + "',新闻内容='" +
TextBox4.Text + "' where 新闻编号='" + TextBox1.Text + "'";
SqlCommand cmd = new SqlCommand(update, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridViewBind();
}
}