废话少说,上图贴代码。
Imports System Imports EnvDTE Imports EnvDTE80 Imports EnvDTE90 Imports EnvDTE90a Imports EnvDTE100 Imports System.Diagnostics Imports System.Data Imports System.Data.SqlClient '' 把数据表生成实体类 (暂只支持SqlServer 2005以上) '' 需要放置一个 Studio.ini 的文件到需要生成实体类的项目的根目录。 内容为 DbConnection = "数据库的链接字符串" '' Author : S.P 开源社区: studio.80y.cn 交流QQ群:1809084 转载请保留此段信息 Public Module SqlToEntity Private Const title As String = "SP.Studio 实体类生成工具" Private Const dbKey As String = "DbConnection = " Public Sub CreateEntity() Dim path As String = DTE.ActiveDocument.FullName Dim dbConnection As String Do While (path.Contains("\")) path = path.Substring(0, path.LastIndexOf("\")) If System.IO.File.Exists(path + "\Studio.ini") Then path += "\Studio.ini" Exit Do End If Loop If Not path.Contains("\") Then MsgBox("未找到配置文件。请将配置文件放入项目的根目录中。", MsgBoxStyle.Critical, title) Exit Sub End If Dim config As String = System.Text.Encoding.UTF8.GetString(System.IO.File.ReadAllBytes(path)) If config.Contains(dbKey) Then config = config.Substring(config.IndexOf(dbKey) + dbKey.Length()) If config.Contains(Chr(13)) Then config = config.Substring(0, config.IndexOf(Chr(13))) End If dbConnection = config.Substring(1, config.Length - 2) Else MsgBox("配置文件中没有配置 """ + dbKey + """的的相关内容", MsgBoxStyle.Critical, title) Exit Sub End If Dim table As String = InputBox("请输入表名", title) If String.IsNullOrEmpty(table) Then MsgBox("没有输入表名", MsgBoxStyle.Critical, title) Exit Sub End If Dim code As String = System.Text.Encoding.UTF8.GetString(System.IO.File.ReadAllBytes(DTE.ActiveDocument.FullName)) Dim line As Integer = DTE.ActiveDocument.Selection.CurrentLine Dim column As Integer = DTE.ActiveDocument.Selection.CurrentColumn DTE.ActiveDocument.Selection.MoveToLineAndOffset(1, 1) If Not DTE.ActiveDocument.Selection.FindText("using System.Data.Linq.Mapping;") Then DTE.ActiveDocument.Selection.FindText(Chr(10) + "namespace ") DTE.ActiveDocument.Selection.StartOfLine() DTE.ActiveDocument.Selection.LineUp() DTE.ActiveDocument.Selection.Text = "using System.Data.Linq.Mapping;" DTE.ActiveDocument.Selection.NewLine() line = line + 1 End If If Not DTE.ActiveDocument.Selection.FindText("[Table(Name") Then If DTE.ActiveDocument.Selection.FindText("class") Then DTE.ActiveDocument.Selection.MoveTo(DTE.ActiveDocument.Selection.CurrentLine, DTE.ActiveDocument.Selection.CurrentColumn - "class".Length) DTE.ActiveDocument.Selection.Insert("public ") DTE.ActiveDocument.Selection.LineUp() DTE.ActiveDocument.Selection.NewLine() DTE.ActiveDocument.Selection.Insert("[Table(Name = """ + table + """)]") line = line + 1 End If End If If DTE.ActiveDocument.Selection.FindText("class") Then DTE.ActiveDocument.Selection.FindText("{") DTE.ActiveDocument.Selection.EndOfLine() End If Dim conn As SqlConnection = New SqlConnection(dbConnection) Dim comm As SqlCommand = New SqlCommand() comm.Connection = conn comm.CommandType = CommandType.StoredProcedure comm.CommandText = "sp_GetTableColumn" comm.Parameters.Add(New SqlParameter("@TableName", table)) Dim ds As DataSet = New DataSet() Dim ada As SqlDataAdapter = New SqlDataAdapter() ada.SelectCommand = comm ada.Fill(ds) conn.Close() conn.Dispose() If ds.Tables().Item(0).Rows.Count() = 0 Then MsgBox("表 " + table + " 不存在", MsgBoxStyle.Critical, title) Exit Sub End If Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder() For Each dr As DataRow In ds.Tables().Item(0).Rows Dim desc As String = dr.Item("字段说明").ToString() Dim field As String = CType(dr.Item("字段名"), String) If Not code.Contains("Column(Name = """ + field + """") Then DTE.ActiveDocument.Selection.NewLine() If Not String.IsNullOrEmpty(desc) Then desc = Replace(desc, Chr(10), " ") desc = Replace(desc, Chr(13), " ") DTE.ActiveDocument.Selection.Text = "///
使用效果:
1、新建一个类。 这一步还是要手工新建的,输入类名。
![实体类生成器,在Visual Studio中自动生成实体类](/Files/20115/5a65f61b-53c2-4862-8f93-f7244aa761c8.jpg)
2、按下运行宏的快捷键。我设置的是Ctrl+E 、Ctrl+D,将会弹出输入表名的对话框
![实体类生成器,在Visual Studio中自动生成实体类](/Files/20115/77a9cc63-47e8-4627-8d74-b25d08864dad.jpg)
3、代码生成完毕。
![实体类生成器,在Visual Studio中自动生成实体类](/Files/20115/df5486f3-77bb-4776-9ee2-2e2c15fcae2f.jpg)
一键化操作。 很简单吧。
该宏的项目下载地址:
http://svn.80y.cn/svn/SP.Studio/V4.2.Plugins/Macros/ 用户名: guest 密码空
最新评论