Linq to SQL 根据自己需要更改数据源

如何动态改变Linq to SQL的数据源?

1. DBLinqDataContext 是UI自动建立的类, MyDataContext是我的子类, myConnectionString from web.config

 public class MyDataContext : DBLinqDataContext

    {
        private static class ConnectionParams
        {
            public static String ConnectionString
            {
                get
                {
                    return ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
                }
            }
        }
        public MyDataContext(): base(ConnectionParams.ConnectionString)
        {
        }

    }

 2. how to use :

using (MyDataContext ctx = new MyDataContext())
            { //change according to your situation
               /* Table<Customer> CustomerDataTable;
                CustomerDataTable= ctx.Customers;
                DataLinq.Customer dd = ctx.Customers.First();

                gvCustomer.DataSource = (from tbl in CustomerDataTable
                                         select new
                                         {
                                             tbl.CustomerID,
                                             tbl.CompanyName,
                                             tbl.ContactName,
                                             tbl.ContactTitle,
                                             tbl.Address
                                         });
                gvCustomer.DataBind();*/

            }

Give it a try!

原文地址:https://www.cnblogs.com/simonhaninmelbourne/p/1496190.html