2014-01-16 30 views
-1

我對asp.net C#中的2種方法:變化asp.net功能從MySQL到SQL Server

DataTable dt; 
     protected void Page_Load(object sender, EventArgs e) 
     {    

        //Fetch data from mysql database 
        MySqlConnection conn = new MySqlConnection("server=localhost;uid=root; 
         password=priya123;database=world;pooling=false;"); 
        conn.Open(); 
        string cmd = "select * from country limit 7"; 
        MySqlDataAdapter dAdapter = new MySqlDataAdapter(cmd, conn); 
        DataSet ds = new DataSet(); 
        dAdapter.Fill(ds); 
        dt=ds.Tables[0]; 
        //Bind the fetched data to gridview 
        GridView1.DataSource = dt; 
        GridView1.DataBind(); 

     } 

     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 
      if(e.CommandName.Equals("detail")) 
      { 
       int index = Convert.ToInt32(e.CommandArgument); 
       string code = GridView1.DataKeys[index].Value.ToString(); 

        IEnumerable<DataRow> query = from i in dt.AsEnumerable() 
             where i.Field<String>("Code").Equals(code) 
             select i; 
        DataTable detailTable = query.CopyToDataTable<DataRow>(); 
        DetailsView1.DataSource = detailTable; 
        DetailsView1.DataBind(); 

      } 
     } 

現在我想用SQL SERVER。我使用DBML創建了一個LINQ ..並使用DataContext來創建一個名爲'sdb'的對象。我嘗試使用Lambda表達式進行更改,但出現錯誤,即從lambda中選擇的行不是'IEnumerable'。 我該如何將其轉換爲SQL SERVER對象?

謝謝!

+0

爲什麼不只是改變了'MYSQL *'ADO.NET對象'SQL *'? –

+0

是的,我想要的。但我應該改變他們哪一個? – user3167150

回答

0

你只需要從MySql*改變Sql*,每一個ADO.NET對象:

DataTable dt; 
protected void Page_Load(object sender, EventArgs e) 
{    
    //Fetch data from mysql database 
    SqlConnection conn = new SqlConnection("server=localhost;uid=root; 
     password=priya123;database=world;pooling=false;"); 
    conn.Open(); 
    string cmd = "select * from country limit 7"; 
    SqlDataAdapter dAdapter = new SqlDataAdapter(cmd, conn); 
    DataSet ds = new DataSet(); 
    dAdapter.Fill(ds); 
    dt=ds.Tables[0]; 
    //Bind the fetched data to gridview 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
} 

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if(e.CommandName.Equals("detail")) 
    { 
     int index = Convert.ToInt32(e.CommandArgument); 
     string code = GridView1.DataKeys[index].Value.ToString(); 

     IEnumerable<DataRow> query = from i in dt.AsEnumerable() 
          where i.Field<String>("Code").Equals(code) 
          select i; 
     DataTable detailTable = query.CopyToDataTable<DataRow>(); 
     DetailsView1.DataSource = detailTable; 
     DetailsView1.DataBind(); 
    } 
}