Create C# POCO Class Wrapper from SQL table or view

Taken from the following post:

http://www.sqlservercentral.com/scripts/Development/61477/

 

CREATE PROCEDURE usp_TableToClass
/*
Created by Cade Bryant.
Generates C# class code for a table and fields/properties for each column. Run as "Results to Text" or "Results to File" (not Grid)

Example: EXEC usp_TableToClass ‘MyTable’
*/

@table_name SYSNAME

AS

SET NOCOUNT ON

DECLARE @temp TABLE
(
sort INT,
code TEXT
)

INSERT INTO @temp
SELECT 1, ‘public class ‘ + @table_name + CHAR(13) + CHAR(10) + ‘{‘

INSERT INTO @temp
SELECT 2, CHAR(13) + CHAR(10) + ‘#region Constructors’ + CHAR(13) + CHAR(10)
INSERT INTO @temp
SELECT 3, CHAR(9) + ‘public ‘ + @table_name + ‘()’
+ CHAR(13) + CHAR(10) + CHAR(9) + ‘{‘
+ CHAR(13) + CHAR(10) + CHAR(9) + ‘}’

INSERT INTO @temp
SELECT 4, ‘#endregion’ + CHAR(13) + CHAR(10)

INSERT INTO @temp
SELECT 5, ‘#region Private Fields’ + CHAR(13) + CHAR(10)
INSERT INTO @temp
SELECT 6, CHAR(9) + ‘private ‘ +
CASE
WHEN DATA_TYPE LIKE ‘%CHAR%’ THEN ‘string ‘
WHEN DATA_TYPE LIKE ‘%INT%’ THEN ‘int ‘
WHEN DATA_TYPE LIKE ‘%DATETIME%’ THEN ‘DateTime ‘
WHEN DATA_TYPE LIKE ‘%BINARY%’ THEN ‘byte[] ‘
WHEN DATA_TYPE = ‘BIT’ THEN ‘bool ‘
WHEN DATA_TYPE LIKE ‘%TEXT%’ THEN ‘string ‘
ELSE ‘object ‘
END + ‘_’ + COLUMN_NAME + ‘;’ + CHAR(9)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @table_name
ORDER BY ORDINAL_POSITION

INSERT INTO @temp
SELECT 7, ‘#endregion’ +
CHAR(13) + CHAR(10)

INSERT INTO @temp
SELECT 8, ‘#region Public Properties’ + CHAR(13) + CHAR(10)
INSERT INTO @temp
SELECT 9, CHAR(9) + ‘public ‘ +
CASE
WHEN DATA_TYPE LIKE ‘%CHAR%’ THEN ‘string ‘
WHEN DATA_TYPE LIKE ‘%INT%’ THEN ‘int ‘
WHEN DATA_TYPE LIKE ‘%DATETIME%’ THEN ‘DateTime ‘
WHEN DATA_TYPE LIKE ‘%BINARY%’ THEN ‘byte[] ‘
WHEN DATA_TYPE = ‘BIT’ THEN ‘bool ‘
WHEN DATA_TYPE LIKE ‘%TEXT%’ THEN ‘string ‘
ELSE ‘object ‘
END + COLUMN_NAME +
CHAR(13) + CHAR(10) + CHAR(9) + ‘{‘ +
CHAR(13) + CHAR(10) + CHAR(9) + CHAR(9) +
‘get { return _’ + COLUMN_NAME + ‘; }’ +
CHAR(13) + CHAR(10) + CHAR(9) + CHAR(9) +
‘set { _’ + COLUMN_NAME + ‘ = value; }’ +
CHAR(13) + CHAR(10) + CHAR(9) + ‘}’
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @table_name
ORDER BY ORDINAL_POSITION

INSERT INTO @temp
SELECT 10, ‘#endregion’ +
CHAR(13) + CHAR(10) + ‘}’

SELECT code FROM @temp
ORDER BY sort

Advertisement

The wisdom of this world [1 Corinthians 3:19]

[NIV] 1 Corinthians 3:19
For the wisdom of this world is foolishness in God’s sight.

[NASB] 1 Corinthians 3:19
For the wisdom of this world is foolishness before God

[NKJV] 1 Corinthians 3:19
For the wisdom of this world is foolishness with God.

[MSG]
Don’t fool yourself. Don’t think that you can be wise merely by being up-to-date with the times.

[AMP]
For this world’s wisdom is foolishness (absurdity and stupidity) with God.

[NLT]
For the wisdom of this world is foolishness to God.

[GWT]
The wisdom of this world is nonsense in God’s sight.

[WE]
The things that look wise to the people of this world look foolish to God.

Glory Study

Exodus 16:4-12

4Then said the LORD unto Moses, Behold, I will rain bread from heaven for you; and the people shall go out and gather a certain rate every day, that I may prove them, whether they will walk in my law, or no.

5And it shall come to pass, that on the sixth day they shall prepare that which they bring in; and it shall be twice as much as they gather daily.

6And Moses and Aaron said unto all the children of Israel, At even, then ye shall know that the LORD hath brought you out from the land of Egypt:

7And in the morning, then ye shall see the glory of the LORD; for that he heareth your murmurings against the LORD: and what are we, that ye murmur against us?

