2018年6月18日 星期一

C# Connection String & DataAdapter Fill

  以前的紀錄不見了,只好重新紀錄一次常用的Connection String .

在Web.config 中 新增 Connectionstring
----------------------------------------------------------------------------------------------------------------------

  <connectionStrings> 
    <add name="ConnectionString1" connectionString="Data Source=DatabaseName;Initial Catalog=AdventureWorksLT2008;Persist Security Info=True;User ID=xx;Password=xxxxxxxxx" providerName="System.Data.SqlClient"/> 
  </connectionStrings> 
 
----------------------------------------------------------------------------------------------------------------------
 在aspx中新增telerik GridView
---------------------------------------------------

   <div>

        <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Skin="MetroTouch"></telerik:RadGrid>

        <asp:Label ID="Label1" runat="server" Text="Label"/>

    </div>

----------------------------------------------------------------------------------------------------------------------
aspx.cs中 新增 DataBind 的Function ,並在頁面啟動時就呼叫
---------------------------------------------------

protected void Page_Load(object sender, EventArgs e)

    {

        //呼叫Function

        DataBind_GridView();

    }

----------------------------------------------------------------------------------------------------------------------
Function 
---------------------------------------------------

    // Function Start

    private void DataBind_GridView() {

       //Connection String

        string Connection1 = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;

        string selectDB = "SELECT * FROM SalesLT.Customer";     

    

        SqlConnection db = new SqlConnection(Connection1); //new Sql 連線

        SqlCommand cmd = new SqlCommand(selectDB, db); //Sql Command

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        RadGrid1.DataSource = ds;

    }





----------------------------------------------------------------------------------------------------------------------

沒有留言:

張貼留言