Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms; namespace ModelFactory
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DBSelect dbs = new DBSelect();
dbs.ShowDialog();
TableSelect ts = new TableSelect();
ts.ShowDialog();
Application.Run(new ModelFactory());
}
}
}

  

DataBase.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ModelFactory
{
public static class DataBase
{
public static string DataSouse { get; set; }
public static string USER_ID { get; set; }
public static string PASSWORD { get; set; }
public static string database { get; set; }
private static string connectString{ get; set; }
public static List<string> tablenames = new List<string>();
public static void Init()
{ if (string.IsNullOrEmpty(DataBase.database)) DataBase.database = "master";
connectString = string.Format("DATA SOURCE={0};INITIAL CATALOG={1};USER ID={2};PASSWORD={3};", DataSouse, database, USER_ID, PASSWORD);
}
public static SqlConnection SqlOpen()
{
SqlConnection connection = null;
try
{
connection = new SqlConnection(connectString);
//connection.Open();
return connection;
}
catch
{
return connection;
}
}
public static bool SqlClose(SqlConnection connection)
{
if (connection != null)
{
connection.Close();
return true;
}
else
return false;
}
public static DataSet ProcSet(string commandstr)
{
// data.Clear();
DataSet data = new DataSet();
SqlConnection connection = SqlOpen();
SqlDataAdapter aCommand = new SqlDataAdapter(commandstr, connection);
try
{
aCommand.SelectCommand.CommandTimeout = 120;
connection.Open();
aCommand.Fill(data);
SqlClose(connection);
return data;
}
catch (Exception e)
{
return null;
}
finally
{
SqlClose(connection);
}
}
public static int DoProc(string commandstr)
{
SqlConnection connection = SqlOpen();
SqlCommand sqlCom = new SqlCommand(commandstr, connection);
sqlCom.CommandTimeout = 1200;
try
{
connection.Open();
int x = Convert.ToInt32(sqlCom.ExecuteNonQuery());
SqlClose(connection); return x; }
catch (Exception e)
{
return 0;
}
finally
{
SqlClose(connection);
}
}
public static string ProcString(string commandstr)
{
SqlConnection connection = SqlOpen();
SqlCommand sqlCom = new SqlCommand(commandstr, connection);
sqlCom.CommandTimeout = 600;
string str = "";
try
{
connection.Open();
var obj = sqlCom.ExecuteScalar();
if (obj != null)
str = obj.ToString();
return str;
}
catch (Exception e)
{
return "";
}
finally
{
SqlClose(connection);
} }
public static void Add(string tablename)
{
tablenames.Add(tablename);
}
} }

  DBSelect

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; namespace ModelFactory
{
public partial class DBSelect : Form
{
public DBSelect()
{
InitializeComponent();
} private void textBox3_Leave(object sender, EventArgs e)
{
DataBase.DataSouse = textBox1.Text.Trim();
DataBase.USER_ID = textBox2.Text.Trim();
DataBase.PASSWORD = textBox3.Text.Trim();
DataBase.Init();
DataSet ds = DataBase.ProcSet("SELECT d.name,d.database_id FROM sys.databases AS d WHERE d.log_reuse_wait>0"); comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "database_id";
comboBox1.DataSource = ds.Tables[0];
} private void button1_Click(object sender, EventArgs e)
{
DataBase.database = comboBox1.Text;
DataBase.Init();
this.Close(); } private void comboBox1_Enter(object sender, EventArgs e)
{
textBox3_Leave(sender, e);
}
}
}

  TableSelect

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; namespace ModelFactory
{
public partial class TableSelect : Form
{
public TableSelect()
{
InitializeComponent();
listBox2.DisplayMember = "name";
listBox2.ValueMember = "name";
listBox1.DataSource = DataBase.ProcSet("SELECT t.name FROM sys.tables AS t").Tables[0];
listBox1.DisplayMember = "name";
listBox1.ValueMember = "name"; } private void listBox1_DoubleClick(object sender, EventArgs e)
{
//listBox1.Items.Remove(listBox1.SelectedItem);
listBox2.Items.Add(listBox1.SelectedItem);
} private void listBox2_DoubleClick(object sender, EventArgs e)
{
listBox2.Items.Remove(listBox2.SelectedItem);
} private void button3_Click(object sender, EventArgs e)
{ foreach(System.Data.DataRowView row in listBox2.Items)
{
DataBase.Add(row.Row.ItemArray[0].ToString());
}
this.Close();
} }
}

  ModelFactory.cs

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; namespace ModelFactory
{
public partial class ModelFactory : Form
{
private StringBuilder sb = new StringBuilder();
public ModelFactory()
{
InitializeComponent();
foreach (string tablename in DataBase.tablenames)
{
sb.AppendLine(" public class " + tablename);
sb.AppendLine(" {");
DataTable dt = DataBase.ProcSet("select top 1 * from " + tablename).Tables[0];
//DbType dbtype;
string dbtype,columnname; for (int i = 0; i < dt.Columns.Count;i++ )
{
dbtype = CastType(dt.Columns[i].DataType);
columnname = dt.Columns[i].ColumnName;
sb.AppendLine(" public " + dbtype + " " + columnname+" {get;set;}");
}
sb.AppendLine(" }");
}
textBox1.Text = sb.ToString();
}
public string CastType(Type type)
{
if (type == typeof(string))
{
return "string";
}
else if (type == typeof(DateTime))
{
return "DateTime";
}
else if (type == typeof(bool))
{
return "bool";
}
else if (type == typeof(int))
{
return "int";
}
else
{
return type.ToString().Split('.')[type.ToString().Split('.').Length - 1];
} }
}
}

  

