Create a database from the dbml file in LINQ to SQL

Source: Internet
Author: User

Today, I am very depressed. I have no backups of servers in the lab, And my database is where I am. Fortunately, I am using LINQ to SQL. think about it. His dbml file basically contains all the contents of the database. Therefore, there must be a way to create a database from the dbml file!

After studying for a while, I finally rebuilt the database. Even though the data was lost, it was all test data and basically useless. now I want to share them with you. In addition, never forget to back up the database ~~ Blood lessons! Observe the designer. CS file automatically generated by IDE and view the file structure as follows:

[Table (name = "DBO. tb_borrow ")] <br/> Public partial class tb_borrow: inotifypropertychanging, inotifypropertychanged <br/>{</P> <p> Private Static propertychangingeventargs attributes = new propertychangingeventargs (string. empty); </P> <p> private system. nullable <int> _ bookid; </P> <p> private system. nullable <int> _ userid; <br/> [column (storage = "_ bookid", dbtype = "int")] <br/> public system. nullabl E <int> bookid <br/>{< br/> Get <br/>{< br/> return this. _ bookid; <br/>}< br/> set <br/>{< br/> If (this. _ bookid! = Value) <br/>{< br/> This. onbookidchanging (value); <br/> This. sendpropertychanging (); <br/> This. _ bookid = value; <br/> This. sendpropertychanged ("bookid"); <br/> This. onbookidchanged (); <br/>}</P> <p> [column (storage = "_ userid ", dbtype = "int")] <br/> public system. nullable <int> userid <br/> {<br/> Get <br/> {<br/> return this. _ userid; <br/>}< br/> set <br/>{< br/> If (this. _ userid! = Value) <br/>{< br/> This. onuseridchanging (value); <br/> This. sendpropertychanging (); <br/> This. _ userid = value; <br/> This. sendpropertychanged ("userid"); <br/> This. onuseridchanged (); <br/>}< br/>

(To save space, only a few attributes are listed here)

It can be found that each class is modified by the table attribute, and each attribute is modified by the culumn. In fact, this attribute corresponds to the columnattribute class, this class contains the field name of the corresponding field and the field type in the database. we use it to create databases and data tables.

Next, you can easily restore data tables by writing the following methods:

 

Private void reversedatatable () <br/>{// create a data table in the database <br/> string MySQL = ""; <br/> MySQL = gettableinfo (); <br/> sqlconnection myconnection = new sqlconnection ("Data Source = .; database = db_libmanager; Integrated Security = sspi "); <br/> sqlcommand mycommand = new sqlcommand (MySQL, myconnection ); <br/> try <br/> {<br/> mycommand. connection. open (); <br/> mycommand. executenonquery (); <br/> MessageBox. Show ("data table created in db_libmanager database succeeded", "message prompt", messageboxbuttons. OK, messageboxicon. information); <br/>}< br/> catch (exception ex) <br/>{< br/> MessageBox. show (ex. message, "message prompt", messageboxbuttons. OK, messageboxicon. information); <br/>}< br/> finally <br/>{< br/> myconnection. close (); <br/>}< br/> private string gettableinfo () <br/>{< br/> string MySQL = <br/> "create table tb_user ("; <Br/> tb_user testclass = new tb_user (); <br/> type = testclass. getType (); <br/> // traverses all attributes of the tb_user class <br/> foreach (system. reflection. propertyinfo minfo in type. getproperties () {<br/> // traverses all the custom attributes of this attribute. This sentence is a bit round. You can see that the reflection knowledge is not so round ~~ <Br/> foreach (attribute ATTR in <br/> attribute. getcustomattributes (minfo) {<br/> // generate an SQL statement based on custom attributes ~ This is the key. Take a look at <br/> If (ATTR. getType () = typeof (columnattribute) <br/>{< br/> If (columnattribute) ATTR ). dbtype. contains ("null") <br/> {<br/> MySQL + = "[" + minfo. name + "]" + (columnattribute) ATTR ). dbtype + ","; <br/>}< br/> else <br/>{< br/> MySQL + = "[" + minfo. name + "]" + (columnattribute) ATTR ). dbtype + "null,"; <br/>}</P> <p >}< br/> MySQL = MySQL. substring (0, MySQL. length-1); // remove the last comma <br/> MySQL + = ")"; // Add a backslash after the SQL statement, in this way, a complete statement is formed. <br/> MessageBox. show (MySQL); <br/> return MySQL; </P> <p>}

Euler, restore ** to this end, hoping to be useful to those who need it ~~

 

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.