NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

<#@ template language="C#" hostspecific="true" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Data" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Text" #>



<#

// veri tabanı bağlantı stringi
string connection = "Server=SATURN08\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;";



string GetTableData(string tableName) {

using SqlConnection sqlCon = new SqlConnection(connection);
sqlCon.Open();

string sqlQuery =
@"DECLARE @table NVARCHAR(250) = @tableName;

DECLARE @classHeader NVARCHAR(MAX) = 'public class ' + replace(@table,' ','') + '
{
';

DECLARE @classFooter NVARCHAR(MAX) = '
}';

DECLARE @properties NVARCHAR(MAX) = (
SELECT STRING_AGG(CONCAT(
' public ',
IIF(DATA_TYPE = 'nvarchar', 'string' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'uniqueidentifier', 'string' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),


IIF(DATA_TYPE = 'ntext', 'string' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'nchar', 'string' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),


IIF(DATA_TYPE = 'int', 'int' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),


IIF(DATA_TYPE = 'smallint', 'short' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'tinyint', 'byte' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'money', 'decimal' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'float', 'float' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'decimal', 'decimal' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'bit', 'bool' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'datetime', 'DateTime' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'date', 'DateTime' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'datetime2', 'DateTime' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'image', 'byte[]' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
IIF(DATA_TYPE = 'real', 'float' + IIF(IS_NULLABLE = 'YES', '?', ''), ''),
' ',
COLUMN_NAME,
' { get; set; }',
IIF((DATA_TYPE = 'nvarchar' OR DATA_TYPE = 'nchar') AND IS_NULLABLE = 'NO', ' = null!;', ''),
CHAR(13), CHAR(10)
), '') AS properties
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @table
AND TABLE_SCHEMA = 'dbo'
);

DECLARE @classDefinition NVARCHAR(MAX) = @classHeader + @properties + @classFooter;

SELECT @classDefinition; ";

using SqlCommand command = new SqlCommand(sqlQuery, sqlCon);
command.Parameters.AddWithValue("@tableName", tableName);
string result = (string)command.ExecuteScalar();

return result;

}

// klasör oluşturma yolu
string folderPath = Path.Combine(Host.ResolvePath("."), "Models");

// klasör yoksa oluştur
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}




// tablo adlarını al
var tableNames = new List<string>();


// veri tabanı bağlantısı

using SqlConnection con = new SqlConnection(connection);
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
using SqlCommand cmd = new("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG='Northwind'", con);

SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
tableNames.Add(reader[0].ToString());
}

reader.Close();
con.Close();


// tablo adlarına göre dosya oluştur
foreach(string tableName in tableNames){
string classPath = Path.Combine(folderPath, tableName.Replace(" ", "") + ".cs");


// dosya yazma işlemi
using(FileStream fs = File.Create(classPath)){
string content = GetTableData(tableName);
byte[] contentBytes = Encoding.UTF8.GetBytes(content);
fs.Write(contentBytes, 0, contentBytes.Length);



}
}

#>
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.