Сгенерировать код класса C # для таблицы в Visual Studio

#c# #class #visual-studio-2010 #auto-generate

#c# #класс #visual-studio-2010 #автогенерация

Вопрос:

У меня есть несколько таблиц в файле базы данных моего сервера. Я хочу сгенерировать автоматический код классов для таблиц, чтобы автоматически генерировались все классы с их атрибутами, конструктором и методами установки getter.

Пожалуйста, подскажите, как это сделать.

Комментарии:

1. Существует несколько способов «сгенерировать код», в зависимости от того, какой код вы хотите сгенерировать. Вы хотите использовать старую технологию DataSet или более новую «Entity Framework»?

2. Вы хотите использовать Entity Framework?

Ответ №1:

Не автоматически сгенерировать, но нетрудно использовать sql и information_schema для вывода определения класса, при этом класс, названный в честь таблицы, и столбцы сопоставляются со свойствами. Оттуда вы можете заставить его генерировать создание, обновления и удаления (мне нравится использовать слияние для создания / обновления в SQL Server 2008).

Выполняйте их по одному, и в основном это конкатинация строк. Приведенное ниже должно помочь вам начать….

 declare @class varchar(max);

; with typemapping as (
 Select 'varchar' as DATA_TYPE, 'string' ctype
 union
 Select 'int', 'int'
)
select @class = isnull(@class   char(10), '')   'public '  
       tm.ctype  ' '   column_name  
       ' { get; set; }'
from information_schema.columns sc
   inner join typemapping tm on sc.data_type = tm.data_type
where table _name ='yourtbl'

print @class;
 

Остальное оставлено в качестве упражнения для читателя, как говорится, в основном потому, что детали зависят от вас, вместо автоматических свойств вы могли бы использовать резервные переменные, помещать стандартную логику в свойства, делать типы значений обнуляемыми, при создании собственного генератора кода подгоняйте его под ваши шаблоны / стили /потребности.

Ответ №2:

Если вы используете Entity Framework, ознакомьтесь с шагами этой статьи:

Генерация классов первой модели кода EF из существующей базы данных

Ответ №3:

вы могли бы попробовать что-то вроде этого…

Создайте один основной класс с именем ModelCreator.cs, который выполняет все ключевые операции. Точкой входа в это приложение является событие подключения и нажатия кнопки создания. Он запустит метод CreateConnectionString(), который в основном получает входные данные от пользователя и динамически создает строку подключения

 private void lbtnConnect_Click(object sender, System.EventArgs e)
{
   if (CreateConnectionString())
  CreateModelClassFiles(tcGetDataReader());
}

// <summary>
/// Get the SqlDataReader object
/// SqlDataReader
/// </summary>

public SqlDataReader tcGetDataReader()
{
SqlConnection connection = null;
try
{
  connection = GetConnection(SQL_CONN_STRING);
  if (connection == null)
  return null;
  SqlDataReader dr = SqlHelper.ExecuteReader(
         connection,
         CommandType.StoredProcedure,
         "getData");
if (dr.HasRows)
  return dr;
else
  return null;
 }
 catch(Exception ex)
 {
  MessageBox.Show(ex.Message);
  return null;
 }   
}
 

Получение имен таблиц, атрибутов и их типов из базы данных

  CREATE PROCEDURE getData AS 
 select table_name, column_name, data_type
  from information_schema.columns
  where table_name in
  (
   select table_name
   from Information_Schema.Tables
   where Table_Type='Base Table'
  ) order by table_name
GO
 

Основной метод, CreateModelClassFiles

   /// <summary>
 /// Create the Model class list iterating through the tables
/// </summary>
/// <param name="dr">Sql Data reader for the database schema</param>

    private void CreateModelClassFiles(SqlDataReader dr)
    {
     if (dr != null)
    {
    string lstrOldTableName = string.Empty;
    StreamWriter sw = null;
    System.Text.StringBuilder sb = null;
    System.Text.StringBuilder sbAttr = null;
    while(dr.Read())
    {
      string lstrTableName = dr.GetString(0);
      string lstrAttributeName = dr.GetString(1);
      string lstrAttributeType = GetSystemType(dr.GetString(2));
      if (lstrOldTableName != lstrTableName)
      {
        if (sw != null)
        {
          this.CreateClassBottom(sw, sb.ToString().TrimEnd(
                     new char[]{',', ' ', 'r', 't', 'n'}),
                     sbAttr.ToString());
            sw.Close();
        }
        sb = new System.Text.StringBuilder(lstrTableName);
        sb.Append(".cs");
        FileInfo lobjFileInfo = new FileInfo(sb.ToString());
        sw = lobjFileInfo.CreateText();
        this.CreateClassTop(sw, lstrTableName);
        sb = new System.Text.StringBuilder("rnt/// rnt"   
             "/// User defined Contructorrnt/// rntpublic ");
        sbAttr = new System.Text.StringBuilder();
        sb.Append(lstrTableName);
        sb.Append("(");
      }
      else
      {
        this.CreateClassBody(sw, lstrAttributeType, lstrAttributeName);
        sb.AppendFormat("{0} {1}, rntt", 
           new object[]{lstrAttributeType, lstrAttributeName});
        sbAttr.AppendFormat("rnttthis._{0} = {0};", 
           new object[]{lstrAttributeName});
      }
      lstrOldTableName = lstrTableName;
      this.progressBarMain.Increment(1); 
    }
    MessageBox.Show("Done !!");
  }
}
 

