MSSQL数据库表 批量转c#实体类

MSSQL数据库表,批量转c#实体类 ,我知道网上有现成的,可是我就想自己写个来玩玩
这个工具的关键点,在于获取到表的所有字段名和类型,然后就是拼接字符串的事了
直接上代码

首先就是获取所有数据库名,数据库下的所有表名和表里的所有字段,具体怎么获取,拿sql到数据库里跑一下就清楚了

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DataTableToEntityClass
{
    public class DAL:IDAL
    {
       
        /// <summary>
        /// 获取服务器上所有数据库名
        /// </summary>
        /// <param name="conStr"></param>
        /// <returns></returns>
        public List<TempModel> GetDataBaseList(string conStr)
        {
            List<TempModel> result = new List<TempModel>();
            string sql = "Select Name From Master..SysDatabases order By Name";
            DataSet ds = SqlHelper.sqlSelectDS(conStr, CommandType.Text, sql);
            foreach(DataRow row in ds.Tables[0].Rows)
            {
                result.Add(new TempModel { Name = row["Name"].DBValueToTypeValue<string>() ?? "", Type = "DataBase" });
            }
            return result;
        }
        /// <summary>
        /// 获取指定数据库的所有表名
        /// </summary>
        /// <param name="conStr"></param>
        /// <returns></returns>
        public List<TempModel> GetTableList(string conStr)
        {
            List<TempModel> result = new List<TempModel>();
            string sql = $"Select Name From sysobjects Where XType='U' order By Name";
            DataSet ds = SqlHelper.sqlSelectDS(conStr, CommandType.Text, sql);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                result.Add(new TempModel { Name = row["Name"].DBValueToTypeValue<string>() ?? "", Type = "Table" });
            }
            return resul
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值