[Go] Extract database tables and fields as classes in C # based on SQL scripts

Source: Internet
Author: User

When developing, it is not necessary to use SQL to directly interact with the database, when the table names and field names in the database are used more. If you copy one every time you use it, the egg hurts. So consider making it a const constant. But there are quite a few tables and fields in the database, one knocking, not only tired, but also possibly knocking the wrong. The best way to ensure that you are right is to use tools or scripts, of course.

A SQL script implementation is provided here.

Principle: Get the table---of the database > traverse the fields in each table---> Generate data

SQL code

DECLARE @tableName VARCHAR (MAX)
DECLARE @tableCount int
DECLARE @tableIndex int
DECLARE @tempTable TABLE
(
DATA VARCHAR (MAX)
)

SELECT @tableCount =count (*) from SysObjects WHERE xtype= ' U '
--xtype= ' U ': represents all user tables;
--xtype= ' S ': denotes all system tables;
SET @tableIndex =1

While @tableIndex <[email protected]

BEGIN
SELECT @tableName =name
From
(
SELECT row_number () over (ORDER by NAME) as Tempno,name from SysObjects
WHERE xtype= ' U '
) TEMP
WHERE [email protected]

INSERT into @tempTable VALUES (")

INSERT into @tempTable VALUES (' #region ' [email protected])

INSERT into @tempTable VALUES (' Public sealed class ' [email protected]+ ' {')

INSERT into @tempTable the Select ' Public const string ' +name+ ' = ' +name+ ' "; ' From syscolumns WHERE ID in
(SELECT ID from sysobjects WHERE [email protected])

INSERT into @tempTable VALUES ('} ')

INSERT into @tempTable VALUES (' #endregion ')

INSERT into @tempTable VALUES (")

SET @tableIndex +=1
END

SELECT * from @tempTable

Results of the Build

result graphs in SQL

Result graphs in C #

Specific adjustments can be made as needed

[Go] Extract database tables and fields as classes in C # based on SQL scripts

Related Article

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.