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

        }

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM