使用sql server中的表绑定GridView

时间:2014-03-12 13:50:50

标签: c# sql sql-server gridview data-binding

我想将我的网格视图与来自sql server的表绑定,但是没有得到正确的解决方案。 与我分享它是如何完成的 我的应用程序是一个测试应用程序

1 个答案:

答案 0 :(得分:0)

私有字符串ConnectionString {get;组; } //绑定你的连接字符串

private SqlCommand cmd;

私有SqlConnection con;

私有DataSet ds;

DataSet GetTableData()     {

    try

    {

        using (con = new SqlConnection(ConnectionString)) 

        {

            cmd = new SqlCommand("select * from <table-name> ", con);

            ds = new DataSet();


            var data = new SqlDataAdapter(cmd);

            data.Fill(ds);

        }

    }

    catch (Exception ex)

    {

        //Error Logic

    }

    return ds;

}

从上面的函数返回值后,绑定到gridview

[GridviewID] .dataSource = GetTableData();

[GridviewID] .dataBind();

相关问题