Use LINQtoSQL to copy data from one database to another

Source: Internet
Author: User
Author: barefoot thinking time: 83020105: 04: 58PM about database access technology. Generally, the database study may be more about Northwind. Haha! At least, I often make such a sample database. Even so, the understanding of this database is quite superficial. Or, just recognize it by yourself

Author: barefoot thinking time: 5:04:58 there is a database access technology, usually used to study the database may be more than Northwind. Haha! At least, I often make such a sample database. Even so, the understanding of this database is quite superficial. Or, just recognize it by yourself

Author: barefoot thinking
Time: 8/30/2010 5:04:58

The database access technology is generally used to study databases. It may be more about Northwind. Haha! At least, I often make such a sample database. Even so, the understanding of this database is quite superficial. Or, I think it is not necessary to thoroughly read this database. ^_^ I'm afraid it's because of this superficial understanding that at last such a code was generated!
The purpose of this Code is to study the many-to-many ing relationships of the LINQ to SQL statements. Because you are not familiar with the Northwind database, you do not know which data tables have such a many-to-many relationship. As a result, I tried to prepare such a test database. Otherwise, how will I verify my speculation? Well, this kind of necessary preparation still needs to be done, although it takes less time. It would be even better if you could just start with the topic of the test argument.
Let's talk about database definition first! In fact, it is also very simple. The data model I want is as follows: there is a data table that stores customer information (MERS mers), a data table that stores product information (Products ), and mersproducts ). With these three data tables, it is enough for me to test and use them. The relationship between them is also very clear, a customer can order any number of products, and a product can be ordered by any number of customers. Therefore, there is a many-to-many relationship between the customer and the product, and the relationship is established through the mersproducts join table. It's so simple! Click here!
Is the definition of a data table (MERS mers) that stores customer information:

Is the definition of the data table (Products) that stores product information:


It is the definition of the mersproducts table used to connect the customer table and the product table:


Is it easy? Is it silly? Well, you just need to do it! This is only the data table used for testing. It is not too strict! Pai_^

The definition of the data table is ready, and the data is prepared. Haha, I thought I was not too stupid. At least I wouldn't be too stupid to manually add such data one by one. If this is the case, you should pay a terrible price for your stupidity. At last, I finally couldn't help calling "Niang !" Ah, this world.
Where does the data come from? The first thing that comes to mind is the Northwind database. In fact, it already contains the customer's data table (MERS mers) and the data table (Products) of the storage product ). You only need to read the data and put it in the test data table. If you look at the definition of the database I have previously made, you will find that, in fact, I just moved the definition in Northwind. ^_^ If you can be lazy, you will be lazy! Why should we kill our brain cells for no reason?
In this way, there is a task to copy data from one database to another. Although there are still many ways to achieve it, of course, I know little about it. In addition, I recently have a special liking for the use of LINQ. Even the use of LINQ to SQL is also a topic of great interest to me. Therefore, we plan to use LINQ to SQL to complete such a data replication task.
Simply put, this means that one reads data from the database and then saves the data to another database. However, if we want to implement this using LINQ to SQL, you cannot have only one object model. Because two databases are involved, at least two object models are required, only two object models are required. One is the object model of the Northwind database, and the other is the object model of the test database. The main purpose of the previous object model is to read data, and the main task of the next object model is to add data.
First of all, I have to declare that in this case, I will not create such two object models manually. It is unwise to do that! At this time, it is natural that the convenience provided by the object relation designer of VS will not be spared. Two DBML files were created in one breath, and some operations were performed using the object relationship designer. Finally, I thought it was not ideal. I simply went straight to the ground and thoroughly performed a internal medicine operation on the DBML file. After this operation, the DBML file suddenly understood my coding rules and it felt much better. Let me release three in one breath!
Is the project file in Solution Explorer:

The Northwind. dbml file is used to establish the object model of the Northwind Database, while the Database. dbml file is used to establish the object model of the test Database. Let's also look at what they look like in the object link designer!
Is the posture of Northwind. dbml in the object link designer:



Ah! It's a pity that they are not holding hands when they are so close to each other! -_-
It is the shape of Database. dbml in object Link Design:


However, this picture expresses the naked real triangular relationship in the sky! ^_^ Haha, even the object relation designer of VS understands this!
So much nonsense.
Finally, let's release the code. Let's take a look at the rest!

Static void Main (string [] args) {NorthwindDataContext ndc = new NorthwindDataContext (); DatabaseDataContext ddc = new DatabaseDataContext (); //************************************** * ***************************** // convert the Cutomers data table from the Northwind database all customers, and save it to the test database. //************************************** * ****************************** Var AllCustomersInNorthwind = from CustomerObject in ndc. customers select CustomerObject; foreach (var CustomerObject in AllCustomersInNorthwind) {Customer NewCustomer = new Customer () {CustomerID = CustomerObject. customerID, ContactName = CustomerObject. contactName, ContactTitle = CustomerObject. contactTitle, Country = Cu StomerObject. country, City = CustomerObject. city, Address = CustomerObject. address, Region = CustomerObject. region}; ddc. MERs. insertOnSubmit (NewCustomer);} ddc. submitChanges (); //************************************** * ***************************** // set the Products data table from the Northwind database all customers, and save it to the test database. //************************************** * ****************************** Var AllProductsInNorthwind = from ProductObject in ndc. products select ProductObject; foreach (var ProductObject in AllProductsInNorthwind) {Product NewProduct = new Product () {ProductName = ProductObject. productName, UnitPrice = ProductObject. unitPrice, UnitsInStock = ProductObject. unitsInStock, UnitsOnOrder = ProductObje Ct. unitsOnOrder}; ddc. products. insertOnSubmit (NewProduct);} ddc. submitChanges (); //************************************** * ****************************** // automatically based on the data in the Test Database generate the customer's product order information. //************************************** * ****************************** Var AllCustomers = from CustomerObject in ddc. customers select CustomerObject; var AllProducts = from ProductObject in ddc. products select ProductObject; Random RandomObject = new Random (); foreach (var CustomerObject in AllCustomers) {int SkipCount = RandomObject. next (0, AllCustomers. count ()-1); int TakeCount = RandomObject. next (1, AllCustomers. count ()-SkipCount); var RandomProducts = AllProducts. skip (SkipCount ). take (TakeCount); foreach (var ProductObject in RandomProducts) {CustomerProduct NewCustomerProduct = new CustomerProduct () {CustomerID = CustomerObject. customerID, ProductID = ProductObject. productID, UnitPrice = ProductObject. unitPrice}; ddc. customersProducts. insertOnSubmit (NewCustomerProduct) ;}} ddc. submitChanges ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.