Как только этот метод вызывается, он делает все за вас.

я надеюсь, что это поможет вам….

Комментарии:

1. Привет, не могли бы вы поместить код любого CreateClassBottom, createclassstop или CreateClassBody?

2. @Enigma State пожалуйста, укажите определения методов, используемых в вашем коде.

Ответ №4:

Я изменил приведенный выше класс и включил все отсутствующие методы

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;

namespace CodeGenerator

{
    public partial class Form1 : Form
    {

    public Form1()
    {
        InitializeComponent();
    }

    private void btnGenerateCode_Click(object sender, EventArgs e)
    {
        string conStrJobsDB = ConfigurationSettings.AppSettings["jobsDBConStrKey"].ToString();
        CreateEntitiesFromDBTables(GetDataReader(conStrJobsDB));
    }

    private void CreateEntitiesFromDBTables(SqlDataReader dr)
    {
        if (dr != null)
        {
            string lstrOldTableName = string.Empty;
            StreamWriter swClassWriter = null;
            System.Text.StringBuilder sbFileName = null;
            System.Text.StringBuilder sbConstructorCode = null;
            System.Text.StringBuilder sbClassCode = null;
            FileInfo tableClassFile = null;

            while (dr.Read())
            {
                string lstrTableName = dr.GetString(0);
                string lstrAttributeName = dr.GetString(1);
                string lstrAttributeType = GetDotNetType(dr.GetString(2));

                //If table name is changed...
                if (lstrOldTableName != lstrTableName)
                {
                    //and stream writer is already opened so close this class generation...
                    if (swClassWriter != null)
                    {
                        CreateClassBottom(swClassWriter);
                        swClassWriter.Close();
                    }

                    sbFileName = new System.Text.StringBuilder(lstrTableName);
                    sbFileName.Append("Entity.cs");
                    tableClassFile = new FileInfo(tbPath.Text   "\"   sbFileName.ToString());
                    swClassWriter = tableClassFile.CreateText();
                    CreateClassTop(swClassWriter, lstrTableName);

                    //sbConstructorCode = new System.Text.StringBuilder("rnt/// rnt"  
                    //     "/// User defined Contructorrnt/// rntpublic ");
                    //sbConstructorCode = new System.Text.StringBuilder();
                    //sbConstructorCode.Append(lstrTableName);
                    //sbConstructorCode.Append("(");
                }
                else
                {
                    this.CreateClassBody(swClassWriter, lstrAttributeType, lstrAttributeName);
                    //sbConstructorCode.AppendFormat("{0} {1}, rntt",
                    //   new object[] { lstrAttributeType, lstrAttributeName });
                    //sbConstructorCode.AppendFormat("rnttthis._{0} = {0};",
                    //   new object[] { lstrAttributeName });
                }

                lstrOldTableName = lstrTableName;
                this.pBarMain.Increment(1);
            }

            MessageBox.Show("All classes generated.", "Done");
        }
    }

    private SqlDataReader GetDataReader(string conStrJobsDB)
    {
        SqlConnection connection = null;
        try
        {
            connection = new SqlConnection(conStrJobsDB);
            if (connection == null)
                return null;
            connection.Open();
            SqlCommand command = new System.Data.SqlClient.SqlCommand("exec spGenerateEntitiesFromTables", connection);
            SqlDataReader dr = command.ExecuteReader();
            if (dr.HasRows)
                return dr;
            else
                return null;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return null;
        }
    }

    private string GetDotNetType(string dbColumnType)
    {
        string returnType = string.Empty;
        if (dbColumnType.Equals("nvarchar"))
            returnType = "string";
        else if (dbColumnType.Equals("varchar"))
            returnType = "string";
        else if (dbColumnType.Equals("int"))
            returnType = "int";
        else if (dbColumnType.Equals("bit"))
            returnType = "bool";
        else if (dbColumnType.Equals("bigint"))
            returnType = "long";
        else if (dbColumnType.Equals("binary"))
            returnType = "byte[]";
        else if (dbColumnType.Equals("char"))
            returnType = "string";
        else if (dbColumnType.Equals("date"))
            returnType = "DateTime";
        else if (dbColumnType.Equals("datetime"))
            returnType = "DateTime";
        else if (dbColumnType.Equals("datetime2"))
            returnType = "DateTime";
        else if (dbColumnType.Equals("datetimeoffset"))
            returnType = "DateTimeOffset";
        else if (dbColumnType.Equals("decimal"))
            returnType = "decimal";
        else if (dbColumnType.Equals("float"))
            returnType = "float";
        else if (dbColumnType.Equals("image"))
            returnType = "byte[]";
        else if (dbColumnType.Equals("money"))
            returnType = "decimal";
        else if (dbColumnType.Equals("nchar"))
            returnType = "char";
        else if (dbColumnType.Equals("ntext"))
            returnType = "string";
        else if (dbColumnType.Equals("numeric"))
            returnType = "decimal";
        else if (dbColumnType.Equals("nvarchar"))
            returnType = "string";
        else if (dbColumnType.Equals("real"))
            returnType = "double";
        else if (dbColumnType.Equals("smalldatetime"))
            returnType = "DateTime";
        else if (dbColumnType.Equals("smallint"))
            returnType = "short";
        else if (dbColumnType.Equals("smallmoney"))
            returnType = "decimal";
        else if (dbColumnType.Equals("text"))
            returnType = "string";
        else if (dbColumnType.Equals("time"))
            returnType = "TimeSpan";
        else if (dbColumnType.Equals("timestamp"))
            returnType = "DateTime";
        else if (dbColumnType.Equals("tinyint"))
            returnType = "byte";
        else if (dbColumnType.Equals("uniqueidentifier"))
            returnType = "Guid";
        else if (dbColumnType.Equals("varbinary"))
            returnType = "byte[]";

        return returnType;
    }

    private void CreateClassTop(StreamWriter sw, string lstrTableName)
    {
        System.Text.StringBuilder sb = null;
        sb = new StringBuilder("public class "   lstrTableName  "Entityn{");
        sw.Write(sb.ToString());
    }

    private void CreateClassBody(StreamWriter sw, string lstrAttributeType, string lstrAttributeName)
    {
        System.Text.StringBuilder sb = null;
        sb = new StringBuilder("nrpublic "   lstrAttributeType   " "   lstrAttributeName   " { get; set; }");
        sw.Write(sb.ToString());
    }

    private void CreateClassBottom(StreamWriter sw)
    {
        System.Text.StringBuilder sb = null;
        sb = new StringBuilder("nn}");
        sw.Write(sb.ToString());
    }



}
 

}

Ответ №5:

я думаю, что самый простой способ сгенерировать класс cs из таблицы — это запрос к вашей базе данных, например, в SQL server вы можете использовать этот запрос:

