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();
}
}