1. Preparation work, we need to install some things and copy some things.
0. Open VS2008 or VS2010, create a new engineering console project LinqMySQL, and the target assembly is .NET 3.5
1. Download and install MySQL all the way to NEXT
2. Download and install MySQLWorkBench all the way to NEXT, open it, and create the test database, Customers and Orders tables.
-------------------------------------------------- ---
create table Customers
(
CustomerID varchar(50) not null PRimary key,
ContractName varchar(50) not null,
Phone varchar(50) not null,
City varchar(50) not null,
Country varchar(50) not null
)
create table Orders
(
OrderID int not null primary key,
OrderDate DateTime,
CustomerId varchar(50) not null,
foreign key(CustomerID) references Customers(CustomerID)
)
-------------------------------------------------- ---
3. Download and NEXT install MySQL .NET Connector 6.3.2
4. Download and unzip DbLinq, and set the path to the system environment variable
5. Create a new LinqDlls folder in the LinqMySql directory
Open the folder C:Program FilesReference AssembliesMicrosoftFrameworkv3.5
Copy the following Dlls to the LinqDlls folder
-------------------------------------------------- ---
System.Core.dll
System.Data.DataSetExtensions.dll
System.Data.Linq.dll
System.Runtime.Serialization.dll
System.xml.Linq.dll
-------------------------------------------------- ---
6. Go to the MySQLMySQL Connector Net 6.3.2Assembliesv2.0 directory and copy
-------------------------------------------------- ---
MySql.Data.dll
-------------------------------------------------- ---
Go to the DbLinq installation directory
2. Let’s start working, let’s do it step by step.
1. Open CMD, CD to the DbLinq directory and use DbMetal.exe to create the MySQL DataContext. The command is as follows:
-------------------------------------------------- ---
DbMetal.exe
-provider=MySql
-database:MyDatebase
-server:localhost
-user:mysqluser
-passWord:yourpassword
-namespace:LinqMySql
-code:TestDataContext.cs
-sprocs
-------------------------------------------------- ---
Cut the generated TestDataContext.cs to the LinqMySql project root directory and add it to the LinqMySql project
2. Add the following code in the Main function
-------------------------------------------------- ---
string connStr = @"server=localhost;database=test;user=mysqluser;pwd=yourpassword;";
using (var conn = new MySqlConnection(connStr)){ var t = new Test(conn); var data = from customer in t.Customers select customer.ContractName; foreach (var d in data) { Console.WriteLine(d); }}------------------------------------------------ -----3. Compile and run, successful.
4. Next we need to degrade .NET 3.5 to .NET2.0. First, change the target assembly of the project to .NET2.0.
Then you will find that several dlls in the Reference become exclamation marks, delete them all, and then change the dlls in the LinqDlls folder
Add several Dlls to Referece, and then set copy local to True and Specify Version to False in the properties of these Dlls.
5. Compile again and it will be successful!
3. What about other stories? Trying to use LinqDataSet under .NET2.0 and the database uses MySQL
1. Restart VS so that the MySQL connector just added is loaded by VS.
2. Open Server Explorer, right-click Data Connection, add new link, and change data connector
Select MySql Connector, enter the necessary server address, username, and password all the way to NEXT, and test whether the connection is successful.
3. Change the LinqMySql target assembly to .NET3.5, right-click the project and add a strongly typed DataSet.
Add the Customers and Orders tables under the Mysql test database in Server Explorer to the DataSet.
4. Write test code in the Main function
-------------------------------------------------- ---
using (var dataSet = new DataSet1()){ var customerAdapter = new DataSet1TableAdapters.CustomersTableAdapter(); var orderAdapter = new DataSet1TableAdapters.OrdersTableAdapter(); customerAdapter.Fill(dataSet.Customers); orderAdapter.Fill(dataSet.Orders); var query = from order in dataSet.Orders where order.OrderDate.Date > new DateTime(2010, 8, 1) select new { order.OrderID, order.CustomerID }; foreach (var order in query) { Console.WriteLine(order. CustomerID); }}------------------------------------------------ --------5. Compile and run, success!
6. Wait, it’s only .NET3.5 now, how to change to .NET2.0?
Try to directly change the target assembly to .NET2.0, compile, and fail!
7.TMD checked DataSet1.designer.cs and found the following code under .NET3.5
-------------------------------------------------- ---public partial class customersDataTable : global::System.Data.TypedTableBase<customersRow>---------------------------------- -------------------------- Now it has become
-------------------------------------------------- ---
public partial class customersDataTable : global::System.Data.DataTable, global::System.Collections.IEnumerable------------------------------ --------------------------8.So? what to do? Reset the target assembly to .NET3.5, and then go to the LinqMySql directory to
-------------------------------------------------- ---
DataSet1.Designer.cs
DataSet1.xsc
DataSet1.xsd
DataSet1.xss
-------------------------------------------------- ----
Copy a few files to make a backup, and then set the target assembly to .NET2.0 again, and then. .
Then copy the above files you just backed up back to the project directory, compile and run again, success!
9. At this point, we have successfully used LinqSQL and LinqDataSet to use MySql under .NET2.0, but
I have not successfully used LinqEntity to use MySql under .NET2.0. Let’s talk about it later.