 declare @tblName sysname = 'SetYourTableName'
declare @ResultText varchar(max) = 'public class '   @tblName   '
{'

select @ResultText = @ResultText   '
    public '   ColumnType   NullableSign   ' '   ColumnName   ' { get; set; }
'
from
(
    select 
        replace(col.name, ' ', '_') ColumnName,
        column_id ColumnId,
        case typ.name 
            when 'bigint' then 'long'
            when 'binary' then 'byte[]'
            when 'bit' then 'bool'
            when 'char' then 'string'
            when 'date' then 'DateTime'
            when 'datetime' then 'DateTime'
            when 'datetime2' then 'DateTime'
            when 'datetimeoffset' then 'DateTimeOffset'
            when 'decimal' then 'decimal'
            when 'float' then 'float'
            when 'image' then 'byte[]'
            when 'int' then 'int'
            when 'money' then 'decimal'
            when 'nchar' then 'string'
            when 'ntext' then 'string'
            when 'numeric' then 'decimal'
            when 'nvarchar' then 'string'
            when 'real' then 'double'
            when 'smalldatetime' then 'DateTime'
            when 'smallint' then 'short'
            when 'smallmoney' then 'decimal'
            when 'text' then 'string'
            when 'time' then 'TimeSpan'
            when 'timestamp' then 'DateTime'
            when 'tinyint' then 'byte'
            when 'uniqueidentifier' then 'Guid'
            when 'varbinary' then 'byte[]'
            when 'varchar' then 'string'
            else 'UNKNOWN_'   typ.name
        end ColumnType,
        case 
            when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier') 
            then '?' 
            else '' 
        end NullableSign
    from sys.columns col
        join sys.types typ on
            col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
    where object_id = object_id(@tblName)
) t
order by ColumnId

set @ResultText = @ResultText    '
}'

print @ResultText
 

затем запустите этот запрос и скопируйте напечатанный текст результата в новый класс в Visual Studio

я надеюсь, что это сработает для вас

удачи