C# 连接SQLServer数据库自动生成model类代码的更多相关文章

  1. django根据已有数据库表生成model类

    django根据已有数据库表生成model类 创建一个Django项目 django-admin startproject 'xxxx' 修改setting文件,在setting里面设置你要连接的数据 ...

  2. 使用T4为数据库自动生成实体类

    T4 (Text Template Transformation Toolkit) 是一个基于模板的代码生成器.使用T4你可以通过写一些ASP.NET-like模板,来生成C#, T-SQL, XML ...

  3. Asp.Net Core如何根据数据库自动生成实体类

    通过引用Nuget包添加实体类 运行 Install-Package Microsoft.EntityFrameworkCore.SqlServer 运行 Install-Package Micros ...

  4. SpringBoot整合Mybatis 使用generator自动生成实体类代码、Mapper代码、dao层代码

    1.新建一个SpringBoot项目,并引入Mybatis和mybatis-generator相关的依赖. <dependency> <groupId>org.springfr ...

  5. 使用mybatis-generator在自动生成Model类和Mapper文件

    使用mybatis-generator插件可以很轻松的实现mybatis的逆向工程,即,能通过表结构自动生成对应的java类及mapper文件,可以大大提高工作效率,并且它提供了很多自定义的设置可以应 ...

  6. c# 利用t4模板,自动生成Model类

    我们在用ORM(比如dapper)的时候,很多时候都需要自己写Model层(当然也有很多orm框架自带了这种功能,比如ef),特别是表里字段比较多的时候,一个Model要写半天,而且Model如果用于 ...

  7. Entity Framework连接Mysql数据库并生成Model和DAL层

    Entity Framework (EF,ADO.NET Entity Framework)是微软官方提供的.NET平台的ORM框架.相比于LINQ TO SQL,EF框架具有很明显的优势: EF框架 ...

  8. mybatis自定义代码生成器(Generator)——自动生成model&dao代码

    花了两天的时间研究了下mybatis的generator大体了解了其生成原理以及实现过程.感觉generator做的非常不错,给开发者也留足了空间.看完之后在generator的基础上实现了自定义的生 ...

  9. MVC Html辅助方法DropDownList的简单使用、连接MYSQL数据库用自定义model类接收

    附上启发链接:https://www.cnblogs.com/CreateMyself/p/5424894.html [HttpGet] public ActionResult Edit(int id ...

随机推荐

  1. 【2019.11.18】SDN阅读作业

    为什么需要SDN?SDN特点? 随着网络的快速发展,传统互联网出现了如传统网络配置复杂度高等诸多问题,这些问题说明网络架构需要革新,可编程网络的相关研究为 SDN 的产生提供了可参考的理论依据 SDN ...

  2. 美团-2019Q2述职总结

    述职要求: 产品对平台化的规划并不清晰:内部因素:对SaaS平台的理解不够深刻: 对公司相关脚手架,服务搭建相关需要注意的点,有更深入的认识.对做系统服务的关注点有了更深入的理解. 功能权限的话: Q ...

  3. [Beta]第十次 Scrum Meeting

    [Beta]第十次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/20 22:00 20min 大运村公寓6F寝室 附Github仓库:WEDO 例会照片 工作情况 ...

  4. IE安全限制

    在安全级别下面设置置进行如下调整: A.ActiveX控件自动提示:启用 B.对标记为可安全执行脚本的ActiveX控件执行脚本:启用 C.对未标记为可安全执行脚本的ActiveX控件初始化并执行脚本 ...

  5. 2019_软工实践_Beta(3/5)

    队名:955 组长博客:点这里! 作业博客:点这里! 组员情况 组员1(组长):庄锡荣 过去两天完成了哪些任务 文字/口头描述 ? 维持进度,检查需求 展示GitHub当日代码/文档签入记录 接下来的 ...

  6. SQLSERVER根据提成比率区间计算业绩提成

    USE [Employee] GO /****** Object: Table [dbo].[Commission] Script Date: 2019/11/17 14:10:21 ******/ ...

  7. Android中Activity的启动模式(LaunchMode)和使用场景

    一.为什么需要启动模式在Android开发中,我们都知道,在默认的情况下,如果我们启动的是同一个Activity的话,系统会创建多个实例并把它们一一放入任务栈中.当我们点击返回(back)键,这些Ac ...

  8. 源码包的三个参数make-configure-make install解释

    1.configure 这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如代码: ./configure --prefix= ...

  9. CefSharp 提示 flash player is out of date 运行此插件 等问题解决办法

    CefSharp 提示 flash player is out of date 或者 需要手动右键点 运行此插件 脚本 等问题解决办法 因为中国版FlashPlayer变得Ad模式之后,只好用旧版本的 ...

  10. 运行okvis-mono

    ./build/okvis_app_synchronous config/config_fpga_p2_euroc1.yaml ../mav0