Generate C # entity Class Based on SQLServer data table

Source: Internet
Author: User

The entity tools used to generate tables help reduce the amount of code and speed up development.

First look

The first figure shows how to connect to the database based on the username and password of the entered connection server address. If an error occurs, the connection fails.

The second figure shows the tables created by all users in the current database.

Then, the system table related to the table information in the database is obtained based on the selected table name. Then, the field type is converted to the C # data type, and the TextBox is output.

Several tables are used here.

Select * from sys. extended_properties table comment and column comment, column arrangement order (without column name)

Select * from sys. columns detailed information field types of all columns in the database table, table fields, names, etc.

Select * from sysobjects where xtype = 'U' -- ID of the table column created by all users

Select * from policypes -- all types of SQL SERVER databases

Select * from syscolumns all field names and lengths

The following uses Ts_Customers as an example to obtain the column name, column type, table comment, and column comment of this table.

Select syscolumns. name, policypes. name as type
, (Select isnull (value, '') from sys. extended_properties ex_p where ex_p.minor_id = 0

And ex_p.major_id = sysobjects. id) as TableDemo
, Ext. value as colName from syscolumns
Inner join sysobjects ON syscolumns. id = sysobjects. id
Inner join policypes ON syscolumns. xtype = policypes. xtype
INNER join sys. extended_properties as ext on ext. major_id = sysobjects. id
And ext. minor_id = syscolumns. colorder
WHERE (sysobjects. name = 'ts _ customer') AND (policypes. name <> 'sysname ')

The effect is as follows:

After the result is obtained, it is written to TextBox cyclically. The code is not pasted here. It is very simple.

Basically achieve the desired effect. unfortunately, I want to dynamically keep the database connection to the app after the test passes. config and then read from the configuration file. the application is being executed. you can only read from the configuration file the next time. so I didn't do this.

 

Source code download

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.