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];
} }
}
}

  

最新文章

  1. java删除文件夹
  2. SQLSERVER取当前月第一天和最后一天
  3. WPF学习之路(十二)控件(HeaderedContent控件)
  4. HDU 4821 String (HASH)
  5. DevExpress GridControl使用方法总结
  6. 随机Loading
  7. XMPP 常见错误:(&lt;failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"&gt;&lt;not-autho
  8. BZOJ1576 [Usaco2009 Jan]安全路经Travel
  9. 神经网络:卷积神经网络CNN
  10. Particle designer 粒子工具中属性对应功能的简单介绍
  11. IOS上iframe的滚动条失效的解决办法
  12. Thinkphp里import的几个使用方法介绍
  13. CentOS 6.5 开机启动指定服务
  14. c# Activex开发之HelloWorld
  15. CentOS上安装Git服务器
  16. DEDE整站动态/静态转换
  17. 使用nginx代理kibana并配置登录验证
  18. 问题:UltraISO:这个软件有问题,它制作的iso文件会造成无法正确识别。用PowerISO吧
  19. VS2010使用Release进行调试的三个必须设置选项
  20. Java 利用递归删除文件以及文件夹

热门文章

  1. 微信小程序 movable-view组件应用:可拖动悬浮框_返回首页
  2. CSS各类布局
  3. TCP的连接如何知道对方已经异常断开
  4. [转]OpenGL图形渲染管线、VBO、VAO、EBO概念及用例
  5. nginx针对某个url限制ip访问,常用于后台访问限制【转】
  6. 详解python3如何调用c语言代码
  7. MySQL8新特性(2)--mysql的升级过程
  8. 从安装PHP到第一个tomcat执行的hello world其实没那么难
  9. kafka集群部署以及单机部署
  10. sql server exec 参数的传递