根据以前的积累,写了一个能生成数据实体类的宏
Imports EnvDTE
Imports System.Diagnostics
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.Text.RegularExpressions
Imports System.Text
'eidolon.lee
'20070111
'根据数据库的表 生成数据实体类
Public Module SqlTools
'光标位置写出表名
Public Sub Table2CShrp()
'得到表名
Dim TableName = GetTableName()
'得到表结构
Dim table As DataTable = GetTableStru(TableName)
'取得当前光标位置
Dim txtSel As TextSelection = DTE.ActiveDocument.Selection
'在光标下插入一行
txtSel.NewLine()
'txtSel.Indent()
Dim first As Boolean = True
Dim i As Integer = 0
Dim iCount As Integer = table.Rows.Count
' Dim cs As String
'命名空间名称
Dim _NameSpace As String
'类名称
Dim _ClassName As String
Dim line As String = " "
If iCount > 0 Then
_NameSpace = InputBox("命名空间名称?")
_ClassName = InputBox("类名称?")
Else
MsgBox("没有列存在", MsgBoxStyle.Information, "提示")
Return
End If
'定义用于存储的stringBuilder
'Dim cs As StringBuilder = New StringBuilder
'Dim csFiled As StringBuilder = New StringBuilder
'Dim csProperty As StringBuilder = New StringBuilder
'处理命名空间和类名
_ClassName = _ClassName + "Model"
'加入命名空间
txtSel.NewLine()
txtSel.Text = "using System;"
txtSel.NewLine()
txtSel.NewLine()
txtSel.Text = "/*"
txtSel.NewLine()
txtSel.Text = "本代码部分为程序自动生成"
txtSel.NewLine()
txtSel.Text = "使用前请检查"
txtSel.NewLine()
txtSel.Text = "生成时间:" + System.DateTime.Now
txtSel.NewLine()
txtSel.Text = "*/"
txtSel.NewLine()
txtSel.Text = "namespace " + _NameSpace + "{"
txtSel.NewLine()
txtSel.NewLine()
'加入类名
txtSel.NewLine()
txtSel.Text = "///<summary>"
txtSel.NewLine()
txtSel.Text = "实体类"
txtSel.NewLine()
txtSel.Text = "</summary>"
txtSel.NewLine()
txtSel.DeleteLeft(3)
txtSel.Text = " public class " + _ClassName + ""
txtSel.NewLine()
txtSel.Text = "{"
txtSel.NewLine()
txtSel.NewLine()
txtSel.Text = "#region 私有字段"
For Each row As DataRow In table.Rows
'数据库的字段类型
Dim SqlType, CSharpType, IsNull As String
'字段名称
Dim name As String
'字段长度
Dim len As Integer
'循环每一行
name = row("COLUMN_NAME")
SqlType = row("TYPE_NAME")
CSharpType = SqlType2CSarpType(SqlType)
len = row.Item("LENGTH")
IsNull = row("IS_NULLABLE")
'加入私有字段
txtSel.NewLine()
txtSel.Text = "///<summary>"
txtSel.NewLine()
txtSel.Text = "私有字段"
txtSel.NewLine()
txtSel.Text = "字段名称:" + name
txtSel.NewLine()
txtSel.Text = "字段类型:" + SqlType
txtSel.NewLine()
txtSel.Text = "字段允许为空:" + IsNull
txtSel.NewLine()
txtSel.Text = "字段长度:" + len.ToString()
txtSel.NewLine()
txtSel.Text = "</summary>"
txtSel.NewLine()
txtSel.DeleteLeft(3)
txtSel.Text = " private " + CSharpType + " _" + name + ";"
txtSel.NewLine()
Next
txtSel.NewLine()
txtSel.Text = "#endregion 私有字段"
txtSel.NewLine()
txtSel.NewLine()
txtSel.Text = "#region 公有属性"
For Each row As DataRow In table.Rows
'数据库的字段类型
Dim SqlType, CSharpType, IsNull As String
'字段名称
Dim name As String
'字段长度
Dim len As Integer
'循环每一行
name = row("COLUMN_NAME")
SqlType = row("TYPE_NAME")
CSharpType = SqlType2CSarpType(SqlType)
len = row.Item("LENGTH")
IsNull = row("IS_NULLABLE")
'加入共有方法
txtSel.NewLine()
txtSel.Text = "///<summary>"
txtSel.NewLine()
txtSel.Text = "公有属性"
txtSel.NewLine()
txtSel.Text = "私有字段"
txtSel.NewLine()
txtSel.Text = "字段名称:" + name
txtSel.NewLine()
txtSel.Text = "字段类型:" + SqlType
txtSel.NewLine()
txtSel.Text = "字段允许为空:" + IsNull
txtSel.NewLine()
txtSel.Text = "字段长度:" + len.ToString()
txtSel.NewLine()
txtSel.Text = "</summary>"
txtSel.NewLine()
txtSel.DeleteLeft(3)
txtSel.Text = " public " + CSharpType + " " + name
txtSel.NewLine()
txtSel.Text = "{"
txtSel.NewLine()
txtSel.Text = "get {"
txtSel.NewLine()
txtSel.Text = " return " + " _" + name + ";"
txtSel.NewLine()
txtSel.Text = "}"
txtSel.NewLine()
txtSel.Text = " set {"
txtSel.NewLine()
txtSel.Text = " _" + name + " = value ;"
txtSel.NewLine()
txtSel.Text = "}"
txtSel.NewLine()
txtSel.Text = "}"
txtSel.NewLine()
txtSel.NewLine()
Next
txtSel.NewLine()
txtSel.Text = "#endregion 公有属性"
txtSel.NewLine()
txtSel.NewLine()
txtSel.Text = " }"
txtSel.NewLine()
txtSel.Text = " }"
txtSel.NewLine()
txtSel.NewLine()
'DTE.UndoContext.Open("Table2CShrp", False)
' Dim activeSelection As TextSelection
' Dim codeInQuestion As String
' activeSelection = CType(DTE.ActiveDocument.Selection, _
' TextSelection)
' With activeSelection
' .Insert(cs.ToString(), _
' vsInsertFlags.vsInsertFlagsContainNewText Or _
' vsInsertFlags.vsInsertFlagsInsertAtStart)
' '.Insert(String.Format("{0}#endregion{0}", vbCrLf), _
' ' vsInsertFlags.vsInsertFlagsInsertAtEnd Or _
' ' vsInsertFlags.vsInsertFlagsContainNewText)
' End With
' DTE.UndoContext.Close()
End Sub
'得到光标处的表名
Private Function GetTableName() As String
Dim txtSel As TextSelection = DTE.ActiveDocument.Selection
txtSel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
txtSel.EndOfLine(True)