热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#连接SQLServer数据库自动生成model类代码

Program.csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threadi

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Windows.Forms;
namespace ModelFactory

{

static class Program

{

///



/// 应用程序的主入口点。

///


[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 tablenames = new List();

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
{

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类代码的相关教程结束。



推荐阅读
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • CentOS 6.5安装VMware Tools及共享文件夹显示问题解决方法
    本文介绍了在CentOS 6.5上安装VMware Tools及解决共享文件夹显示问题的方法。包括清空CD/DVD使用的ISO镜像文件、创建挂载目录、改变光驱设备的读写权限等步骤。最后给出了拷贝解压VMware Tools的操作。 ... [详细]
  • 导出功能protectedvoidbtnExport(objectsender,EventArgse){用来打开下载窗口stringfileName中 ... [详细]
  • Windows7 64位系统安装PLSQL Developer的步骤和注意事项
    本文介绍了在Windows7 64位系统上安装PLSQL Developer的步骤和注意事项。首先下载并安装PLSQL Developer,注意不要安装在默认目录下。然后下载Windows 32位的oracle instant client,并解压到指定路径。最后,按照自己的喜好对解压后的文件进行命名和压缩。 ... [详细]
  • 移动端常用单位——rem的使用方法和注意事项
    本文介绍了移动端常用的单位rem的使用方法和注意事项,包括px、%、em、vw、vh等其他常用单位的比较。同时还介绍了如何通过JS获取视口宽度并动态调整rem的值,以适应不同设备的屏幕大小。此外,还提到了rem目前在移动端的主流地位。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • 在C#中,使用关键字abstract来定义抽象类和抽象方法。抽象类是一种不能被实例化的类,它只提供部分实现,但可以被其他类继承并创建实例。抽象类可以用于类、方法、属性、索引器和事件。在一个类声明中使用abstract表示该类倾向于作为其他类的基类成员被标识为抽象,或者被包含在一个抽象类中,必须由其派生类实现。本文介绍了C#中抽象类和抽象方法的基础知识,并提供了一个示例代码。 ... [详细]
  • C#多线程解决界面卡死问题的完美解决方案
    当界面需要在程序运行中不断更新数据时,使用多线程可以解决界面卡死的问题。一个主线程创建界面,使用一个子线程执行程序并更新主界面,可以避免卡死现象。本文分享了一个例子,供大家参考。 ... [详细]
  • 抽空写了一个ICON图标的转换程序
    抽空写了一个ICON图标的转换程序,支持png\jpe\bmp格式到ico的转换。具体的程序就在下面,如果看的人多,过两天再把思路写一下。 ... [详细]
  • 第一步:PyQt4Designer设计程序界面该部分设计类同VisvalStudio内的设计,改下各部件的objectName!设计 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • ExcelApp#启动excel程序ExcelAppCreateOleObject(“Excel.Application”);#加载文件但不显示文件内容(true表 ... [详细]
author-avatar
___墨浔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有