簡體   English   中英

從Web.config文件中的SQL連接字符串創建DataTable

[英]Create DataTable from SQL Connection String in Web.config file

我需要在網頁上的SQL Server數據庫中顯示表中的數據。 我的Web.config文件中有一個連接字符串,如下所示:

<connectionStrings>
<add name="Products.ConnectionString" 
     connectionString="Data Source=...."
     providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
</system.web>

在我的aspx中,我有一個帶有ObjectDataSource的GridView來顯示數據。 在后面的代碼中,我根據列表創建了一個方法來從數據庫表中返回值。 但是,現在我被告知用戶需要能夠過濾數據,因此我創建了一個文本框和一個按鈕來啟用此功能,但是在這種情況下,我意識到最好有一個DataTable而不是List。 雖然我一直將List用於此類項目,但是我不確定如何在DataTable中實現相同功能。 這是我的列表代碼的樣子:

 public class Products
{

    public string Name { get; set;} 
    public int Price { get; set; }



    public List<Products> DataTable()
    {
        List<Products> myList = new List<Products>();

        string sqlQuery = "SELECT * FROM [Products_Table] ";
        string connectionString = ConfigurationManager.ConnectionStrings["Products_ConnectionString"].ConnectionString; //Read connection string from config file


        using (var con = new SqlConnection(connectionString))
        {
            using (var cmd = new SqlCommand(sqlQuery, con))
            {

                con.Open(); //Open connection
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Products t = new Products();
                        t.Name = reader["Name"].ToString();
                        t.Price = Convert.ToInt32(reader["Price"]);


                        myList.Add(t);
                    }
                }
            }
        }
        return myList;

    }
}

因此,如果有人可以讓我正確地用DataTable替換列表,那將是很棒的。

就像是 -

GridView1.DataSource = DataTable();
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString);
    string SQL = "SELECT * FROM [Table];";
    SqlDataAdapter adapter = new SqlDataAdapter(SQL, conn);
    DataTable dt = new DataTable();
    adapter.Fill(dt);

    GridView1.DataSource = dt;
    GridView1.DataBind();

我不知道那是您要尋找的東西,但這是將GridView與DataTable綁定的方式。

在后面的代碼中,您需要執行以下操作:

public static class ProductsDataSource
 {
public static DataTable LoadProducts()
 {
    using (SqlConnection conn = new   SqlConnection(ConfigurationManager.ConnectionStrings["Products_ConnectionString"].ConnectionString))
    using (SqlCommand command = new SqlCommand("SELECT * FROM Products_Table", conn))
    {
        DataTable data = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(data);
        return data;
    }
  }
}

由於您使用的是ObjectData Source,因此請確保正確定義SelectMethod和Typename以便返回數據。 像這樣:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="ProductsDataSource"
SelectMethod="LoadProducts" 
/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM