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

        }

    }

 

转载于:https://www.cnblogs.com/ngnetboy/archive/2012/03/11/2390127.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET 中的 GridView 控件可以与 SQL Server 数据库进行连接,实现数据的展示和操作。以下是连接 GridViewSQL Server 数据库的步骤: 1. 在 ASP.NET 网页中,首先需要在代码前导处引入 System.Data.SqlClient 命名空间,以便可以使用SQL Server 数据库相关的类和方法。 2. 在 ASP.NET 网页中将 GridView 控件添加到页面上,并设置其属性。例如,设置 AutoGenerateColumns 属性为 true,可以自动根据数据源生成列;设置 DataSourceID 属性为数据源的 ID。 3. 创建连接字符串,用于连接SQL Server 数据库连接字符串中包含数据库的服务器名称、身份验证方式、数据库名称等信息。可以使用 Configuration Manager 类的 ConnectionStrings 属性来获取连接字符串。 4. 在代码中创建 SqlConnection 对象,并将连接字符串作为参数传递给 SqlConnection 构造函数。然后,打开连接,并创建 SQL 查询或存储过程。 5. 创建 SqlCommand 对象,用于执行 SQL 查询或存储过程。将 SqlCommand 对象的 CommandText 设置为 SQL 查询或存储过程的名称。 6. 创建 SqlDataAdapter 对象,并将 SqlCommand 对象作为参数传递给 SqlDataAdapter 构造函数。然后,使用 SqlDataAdapter 的 Fill 方法将结果集填充到 DataSet 或 DataTable 中。 7. 将 DataSet 或 DataTable 对象设置为 GridView 控件的数据源。可以使用 GridView 的 DataSource 属性绑定到 DataSet 或 DataTable,然后调用 GridView 的 DataBind 方法进行数据绑定。 8. 最后,关闭数据库连接,释放资源。可以使用 SqlConnection 对象的 Close 方法来关闭连接。 通过以上步骤,我们可以实现 ASP.NET GridView 控件SQL Server 数据库连接和数据展示。关键是要正确设置连接字符串、创建 SqlConnectionSqlCommand 和 SqlDataAdapter 对象,并使用合适的数据源进行绑定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值