8And Moses said, This shall be, when the LORD shall give you in the evening flesh to eat, and in the morning bread to the full; for that the LORD heareth your murmurings which ye murmur against him: and what are we? your murmurings are not against us, but against the LORD.

9And Moses spake unto Aaron, Say unto all the congregation of the children of Israel, Come near before the LORD: for he hath heard your murmurings.

10And it came to pass, as Aaron spake unto the whole congregation of the children of Israel, that they looked toward the wilderness, and, behold, the glory of the LORD appeared in the cloud.

11And the LORD spake unto Moses, saying,

12I have heard the murmurings of the children of Israel: speak unto them, saying, At even ye shall eat flesh, and in the morning ye shall be filled with bread; and ye shall know that I am the LORD your God.

Adopted as sons and daughters

Ephesians 1
3-6How blessed is God! And what a blessing he is! He’s the Father of our Master, Jesus Christ, and takes us to the high places of blessing in him. Long before he laid down earth’s foundations, he had us in mind, had settled on us as the focus of his love, to be made whole and holy by his love. Long, long ago he decided to adopt us into his family through Jesus Christ. (What pleasure he took in planning this!) He wanted us to enter into the celebration of his lavish gift-giving by the hand of his beloved Son.

Sinner to Saint

Proverbs 23:7:

Or as he thinks within himself, so he is; (NIV)
For as he thinks in his heart, so is he: (Amplified Bible)
For as he thinketh in his heart, so is he: (KJV)
For as he thinketh within himself, so is he: (ASV)

Revelation 1:5-6

To him who loves us and has freed us from our sins by his blood, 6and has made us to be a kingdom and priests to serve his God and Father—to him be glory and power for ever and ever! Amen.

[NKJV] Romans 8:26-27

Likewise the Spirit also helps in our weaknesses. For we do not know what we should pray for as we ought, but the Spirit Himself makes intercession for us with groanings which cannot be uttered. Now He who searches the hearts knows what the mind of the Spirit is, because He makes intercession for the saints according to the will of God.

1 Corinthians 1:2

[NKJV] To the church of God which is at Corinth, to those who are sanctified in Christ Jesus, called to be saints, with all who in every place call on the name of Jesus Christ our Lord, both theirs and ours:

[NIV] To the church of God in Corinth, to those sanctified in Christ Jesus and called to be his holy people, together with all those everywhere who call on the name of our Lord Jesus Christ—their Lord and ours:

[NASB] To the church of God which is at Corinth, to those who have been sanctified in Christ Jesus, saints by calling, with all who in every place call on the name of our Lord Jesus Christ, their Lord and ours:

[MSG] I send this letter to you in God’s church at Corinth, believers cleaned up by Jesus and set apart for a God-filled life. I include in my greeting all who call out to Jesus, wherever they live. He’s their Master as well as ours!

Ephesians 2:19-22

[NKJV] Now, therefore, you are no longer strangers and foreigners, but fellow citizens with the saints and members of the household of God, having been built on the foundation of the apostles and prophets, Jesus Christ Himself being the chief cornerstone, in whom the whole building, being fitted together, grows into a holy temple in the Lord, in whom you also are being built together for a dwelling place of God in the Spirit.

[MSG] That’s plain enough, isn’t it? You’re no longer wandering exiles. This kingdom of faith is now your home country. You’re no longer strangers or outsiders. You belong here, with as much right to the name Christian as anyone. God is building a home. He’s using us all—irrespective of how we got here—in what he is building. He used the apostles and prophets for the foundation. Now he’s using you, fitting you in brick by brick, stone by stone, with Christ Jesus as the cornerstone that holds all the parts together. We see it taking shape day after day—a holy temple built by God, all of us built into it, a temple in which God is quite at home.

[NIV] Consequently, you are no longer foreigners and strangers, but fellow citizens with God’s people and also members of his household, built on the foundation of the apostles and prophets, with Christ Jesus himself as the chief cornerstone. In him the whole building is joined together and rises to become a holy temple in the Lord. And in him you too are being built together to become a dwelling in which God lives by his Spirit.

[AMP] Therefore you are no longer outsiders (exiles, migrants, and aliens, excluded from the rights of citizens), but you now share citizenship with the saints (God’s own people, consecrated and set apart for Himself); and you belong to God’s [own] household.You are built upon the foundation of the apostles and prophets with Christ Jesus Himself the chief Cornerstone. In Him the whole structure is joined (bound, welded) together harmoniously, and it continues to rise (grow, increase) into a holy temple in the Lord [a sanctuary dedicated, consecrated, and sacred to the presence of the Lord]. In Him [and in fellowship with one another] you yourselves also are being built up [into this structure] with the rest, to form a fixed abode (dwelling place) of God in (by, through) the Spirit.

Romans 14:1,22 Extend Grace

1 Welcome with open arms fellow believers who don’t see things the way you do. And don’t jump all over them every time they do or say something you don’t agree with—even when it seems that they are strong on opinions but weak in the faith department. Remember, they have their own history to deal with. Treat them gently.

22 Cultivate your own relationship with God, but don’t impose it on others.