标签:
SQL语句自动生成工具
大哉乾元 2016/2/26 作者原创转载请注明出处
前言
这个程序是几年前做成的,现在整理成文档和大家分享,当时参与的项目中大量使用的sql语句,所以SqL语句的代码输入占了较大的比例,为了提高sql语句的书写正确性和输入效率,做成了这个自动生成工具。
一:ADO.NET中执行sql文,对应的sql语句嵌入到vb.net的代码内,分析sql的语法格式个特点:
已检索语句为例分析:
A:必须是已select开头
B:检索的字段名(一些系统函数:可选,例如max,sum等)
C:必须要有From
D:表名,如果有多个表则会有表连接,连接格式:表1 left join(inner join,right join)表2 on 关联字段:表1:xxx =表2.xxx
E:检索条件(可选)where关键字
F:检索条件(可选)条件字段和传入的值或参数进行比较。
二:思路整理
A:想要获取检索字段,首先要获取字段所在的表名。
B:可以调用sql server或oracle提供的存储过程来获取当前连接权限内所有的表名,
C:通过一览选择的方式从一览中选择表名,
D:多表连接:可以使用两个一览来选择对应的两张表,连接方式比较固定常用的只有3中方式可以使用下拉框设选择设定,在使用两个一览分别选择对应表中的字段(通过系统提供的存储过程可以获取指定表中字段的信息),在使用一个输入框输入连个字段的对应关系。多次重复就可以实现多个表的两两关联。
E:将要检索的表保存到对应一览中。
F:检索字段的获取,在上步的一览众可以指定需要检索表的表名,更加表名获取表对应字段一览,将需要的字段选定,追加到指定一览保存,这样就获取到所有需要检索的表字段了。
G:检索条件和上一步的处理类似。
H:这样就得到了from中表的信息,select中字段的信息,where条件的信息,根据这些信息,拼凑成一个sql语句就很简单了。
I:将保存信息按照sql语句的语法结构拼凑成一个sql执行语句并附加上一些必要的信息,将这些保存到一个文件中,通过配置方法字段自动生成sql语句。搞定!!!
三:画面展示
化面功能概述
A:在DB连接设定中输入需要连接的数据库的连接信息
B:在表对象中第一部选择一个主表点击追加到上侧的一栏中,如果有表连接则选择连接方式,选择主表和次表,在右侧有三个关联条件,设置完成后后点击追加表将表信息追加到上侧的一览中。
C:在插入,检索,修改的字段区的右侧选择需要处理的表名,在选择字段名,点击追加字段将字段信息追加到左侧的一览中。
D:where条件区,在右侧选择表名,字段名,输入参数名称,点击追加字段,将条件信息追加到左侧的一览中。
E:点击sql文做成,将自动生成配置的sql文件。
F:执行结果
‘SQL文の設定
Dim strSql As StringBuilder = New StringBuilder()
strSql.Append(" SELECT ")
strSql.Append(" RTRIM(txx_200.t001_0010) AS t001_0010 ") ‘xxx区分
strSql.Append(" , RTRIM(txx_200.t200_0010) AS t200_0010 ") ‘xxx_0010
strSql.Append(" , RTRIM(txx_200.t300_2050) AS t300_2050 ") ‘xxx年月日
strSql.Append(" , RTRIM(txx_200.t401_0010) AS t401_0010 ") ‘xxコード
strSql.Append(" FROM ")
strSql.Append(" txx_200 ") ‘xx台帳
strSql.Append(" INNER JOIN txx2_I007 ") ‘xxxマスタ
strSql.Append(" ON txx_200.t200_0010 = txx2_I007.t200_0010 ")
strSql.Append(" WHERE ")
strSql.Append(" txx_200.t001_0010 = @W_t ") ‘xxx区分
‘パラメーターの設定
Dim paras() As SqlParameter = New SqlParameter(0) {}
paras(0) = New SqlParameter("@W_t", SqlDbType.char, 2 ) ‘xxx区分
paras(0).Value = 1 ‘xxx区分 (char)
Try
Return Me.ExecuteDataTable(CommandType.Text, strSql.ToString, paras)
Catch ex As SqlException
Throw New DaoAccessException(ex)
End Try
G:效率分析输入上述代码,手动输入连带代码调试确保正确性大概需要10-20分钟,使用本工具则需要2-3分钟即可,而且保证了正确性和代码书写一致性。效率大幅度提高。
注:这个版本只是初版,只实现了最简单的sql生成。如有需要可以在此基础上追加对应的功能。
四:全部代码
1 Imports System.Data.SqlClient 2 Imports System.IO 3 4 Public Class Form1 5 ‘数据库表检索结果 6 Public dtTable As DataTable = New DataTable 7 ‘table表追加数据 8 Public dtFromTblData As DataTable = New DataTable 9 ‘检索字段追加数据 10 Private dtSelColData As DataTable = New DataTable 11 ‘条件字段追加数据 12 Private dtWhColData As DataTable = New DataTable 13 14 Private strConnectstring As String = "" 15 16 17 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 18 txtDBIp.Text = "192.168.5.14" 19 txtDBName.Text = "TNET_DB" 20 txtDBUser.Text = "sa" 21 txtDBPasswd.Text = "softroad@2013" 22 23 rdoInsert.Checked = True 24 25 cmbTabType.Items.Add("INNER JOIN") 26 cmbTabType.Items.Add("LEFT JOIN") 27 cmbTabType.Items.Add("RIGHT JOIN") 28 29 cmbTabType.Visible = False 30 cmbTabTo.Visible = False 31 cmbTabF1.Visible = False 32 cmbTabF2.Visible = False 33 cmbTabF3.Visible = False 34 cmbTabT1.Visible = False 35 cmbTabT2.Visible = False 36 cmbTabT3.Visible = False 37 38 ‘table from qu 39 dtFromTblData.Columns.Add("d1") 40 dtFromTblData.Columns.Add("d2") 41 dtFromTblData.Columns.Add("d3") 42 dtFromTblData.Columns.Add("d4") 43 dtFromTblData.Columns.Add("d5") 44 dtFromTblData.Columns.Add("d6") 45 dtFromTblData.Columns.Add("d7") 46 grdTabFrom.DataSource = dtFromTblData 47 48 ‘检索字段追加数据() 49 dtSelColData.Columns.Add("d1") 50 dtSelColData.Columns.Add("d2") 51 dtSelColData.Columns.Add("d3") 52 dtSelColData.Columns.Add("d4") 53 dtSelColData.Columns.Add("d5") 54 dtSelColData.Columns.Add("d6") 55 grdSelCol.DataSource = dtSelColData 56 selectDataClear() 57 58 ‘条件字段追加数据() 59 dtWhColData.Columns.Add("d1") 60 dtWhColData.Columns.Add("d2") 61 dtWhColData.Columns.Add("d3") 62 dtWhColData.Columns.Add("d4") 63 dtWhColData.Columns.Add("d5") 64 dtWhColData.Columns.Add("d6") 65 grdWhCol.DataSource = dtWhColData 66 WhereDataClear() 67 End Sub 68 69 Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click 70 Me.Close() 71 End Sub 72 73 Private Sub btnDBTest_Click(sender As Object, e As EventArgs) Handles btnDBTest.Click 74 ‘输入数据的check 75 If String.IsNullOrEmpty(txtDBIp.Text) = True Then 76 MessageBox.Show("请输入数据库主机ip地址") 77 Exit Sub 78 End If 79 If String.IsNullOrEmpty(txtDBName.Text) = True Then 80 MessageBox.Show("请输入数据库名称") 81 Exit Sub 82 End If 83 If String.IsNullOrEmpty(txtDBUser.Text) = True Then 84 MessageBox.Show("请输入用户名") 85 Exit Sub 86 End If 87 If String.IsNullOrEmpty(txtDBPasswd.Text) = True Then 88 MessageBox.Show("请输入密码") 89 Exit Sub 90 End If 91 92 strConnectstring = "Password=" & txtDBPasswd.Text & ";Persist Security Info=True;User ID=" & txtDBUser.Text & ";Initial Catalog=" & txtDBName.Text & ";Data Source=" & txtDBIp.Text 93 94 Dim conn As SqlConnection = Nothing 95 Try 96 conn = New SqlConnection(strConnectstring) 97 conn.Open() 98 If conn.State = ConnectionState.Open Then 99 MessageBox.Show("连接测试正常") 100 End If 101 Catch ex As Exception 102 MessageBox.Show("链接异常:" & ex.Message()) 103 Finally 104 If conn IsNot Nothing Then 105 If conn.State = ConnectionState.Open Then 106 conn.Close() 107 End If 108 End If 109 End Try 110 End Sub 111 112 ‘初始化, 取得数据库表结构 113 Private Sub btnDBGetData_Click(sender As Object, e As EventArgs) Handles btnDBGetData.Click 114 ‘输入数据的check 115 If String.IsNullOrEmpty(txtDBIp.Text) = True Then 116 MessageBox.Show("请输入数据库主机ip地址") 117 Exit Sub 118 End If 119 If String.IsNullOrEmpty(txtDBName.Text) = True Then 120 MessageBox.Show("请输入数据库名称") 121 Exit Sub 122 End If 123 If String.IsNullOrEmpty(txtDBUser.Text) = True Then 124 MessageBox.Show("请输入用户名") 125 Exit Sub 126 End If 127 If String.IsNullOrEmpty(txtDBPasswd.Text) = True Then 128 MessageBox.Show("请输入密码") 129 Exit Sub 130 End If 131 Dim strsql As String = "select c.name,cast(isnull(f.[value], ‘‘) as nvarchar(100)) as remark " _ 132 & "from sys.objects c left join sys.extended_properties f on f.major_id=c.object_id and f.minor_id=0 and f.class=1 " _ 133 & "where c.type=‘u‘ or c.type=‘v‘ order by name" 134 135 dtTable = SelsectDb(strsql) 136 setComBoxData() 137 138 MessageBox.Show("取得数据库表名成功,请在from区域选择需要处理的表") 139 End Sub 140 141 Private Sub cmbTabFrom_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbTabFrom.SelectedIndexChanged 142 GetTableData(1) 143 End Sub 144 145 Private Sub cmbTabTo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbTabTo.SelectedIndexChanged 146 GetTableData(2) 147 End Sub 148 149 ‘表追加按钮 150 Private Sub btnAddTable_Click(sender As Object, e As EventArgs) Handles btnAddTable.Click 151 FromTableAdd() 152 End Sub 153 154 155 ‘设置字段 156 Private Sub btnSelClear_Click(sender As Object, e As EventArgs) Handles btnSelClear.Click 157 selectDataClear() 158 End Sub 159 160 Private Sub btnSelAdd_Click(sender As Object, e As EventArgs) Handles btnSelAdd.Click 161 selectAddData() 162 End Sub 163 164 Private Sub cmbSelTabName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbSelTabName.SelectedIndexChanged 165 selectTabData() 166 End Sub 167 168 Private Sub cmbSelTabColName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbSelTabColName.SelectedIndexChanged 169 selectTabColData() 170 End Sub 171 172 ‘设置wher条件 173 174 Private Sub btnWhClear_Click(sender As Object, e As EventArgs) Handles btnWhClear.Click 175 WhereDataClear() 176 End Sub 177 178 Private Sub btnWhAdd_Click(sender As Object, e As EventArgs) Handles btnWhAdd.Click 179 WhereAddData() 180 End Sub 181 182 Private Sub cmbWhTabName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbWhTabName.SelectedIndexChanged 183 WhereTabData() 184 End Sub 185 186 Private Sub cmbWhTabColName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbWhTabColName.SelectedIndexChanged 187 WhereTabColData() 188 End Sub 189 190 191 ‘画面清空 192 Private Sub btnclear_Click(sender As Object, e As EventArgs) Handles btnclear.Click 193 194 ClearAll() 195 End Sub 196 197 Private Sub rdoInsert_CheckedChanged(sender As Object, e As EventArgs) Handles rdoInsert.CheckedChanged 198 ClearAll() 199 End Sub 200 201 Private Sub rdoDelete_CheckedChanged(sender As Object, e As EventArgs) Handles rdoDelete.CheckedChanged 202 ClearAll() 203 End Sub 204 205 Private Sub rdoUpdate_CheckedChanged(sender As Object, e As EventArgs) Handles rdoUpdate.CheckedChanged 206 ClearAll() 207 End Sub 208 209 Private Sub rdoSelect_CheckedChanged(sender As Object, e As EventArgs) Handles rdoSelect.CheckedChanged 210 ClearAll() 211 End Sub 212 213 214 ‘sql文做成 215 Private Sub btnZuocheng_Click(sender As Object, e As EventArgs) Handles btnZuocheng.Click 216 sqlSourceSakusei() 217 End Sub 218 219 220 ‘处理函数 221 222 #Region "表连接设置" 223 ‘按照取得的表结构,初始画面显示表名的控件 224 Private Sub setComBoxData() 225 Dim dTabFrom As DataTable = Nothing 226 Dim dTabTo As DataTable = Nothing 227 dTabFrom = dtTable.Copy 228 dTabTo = dtTable.Copy 229 dTabFrom.Columns.Add("ID") 230 dTabTo.Columns.Add("ID") 231 dTabFrom.Columns.Add("SHOWNAME") 232 dTabTo.Columns.Add("SHOWNAME") 233 For irow = 0 To dtTable.Rows.Count - 1 234 dTabFrom.Rows(irow)("ID") = irow 235 dTabTo.Rows(irow)("ID") = irow 236 237 dTabFrom.Rows(irow)("SHOWNAME") = dtTable.Rows(irow)(0) & " " & dtTable.Rows(irow)(1) 238 dTabTo.Rows(irow)("SHOWNAME") = dtTable.Rows(irow)(0) & " " & dtTable.Rows(irow)(1) 239 Next 240 241 cmbTabFrom.ValueMember = "ID" 242 cmbTabFrom.DisplayMember = "SHOWNAME" 243 cmbTabFrom.DataSource = dTabFrom 244 cmbTabFrom.SelectedIndex = -1 245 cmbTabTo.ValueMember = "ID" 246 cmbTabTo.DisplayMember = "SHOWNAME" 247 cmbTabTo.DataSource = dTabTo 248 cmbTabTo.SelectedIndex = -1 249 End Sub 250 251 Private Sub GetTableData(ByVal mode As Integer) 252 Dim strTable As String 253 Dim dt As DataTable 254 Dim strTableName As String = "" 255 256 If mode = 1 Then 257 dt = cmbTabFrom.DataSource 258 strTable = cmbTabFrom.SelectedValue 259 If dt Is Nothing Or String.IsNullOrEmpty(strTable) = True Then 260 Exit Sub 261 End If 262 strTableName = dt.Rows(CInt(strTable))("name") 263 ElseIf mode = 2 Then 264 265 dt = cmbTabTo.DataSource 266 strTable = cmbTabTo.SelectedValue 267 If dt Is Nothing Or String.IsNullOrEmpty(strTable) = True Then 268 Exit Sub 269 End If 270 strTableName = dt.Rows(CInt(strTable))("name") 271 End If 272 273 Dim strsql As String = "" 274 275 strsql = strsql & "select a.name, a.max_length as len , a.precision , a.scale , " 276 strsql = strsql & "cast(isnull(e.[value],‘‘) as nvarchar(100)) as remark, f.name as typename," 277 strsql = strsql & " a.column_id -1 as ID, a.name + ‘ ‘ + cast(isnull(e.[value],‘‘) as nvarchar(100)) as SHOWNAME " 278 strsql = strsql & " from sys.columns a inner join sys.objects c on a.object_id=c.object_id and ( c.type=‘u‘ or c.type=‘v‘) " 279 strsql = strsql & " left join sys.extended_properties e on e.major_id=c.object_id " 280 strsql = strsql & " and e.minor_id=a.column_id and e.class=1 " 281 strsql = strsql & " left join sys.types f on f.system_type_id= a.system_type_id " 282 strsql = strsql & " where c.name=‘" & strTableName & "‘" 283 strsql = strsql & " order by ID" 284 285 Dim dtcol As DataTable 286 dtcol = SelsectDb(strsql) 287 288 If mode = 1 Then 289 Dim dt1 As DataTable 290 Dim dt2 As DataTable 291 Dim dt3 As DataTable 292 293 dt1 = dtcol.Copy 294 cmbTabF1.ValueMember = "ID" 295 cmbTabF1.DisplayMember = "SHOWNAME" 296 cmbTabF1.DataSource = dt1 297 cmbTabF1.SelectedIndex = -1 298 299 dt2 = dtcol.Copy 300 cmbTabF2.ValueMember = "ID" 301 cmbTabF2.DisplayMember = "SHOWNAME" 302 cmbTabF2.DataSource = dt2 303 cmbTabF2.SelectedIndex = -1 304 305 dt3 = dtcol.Copy 306 cmbTabF3.ValueMember = "ID" 307 cmbTabF3.DisplayMember = "SHOWNAME" 308 cmbTabF3.DataSource = dt3 309 cmbTabF3.SelectedIndex = -1 310 ElseIf mode = 2 Then 311 312 Dim dt1 As DataTable 313 Dim dt2 As DataTable 314 Dim dt3 As DataTable 315 316 dt1 = dtcol.Copy 317 cmbTabT1.ValueMember = "ID" 318 cmbTabT1.DisplayMember = "SHOWNAME" 319 cmbTabT1.DataSource = dt1 320 cmbTabT1.SelectedIndex = -1 321 322 dt2 = dtcol.Copy 323 cmbTabT2.ValueMember = "ID" 324 cmbTabT2.DisplayMember = "SHOWNAME" 325 cmbTabT2.DataSource = dt2 326 cmbTabT2.SelectedIndex = -1 327 328 dt3 = dtcol.Copy 329 cmbTabT3.ValueMember = "ID" 330 cmbTabT3.DisplayMember = "SHOWNAME" 331 cmbTabT3.DataSource = dt3 332 cmbTabT3.SelectedIndex = -1 333 End If 334 End Sub 335 336 ‘追加选择的表和表连接关系 337 Private Sub FromTableAdd() 338 Dim dt As DataTable = New DataTable() 339 If dtFromTblData.Rows.Count <= 0 Then 340 If cmbTabFrom.SelectedIndex < 0 Then 341 MessageBox.Show("请选择表F项,指定操作的表") 342 Exit Sub 343 Else 344 Dim dr As DataRow = dtFromTblData.NewRow 345 dt = cmbTabFrom.DataSource 346 dr("d1") = "主表" 347 dr("d2") = dt.Rows(CInt(cmbTabFrom.SelectedValue))("name") 348 dr("d4") = dt.Rows(CInt(cmbTabFrom.SelectedValue))("remark") 349 dtFromTblData.Rows.Add(dr) 350 351 dt.Clear() 352 For i As Integer = 0 To dtFromTblData.Rows.Count - 1 353 Dim strFromTabName As String 354 strFromTabName = dtFromTblData.Rows(i)("d2") 355 356 For j As Integer = 0 To dtTable.Rows.Count - 1 357 Dim strToTabName = dtTable.Rows(j)("Name") 358 If strFromTabName = strToTabName Then 359 Dim dr1 As DataRow = dt.NewRow 360 dr1("name") = strFromTabName 361 dr1("remark") = dtTable.Rows(j)("remark") 362 dr1("ID") = i.ToString() 363 dr1("SHOWNAME") = dtTable.Rows(j)(0) & " " & dtTable.Rows(j)(1) 364 dt.Rows.Add(dr1) 365 End If 366 367 Next 368 Next 369 cmbTabFrom.SelectedIndex = -1 370 371 Dim dtSelTab As DataTable 372 dtSelTab = cmbTabFrom.DataSource.copy() 373 374 cmbSelTabName.ValueMember = "ID" 375 cmbSelTabName.DisplayMember = "SHOWNAME" 376 cmbSelTabName.DataSource = dtSelTab 377 cmbSelTabName.SelectedIndex = -1 378 379 380 Dim dtWhTab As DataTable 381 dtWhTab = cmbTabFrom.DataSource.copy() 382 383 cmbWhTabName.ValueMember = "ID" 384 cmbWhTabName.DisplayMember = "SHOWNAME" 385 cmbWhTabName.DataSource = dtWhTab 386 cmbWhTabName.SelectedIndex = -1 387 388 If rdoSelect.Checked = True Then 389 cmbTabType.Visible = True 390 cmbTabTo.Visible = True 391 cmbTabF1.Visible = True 392 cmbTabF2.Visible = True 393 cmbTabF3.Visible = True 394 cmbTabT1.Visible = True 395 cmbTabT2.Visible = True 396 cmbTabT3.Visible = True 397 End If 398 End If 399 Else 400 If rdoDelete.Checked = True Or rdoInsert.Checked = True Or rdoUpdate.Checked = True Then 401 MessageBox.Show("插入,删除,更新操作只能选择一个表") 402 Exit Sub 403 Else 404 405 If cmbTabType.SelectedIndex < 0 Then 406 MessageBox.Show("请选择表连接的类型") 407 Exit Sub 408 End If 409 410 If cmbTabFrom.SelectedIndex < 0 Then 411 MessageBox.Show("请选择连接的主表") 412 Exit Sub 413 End If 414 415 If cmbTabTo.SelectedIndex < 0 Then 416 MessageBox.Show("请选择连接的子表") 417 Exit Sub 418 End If 419 420 If (cmbTabF1.SelectedIndex < 0 And _ 421 cmbTabF2.SelectedIndex < 0 And _ 422 cmbTabF3.SelectedIndex < 0 And _ 423 cmbTabT1.SelectedIndex < 0 And _ 424 cmbTabT2.SelectedIndex < 0 And _ 425 cmbTabT3.SelectedIndex < 0) Then 426 MessageBox.Show("请选择连接的字段对应关系,最多选择3组") 427 Exit Sub 428 End If 429 430 If (cmbTabF1.SelectedIndex < 0 And _ 431 cmbTabT1.SelectedIndex > 0) Or _ 432 (cmbTabF1.SelectedIndex > 0 And _ 433 cmbTabT1.SelectedIndex < 0) Then 434 MessageBox.Show("请选择正确选择第一组的连接关系") 435 Exit Sub 436 End If 437 438 If (cmbTabF2.SelectedIndex < 0 And _ 439 cmbTabT2.SelectedIndex > 0) Or _ 440 (cmbTabF2.SelectedIndex > 0 And _ 441 cmbTabT2.SelectedIndex < 0) Then 442 MessageBox.Show("请选择正确选择第二组的连接关系") 443 Exit Sub 444 End If 445 446 If (cmbTabF3.SelectedIndex < 0 And _ 447 cmbTabT3.SelectedIndex > 0) Or _ 448 (cmbTabF3.SelectedIndex > 0 And _ 449 cmbTabT3.SelectedIndex < 0) Then 450 MessageBox.Show("请选择正确选择第三组的连接关系") 451 Exit Sub 452 End If 453 454 Dim dr As DataRow = dtFromTblData.NewRow 455 Dim strFromTabName As String 456 Dim strToTabName As String 457 Dim strTemp As String 458 459 dr("d1") = cmbTabType.Text 460 strFromTabName = cmbTabFrom.DataSource.Rows(CInt(cmbTabFrom.SelectedValue))("name") 461 dr("d2") = strFromTabName 462 strToTabName = cmbTabTo.DataSource.Rows(CInt(cmbTabTo.SelectedValue))("name") 463 dr("d3") = strToTabName 464 dr("d4") = cmbTabTo.DataSource.Rows(CInt(cmbTabTo.SelectedValue))("remark") 465 If cmbTabF1.SelectedIndex >= 0 Then 466 strTemp = strFromTabName & "." & cmbTabF1.DataSource.rows(CInt(cmbTabF1.SelectedIndex))("name") & " = " 467 strTemp = strTemp & strToTabName & "." & cmbTabT1.DataSource.rows(CInt(cmbTabT1.SelectedIndex))("name") 468 dr("d5") = strTemp 469 End If 470 471 If cmbTabF2.SelectedIndex >= 0 Then 472 strTemp = strFromTabName & "." & cmbTabF2.DataSource.rows(CInt(cmbTabF2.SelectedIndex))("name") & " = " 473 strTemp = strTemp & strToTabName & "." & cmbTabT2.DataSource.rows(CInt(cmbTabT2.SelectedIndex))("name") 474 dr("d6") = strTemp 475 End If 476 If cmbTabF3.SelectedIndex >= 0 Then 477 strTemp = strFromTabName & "." & cmbTabF3.DataSource.rows(CInt(cmbTabF3.SelectedIndex))("name") & " = " 478 strTemp = strTemp & strToTabName & "." & cmbTabT3.DataSource.rows(CInt(cmbTabT3.SelectedIndex))("name") 479 dr("d7") = strTemp 480 End If 481 482 dtFromTblData.Rows.Add(dr) 483 484 dt = cmbTabFrom.DataSource 485 dt.Clear() 486 For i As Integer = 0 To dtFromTblData.Rows.Count - 1 487 Dim strFromTabName1 As String 488 If i = 0 Then 489 strFromTabName1 = dtFromTblData.Rows(i)("d2") 490 Else 491 strFromTabName1 = dtFromTblData.Rows(i)("d3") 492 End If 493 For j As Integer = 0 To dtTable.Rows.Count - 1 494 Dim strToTabName1 = dtTable.Rows(j)("Name") 495 If strFromTabName1 = strToTabName1 Then 496 Dim dr1 As DataRow = dt.NewRow 497 dr1("name") = strFromTabName1 498 dr1("remark") = dtTable.Rows(j)("remark") 499 dr1("ID") = i.ToString() 500 dr1("SHOWNAME") = dtTable.Rows(j)(0) & " " & dtTable.Rows(j)(1) 501 dt.Rows.Add(dr1) 502 End If 503 504 Next 505 Next 506 507 508 cmbTabType.SelectedIndex = -1 509 cmbTabFrom.SelectedIndex = -1 510 cmbTabTo.SelectedIndex = -1 511 512 cmbTabF1.SelectedIndex = -1 513 cmbTabF2.SelectedIndex = -1 514 cmbTabF3.SelectedIndex = -1 515 cmbTabT1.SelectedIndex = -1 516 cmbTabT2.SelectedIndex = -1 517 cmbTabT3.SelectedIndex = -1 518 519 520 Dim dtSelTab As DataTable 521 dtSelTab = cmbTabFrom.DataSource.copy() 522 523 cmbSelTabName.ValueMember = "ID" 524 cmbSelTabName.DisplayMember = "SHOWNAME" 525 cmbSelTabName.DataSource = dtSelTab 526 cmbSelTabName.SelectedIndex = -1 527 528 cmbSelTabColName.SelectedIndex = -1 529 txtSelColType.Text = "" 530 txtSelColLen1.Text = "" 531 txtSelColLen2.Text = "" 532 txtSelColParmant.Text = "" 533 534 Dim dtWhTab As DataTable 535 dtWhTab = cmbTabFrom.DataSource.copy() 536 537 cmbWhTabName.ValueMember = "ID" 538 cmbWhTabName.DisplayMember = "SHOWNAME" 539 cmbWhTabName.DataSource = dtWhTab 540 cmbWhTabName.SelectedIndex = -1 541 542 txtWhColType.Text = "" 543 txtWhColLen1.Text = "" 544 txtWhColLen2.Text = "" 545 txtWhColParmant.Text = "" 546 End If 547 End If 548 End Sub 549 550 #End Region 551 552 #Region "字段设置" 553 554 ‘清空 555 Private Sub selectDataClear() 556 Dim dtsel As DataTable 557 dtsel = grdSelCol.DataSource 558 If dtsel IsNot Nothing Then 559 dtsel.Clear() 560 End If 561 dtsel = cmbSelTabName.DataSource 562 If dtsel IsNot Nothing Then 563 dtsel.Clear() 564 For i As Integer = 0 To dtFromTblData.Rows.Count - 1 565 Dim strFromTabName1 As String 566 If i = 0 Then 567 strFromTabName1 = dtFromTblData.Rows(i)("d2") 568 Else 569 strFromTabName1 = dtFromTblData.Rows(i)("d3") 570 End If 571 For j As Integer = 0 To dtTable.Rows.Count - 1 572 Dim strToTabName1 = dtTable.Rows(j)("Name") 573 If strFromTabName1 = strToTabName1 Then 574 Dim dr1 As DataRow = dtsel.NewRow 575 dr1("name") = strFromTabName1 576 dr1("remark") = dtTable.Rows(j)("remark") 577 dr1("ID") = i.ToString() 578 dr1("SHOWNAME") = dtTable.Rows(j)(0) & " " & dtTable.Rows(j)(1) 579 dtsel.Rows.Add(dr1) 580 End If 581 582 Next 583 Next 584 585 End If 586 cmbSelTabName.SelectedIndex = -1 587 dtsel = cmbSelTabColName.DataSource 588 If dtsel IsNot Nothing Then 589 dtsel.Clear() 590 591 End If 592 txtSelColType.Text = "" 593 txtSelColLen1.Text = "" 594 txtSelColLen2.Text = "" 595 txtSelColParmant.Text = "" 596 End Sub 597 598 ‘选择表 599 Private Sub selectTabData() 600 Dim strTable As String 601 Dim dt As DataTable 602 Dim strTableName As String = "" 603 604 dt = cmbSelTabName.DataSource 605 strTable = cmbSelTabName.SelectedValue 606 If dt Is Nothing Or String.IsNullOrEmpty(strTable) = True Then 607 Exit Sub 608 End If 609 strTableName = dt.Rows(CInt(strTable))("name") 610 611 Dim strsql As String = "" 612 613 strsql = strsql & "select a.name, a.max_length as len , a.precision , a.scale , " 614 strsql = strsql & "cast(isnull(e.[value],‘‘) as nvarchar(100)) as remark, f.name as typename," 615 strsql = strsql & " a.column_id -1 as ID, a.name + ‘ ‘ + cast(isnull(e.[value],‘‘) as nvarchar(100)) as SHOWNAME " 616 strsql = strsql & " from sys.columns a inner join sys.objects c on a.object_id=c.object_id and ( c.type=‘u‘ or c.type=‘v‘) " 617 strsql = strsql & " left join sys.extended_properties e on e.major_id=c.object_id " 618 strsql = strsql & " and e.minor_id=a.column_id and e.class=1 " 619 strsql = strsql & " left join sys.types f on f.system_type_id= a.system_type_id " 620 strsql = strsql & " where c.name=‘" & strTableName & "‘" 621 strsql = strsql & " order by ID" 622 623 Dim dtcol As DataTable 624 dtcol = SelsectDb(strsql) 625 626 Dim dt1 As DataTable 627 628 629 dt1 = dtcol.Copy 630 cmbSelTabColName.ValueMember = "ID" 631 cmbSelTabColName.DisplayMember = "SHOWNAME" 632 cmbSelTabColName.DataSource = dt1 633 cmbSelTabColName.SelectedIndex = -1 634 635 txtSelColType.Text = "" 636 txtSelColLen1.Text = "" 637 txtSelColLen2.Text = "" 638 txtSelColParmant.Text = "" 639 End Sub 640 641 ‘选择表字段 642 Private Sub selectTabColData() 643 Dim dtcol As DataTable 644 dtcol = cmbSelTabColName.DataSource 645 If dtcol Is Nothing Then 646 Exit Sub 647 End If 648 Dim iIndex As Integer 649 If cmbSelTabColName.SelectedIndex < 0 Then 650 Exit Sub 651 End If 652 653 iIndex = CInt(cmbSelTabColName.SelectedIndex) 654 Dim strType As String = dtcol.Rows(iIndex)("typename") 655 656 txtSelColType.Text = strType 657 strType = strType.ToUpper() 658 If strType = "numeric".ToUpper() Then 659 txtSelColLen1.Text = dtcol.Rows(iIndex)("precision") 660 txtSelColLen2.Text = dtcol.Rows(iIndex)("scale") 661 ElseIf strType = "datetime".ToUpper() Then 662 txtSelColLen1.Text = "" 663 txtSelColLen2.Text = "" 664 Else 665 txtSelColLen1.Text = dtcol.Rows(iIndex)("len") 666 txtSelColLen2.Text = "" 667 End If 668 End Sub 669 670 ‘增加检索字段 671 Private Sub selectAddData() 672 If rdoDelete.Checked = True Then 673 MessageBox.Show("删除数据时,不需要设定字段") 674 Exit Sub 675 End If 676 677 If cmbSelTabName.SelectedIndex < 0 Then 678 MessageBox.Show("请选择检索处理的表") 679 Exit Sub 680 End If 681 682 If cmbSelTabColName.SelectedIndex < 0 Then 683 MessageBox.Show("请选择检索处理的字段") 684 Exit Sub 685 End If 686 687 If (rdoInsert.Checked = True Or rdoUpdate.Checked = True) And chkSelCol.Checked = True Then 688 txtSelColParmant.Text = cmbSelTabColName.DataSource.rows(CInt(cmbSelTabColName.SelectedValue))("name") 689 If txtSelColParmant.Text.Substring(0, 1) <> "@" Then 690 txtSelColParmant.Text = "@" & txtSelColParmant.Text 691 End If 692 End If 693 694 If (rdoInsert.Checked = True Or rdoUpdate.Checked = True) And chkSelCol.Checked = False Then 695 If txtSelColParmant.Text = "" Then 696 If MsgBox("没有输入字段的参数变量,如果不设置参数,默认使用变量名作为参数 " & vbCrLf & "是否登录该字段", MsgBoxStyle.YesNo, ) = MsgBoxResult.No Then 697 Exit Sub 698 Else 699 txtSelColParmant.Text = cmbSelTabColName.DataSource.rows(CInt(cmbSelTabColName.SelectedValue))("name") 700 End If 701 End If 702 If txtSelColParmant.Text.Substring(0, 1) <> "@" Then 703 txtSelColParmant.Text = "@" & txtSelColParmant.Text 704 End If 705 End If 706 707 Dim mode As Boolean = False 708 Dim index As Integer 709 Dim strSelTabName As String 710 Dim strSelTabColName As String 711 712 Dim dtGrid As DataTable 713 dtGrid = grdSelCol.DataSource 714 715 strSelTabName = cmbSelTabName.DataSource.rows(CInt(cmbSelTabName.SelectedValue))("name") 716 strSelTabColName = cmbSelTabColName.DataSource.rows(CInt(cmbSelTabColName.SelectedValue))("name") 717 718 For index = 0 To grdSelCol.Rows.Count - 1 719 If dtGrid.Rows(index)("d1") = strSelTabName And dtGrid.Rows(index)("d2") = strSelTabColName Then 720 mode = True 721 Exit For 722 End If 723 Next 724 725 If mode = True Then 726 dtGrid.Rows(index)("d3") = cmbSelTabColName.DataSource.rows(CInt(cmbSelTabColName.SelectedValue))("remark") 727 dtGrid.Rows(index)("d4") = txtSelColType.Text 728 dtGrid.Rows(index)("d5") = txtSelColLen1.Text 729 dtGrid.Rows(index)("d6") = txtSelColParmant.Text 730 Else 731 Dim dr As DataRow = dtGrid.NewRow 732 dr("d1") = strSelTabName 733 dr("d2") = strSelTabColName 734 dr("d3") = cmbSelTabColName.DataSource.rows(CInt(cmbSelTabColName.SelectedValue))("remark") 735 dr("d4") = txtSelColType.Text 736 dr("d5") = txtSelColLen1.Text 737 dr("d6") = txtSelColParmant.Text 738 dtGrid.Rows.Add(dr) 739 End If 740 txtSelColParmant.Text = "" 741 End Sub 742 #End Region 743 744 #Region "where 条件字段设置" 745 746 ‘清空 747 Private Sub WhereDataClear() 748 Dim dtWh As DataTable 749 dtWh = grdWhCol.DataSource 750 If dtWh IsNot Nothing Then 751 dtWh.Clear() 752 End If 753 dtWh = cmbWhTabName.DataSource 754 If dtWh IsNot Nothing Then 755 dtWh.Clear() 756 For i As Integer = 0 To dtFromTblData.Rows.Count - 1 757 Dim strFromTabName1 As String 758 If i = 0 Then 759 strFromTabName1 = dtFromTblData.Rows(i)("d2") 760 Else 761 strFromTabName1 = dtFromTblData.Rows(i)("d3") 762 End If 763 For j As Integer = 0 To dtTable.Rows.Count - 1 764 Dim strToTabName1 = dtTable.Rows(j)("Name") 765 If strFromTabName1 = strToTabName1 Then 766 Dim dr1 As DataRow = dtWh.NewRow 767 dr1("name") = strFromTabName1 768 dr1("remark") = dtTable.Rows(j)("remark") 769 dr1("ID") = i.ToString() 770 dr1("SHOWNAME") = dtTable.Rows(j)(0) & " " & dtTable.Rows(j)(1) 771 dtWh.Rows.Add(dr1) 772 End If 773 774 Next 775 Next 776 777 End If 778 cmbWhTabName.SelectedIndex = -1 779 dtWh = cmbWhTabColName.DataSource 780 If dtWh IsNot Nothing Then 781 dtWh.Clear() 782 783 End If 784 txtWhColType.Text = "" 785 txtWhColLen1.Text = "" 786 txtWhColLen2.Text = "" 787 txtWhColParmant.Text = "" 788 End Sub 789 790 ‘选择表 791 Private Sub WhereTabData() 792 Dim strTable As String 793 Dim dt As DataTable 794 Dim strTableName As String = "" 795 796 dt = cmbWhTabName.DataSource 797 strTable = cmbWhTabName.SelectedValue 798 If dt Is Nothing Or String.IsNullOrEmpty(strTable) = True Then 799 Exit Sub 800 End If 801 strTableName = dt.Rows(CInt(strTable))("name") 802 803 Dim strsql As String = "" 804 805 strsql = strsql & "select a.name, a.max_length as len , a.precision , a.scale , " 806 strsql = strsql & "cast(isnull(e.[value],‘‘) as nvarchar(100)) as remark, f.name as typename," 807 strsql = strsql & " a.column_id -1 as ID, a.name + ‘ ‘ + cast(isnull(e.[value],‘‘) as nvarchar(100)) as SHOWNAME " 808 strsql = strsql & " from sys.columns a inner join sys.objects c on a.object_id=c.object_id and ( c.type=‘u‘ or c.type=‘v‘) " 809 strsql = strsql & " left join sys.extended_properties e on e.major_id=c.object_id " 810 strsql = strsql & " and e.minor_id=a.column_id and e.class=1 " 811 strsql = strsql & " left join sys.types f on f.system_type_id= a.system_type_id " 812 strsql = strsql & " where c.name=‘" & strTableName & "‘" 813 strsql = strsql & " order by ID" 814 815 Dim dtcol As DataTable 816 dtcol = SelsectDb(strsql) 817 818 Dim dt1 As DataTable 819 820 821 dt1 = dtcol.Copy 822 cmbWhTabColName.ValueMember = "ID" 823 cmbWhTabColName.DisplayMember = "SHOWNAME" 824 cmbWhTabColName.DataSource = dt1 825 cmbWhTabColName.SelectedIndex = -1 826 827 txtWhColType.Text = "" 828 txtWhColLen1.Text = "" 829 txtWhColLen2.Text = "" 830 txtWhColParmant.Text = "" 831 End Sub 832 833 ‘选择表字段 834 Private Sub WhereTabColData() 835 Dim dtcol As DataTable 836 dtcol = cmbWhTabColName.DataSource 837 If dtcol Is Nothing Then 838 Exit Sub 839 End If 840 Dim iIndex As Integer 841 If cmbWhTabColName.SelectedIndex < 0 Then 842 Exit Sub 843 End If 844 845 iIndex = CInt(cmbWhTabColName.SelectedValue) 846 Dim strType As String = dtcol.Rows(iIndex)("typename") 847 848 txtWhColType.Text = strType 849 strType = strType.ToUpper() 850 If strType = "numeric".ToUpper() Then 851 txtWhColLen1.Text = dtcol.Rows(iIndex)("precision") 852 txtWhColLen2.Text = dtcol.Rows(iIndex)("scale") 853 ElseIf strType = "datetime".ToUpper() Then 854 txtWhColLen1.Text = "" 855 txtWhColLen2.Text = "" 856 Else 857 txtWhColLen1.Text = dtcol.Rows(iIndex)("len") 858 txtWhColLen2.Text = "" 859 End If 860 End Sub 861 862 ‘增加检索字段 863 Private Sub WhereAddData() 864 If rdoInsert.Checked = True Then 865 MessageBox.Show("登录数据时,不需要条件字段") 866 Exit Sub 867 End If 868 869 If cmbWhTabName.SelectedIndex < 0 Then 870 MessageBox.Show("请选择条件处理的表") 871 Exit Sub 872 End If 873 874 If cmbWhTabColName.SelectedIndex < 0 Then 875 MessageBox.Show("请选择条件处理的字段") 876 Exit Sub 877 End If 878 879 If txtWhColParmant.Text = "" And chkWhCol.Checked = True Then 880 txtWhColParmant.Text = cmbWhTabColName.DataSource.rows(CInt(cmbWhTabColName.SelectedValue))("name") 881 End If 882 883 If txtWhColParmant.Text = "" And chkWhCol.Checked = False Then 884 If MsgBox("没有输入字段的参数变量,如果不设置参数,默认使用变量名作为参数 " & vbCrLf & "是否登录该字段", MsgBoxStyle.YesNo, ) = MsgBoxResult.No Then 885 Exit Sub 886 Else 887 txtWhColParmant.Text = cmbWhTabColName.DataSource.rows(CInt(cmbWhTabColName.SelectedValue))("name") 888 End If 889 End If 890 If txtWhColParmant.Text.Substring(0, 1) <> "@" Then 891 txtWhColParmant.Text = "@W_" & txtWhColParmant.Text 892 End If 893 894 Dim mode As Boolean = False 895 Dim index As Integer 896 Dim strWhTabName As String 897 Dim strWhTabColName As String 898 899 Dim dtGrid As DataTable 900 dtGrid = grdWhCol.DataSource 901 902 strWhTabName = cmbWhTabName.DataSource.rows(CInt(cmbWhTabName.SelectedValue))("name") 903 strWhTabColName = cmbWhTabColName.DataSource.rows(CInt(cmbWhTabColName.SelectedValue))("name") 904 905 For index = 0 To grdWhCol.Rows.Count - 1 906 If dtGrid.Rows(index)("d1") = strWhTabName And dtGrid.Rows(index)("d2") = strWhTabColName Then 907 mode = True 908 Exit For 909 End If 910 Next 911 912 If mode = True Then 913 dtGrid.Rows(index)("d3") = cmbWhTabColName.DataSource.rows(CInt(cmbWhTabColName.SelectedValue))("remark") 914 dtGrid.Rows(index)("d4") = txtWhColType.Text 915 dtGrid.Rows(index)("d5") = txtWhColLen1.Text 916 dtGrid.Rows(index)("d6") = txtWhColParmant.Text 917 Else 918 Dim dr As DataRow = dtGrid.NewRow 919 dr("d1") = strWhTabName 920 dr("d2") = strWhTabColName 921 dr("d3") = cmbWhTabColName.DataSource.rows(CInt(cmbWhTabColName.SelectedValue))("remark") 922 dr("d4") = txtWhColType.Text 923 dr("d5") = txtWhColLen1.Text 924 dr("d6") = txtWhColParmant.Text 925 dtGrid.Rows.Add(dr) 926 End If 927 txtWhColParmant.Text = "" 928 End Sub 929 #End Region 930 931 #Region "整体按钮" 932 ‘清空 933 Private Sub ClearAll() 934 935 cmbTabType.Visible = False 936 cmbTabTo.Visible = False 937 cmbTabF1.Visible = False 938 cmbTabF2.Visible = False 939 cmbTabF3.Visible = False 940 cmbTabT1.Visible = False 941 cmbTabT2.Visible = False 942 cmbTabT3.Visible = False 943 944 Dim dTabFrom As DataTable = Nothing 945 dTabFrom = dtTable.Copy 946 dTabFrom.Columns.Add("ID") 947 dTabFrom.Columns.Add("SHOWNAME") 948 For irow = 0 To dtTable.Rows.Count - 1 949 dTabFrom.Rows(irow)("ID") = irow 950 951 dTabFrom.Rows(irow)("SHOWNAME") = dtTable.Rows(irow)(0) & " " & dtTable.Rows(irow)(1) 952 Next 953 954 cmbTabFrom.ValueMember = "ID" 955 cmbTabFrom.DisplayMember = "SHOWNAME" 956 cmbTabFrom.DataSource = dTabFrom 957 cmbTabFrom.SelectedIndex = -1 958 959 cmbTabType.SelectedIndex = -1 960 cmbTabFrom.SelectedIndex = -1 961 cmbTabTo.SelectedIndex = -1 962 cmbTabF1.SelectedIndex = -1 963 cmbTabF2.SelectedIndex = -1 964 cmbTabF3.SelectedIndex = -1 965 cmbTabT1.SelectedIndex = -1 966 cmbTabT2.SelectedIndex = -1 967 cmbTabT3.SelectedIndex = -1 968 Dim dtFrom As DataTable 969 dtFrom = grdTabFrom.DataSource 970 If dtFrom IsNot Nothing Then 971 dtFrom.Clear() 972 End If 973 974 selectDataClear() 975 WhereDataClear() 976 End Sub 977 978 Private intParmat As Integer = 0 979 Private strParmat As String 980 Private strParmatValue As String 981 982 ‘sql文做成 983 Private Sub sqlSourceSakusei() 984 If grdTabFrom.Rows.Count <= 0 Then 985 MessageBox.Show("请选择处理的from的表对象") 986 Exit Sub 987 End If 988 989 If rdoDelete.Checked = False Then 990 If grdSelCol.Rows.Count <= 0 Then 991 MessageBox.Show("请选择检索处理的字段") 992 Exit Sub 993 End If 994 End If 995 996 Dim strSelString As String = "" 997 Dim strFromString As String = "" 998 Dim strWhereString As String = "" 999 Dim strTemp As String 1000 1001 Dim strSql As String = "" 1002 Dim strParmat1 As String = "" 1003 1004 intParmat = 0 1005 strParmat = "" 1006 strParmatValue = "" 1007 1008 strSelString = strSelectSql() 1009 strFromString = strFromSql() 1010 strWhereString = strWhSql() 1011 1012 If intParmat > 0 Then 1013 1014 strParmat1 = " ‘パラメーターの設定" & vbCrLf 1015 strParmat1 = strParmat1 & " Dim paras() As SqlParameter = New SqlParameter(" & intParmat - 1 & ") {}" & vbCrLf 1016 strParmat1 = strParmat1 & strParmat & vbCrLf 1017 strParmat1 = strParmat1 & strParmatValue & vbCrLf 1018 End If 1019 1020 strSql = "" 1021 strSql = strSql & " ‘SQL文の設定" & vbCrLf 1022 strSql = strSql & " Dim strSql As StringBuilder = New StringBuilder()" & vbCrLf & vbCrLf 1023 1024 If rdoInsert.Checked = True Or rdoDelete.Checked = True Or rdoUpdate.Checked = True Then 1025 strSql = strSql & strFromString 1026 strSql = strSql & strSelString 1027 strSql = strSql & strWhereString 1028 1029 strSql = strSql & vbCrLf & strParmat1 1030 Else 1031 strSql = strSql & strSelString 1032 strSql = strSql & strFromString 1033 strSql = strSql & strWhereString 1034 1035 strSql = strSql & vbCrLf & strParmat1 1036 End If 1037 1038 ‘ログパラメーター 1039 If rdoDelete.Checked = True Or rdoUpdate.Checked = True Or rdoInsert.Checked = True Then 1040 strTemp = "" 1041 strTemp = strTemp & " ‘ログパラメーター " & vbCrLf 1042 strTemp = strTemp & " Dim strProgromId As String" & vbCrLf 1043 strTemp = strTemp & " Dim strTypeAction As String" & vbCrLf 1044 strTemp = strTemp & " Dim strTabNa As String" & vbCrLf 1045 strTemp = strTemp & " Dim strTblKey As String" & vbCrLf 1046 strTemp = strTemp & " Dim strReseve As String" & vbCrLf 1047 strTemp = strTemp & "" & vbCrLf 1048 strTemp = strTemp & " ‘プログラムID" & vbCrLf 1049 strTemp = strTemp & " strProgromId = ""TNETMST002_SEL""" & vbCrLf 1050 strTemp = strTemp & "" & vbCrLf 1051 strTemp = strTemp & " ‘操作" & vbCrLf 1052 strTemp = strTemp & " strTypeAction = ""UPDATE""" & vbCrLf 1053 strTemp = strTemp & "" & vbCrLf 1054 strTemp = strTemp & " ‘対象テーブル" & vbCrLf 1055 strTemp = strTemp & " strTabNa = ""SHIP_MST""" & vbCrLf 1056 strTemp = strTemp & "" & vbCrLf 1057 strTemp = strTemp & " ‘キー項目" & vbCrLf 1058 strTemp = strTemp & " strTblKey = ""SHIP_CD:"" & tnetmst002SelMod.ShipCd & "";APP_START_DATE:"" & tnetmst002SelMod.AppStartDate" & vbCrLf 1059 strTemp = strTemp & "" & vbCrLf 1060 strTemp = strTemp & " ‘予備" & vbCrLf 1061 strTemp = strTemp & " strReseve = """ & vbCrLf 1062 strTemp = strTemp & "" & vbCrLf 1063 strTemp = strTemp & " Dim parasLog() As SqlParameter = New SqlParameter(5) {}" & vbCrLf 1064 strTemp = strTemp & " parasLog(0) = New SqlParameter(""@UserId"", SqlDbType.Char, 6)" & vbCrLf 1065 strTemp = strTemp & " parasLog(0).Value = SessionUtil.SESSION_PCHARGE_CD & "" ‘担当者コード" & vbCrLf 1066 strTemp = strTemp & " parasLog(1) = New SqlParameter(""@ProgromId"", SqlDbType.VarChar, 20)" & vbCrLf 1067 strTemp = strTemp & " parasLog(1).Value = strProgromId " & vbCrLf 1068 strTemp = strTemp & " parasLog(2) = New SqlParameter(""@TypeAction"", SqlDbType.Char, 6) " & vbCrLf 1069 strTemp = strTemp & " parasLog(2).Value = strTypeAction" & vbCrLf 1070 strTemp = strTemp & " parasLog(3) = New SqlParameter(""@TabNa"", SqlDbType.VarChar, 20)" & vbCrLf 1071 strTemp = strTemp & " parasLog(3).Value = strTabNa" & vbCrLf 1072 strTemp = strTemp & " parasLog(4) = New SqlParameter(""@TblKey"", SqlDbType.VarChar, 200)" & vbCrLf 1073 strTemp = strTemp & " parasLog(4).Value = strTblKey" & vbCrLf 1074 strTemp = strTemp & " parasLog(5) = New SqlParameter(""@Reseve"", SqlDbType.VarChar, 200)" & vbCrLf 1075 strTemp = strTemp & " parasLog(5).Value = strReseve" & vbCrLf 1076 1077 strSql = strSql & vbCrLf & strTemp 1078 End If 1079 1080 ‘db检索 1081 If rdoSelect.Checked = True Then 1082 strSql = strSql & "" & vbCrLf 1083 strSql = strSql & " Try" & vbCrLf 1084 strSql = strSql & " Return Me.ExecuteDataTable(CommandType.Text, strSql.ToString, paras)" & vbCrLf 1085 strSql = strSql & " Catch ex As SqlException" & vbCrLf 1086 strSql = strSql & " Throw New DaoAccessException(ex)" & vbCrLf 1087 strSql = strSql & " End Try" & vbCrLf 1088 Else 1089 1090 strSql = strSql & "" & vbCrLf 1091 strSql = strSql & " Try" & vbCrLf 1092 If rdoInsert.Checked = True Then 1093 strSql = strSql & " Return Me.ExecuteNonQuery(CommandType.Text, strSql.ToString, paras, nothing, parasLog)" & vbCrLf 1094 Else 1095 strSql = strSql & " Return Me.ExecuteNonQuery(CommandType.Text, strSql.ToString, paras, tnetmst001Mod.HaitaDateTime, parasLog)" & vbCrLf 1096 End If 1097 strSql = strSql & " Catch ex As SqlException" & vbCrLf 1098 strSql = strSql & " Throw New DaoAccessException(ex)" & vbCrLf 1099 strSql = strSql & " End Try" & vbCrLf 1100 End If 1101 1102 strTemp = "" & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf 1103 strTemp = strTemp & "‘***************************************************************************" & vbCrLf 1104 strTemp = strTemp & "‘" & Now.ToString() & vbCrLf & vbCrLf & vbCrLf 1105 1106 strSql = strTemp & strSql 1107 1108 Dim strfile As String 1109 strfile = "Vb_Sql_Source" & Now.ToString("yyyyMMdd") & ".vb" 1110 If System.IO.File.Exists(strfile) = False Then 1111 System.IO.File.WriteAllText(strfile, strSql) 1112 Else 1113 System.IO.File.AppendAllText(strfile, strSql) 1114 End If 1115 1116 MessageBox.Show("vb的sql source生成成功,source存放当前目录下的:【" & strfile & "】文件中。") 1117 End Sub 1118 1119 Private Function strSelectSql() As String 1120 Dim strSelString As String = "" 1121 Dim strInsValue As String = "" 1122 1123 1124 Dim strTemp As String 1125 Dim strTValue As String 1126 1127 Dim strTab As String 1128 Dim strCol As String 1129 Dim strCom As String 1130 Dim strType As String 1131 Dim strLen As String 1132 Dim strASName As String 1133 1134 ‘删除 1135 If rdoDelete.Checked = True Then 1136 Return "" 1137 End If 1138 1139 ‘检索 1140 If rdoSelect.Checked = True Then 1141 strSelString = "" 1142 1143 Dim dtSel As DataTable 1144 dtSel = grdSelCol.DataSource 1145 For index As Integer = 0 To dtSel.Rows.Count - 1 1146 strTab = dtSel.Rows(index)("d1") 1147 strCol = dtSel.Rows(index)("d2") 1148 strCom = dtSel.Rows(index)("d3") 1149 strType = dtSel.Rows(index)("d4") 1150 strLen = dtSel.Rows(index)("d5") 1151 strASName = dtSel.Rows(index)("d6") 1152 1153 If index <= 0 Then 1154 strTemp = " strSql.Append("" SELECT "")" & vbCrLf 1155 Else 1156 strTemp = "" 1157 End If 1158 1159 strTemp = strTemp & " strSql.Append("" " 1160 If index > 0 Then 1161 strTemp = strTemp & ", " 1162 End If 1163 1164 If strType.ToUpper = "char".ToUpper() Then 1165 If grdTabFrom.Rows.Count > 1 Then 1166 strTemp = strTemp & "RTRIM(" & strTab & "." & strCol & ")" 1167 Else 1168 strTemp = strTemp & "RTRIM(" & strCol & ")" 1169 End If 1170 If String.IsNullOrEmpty(strASName) = False Then 1171 strTemp = strTemp & " AS " & strASName 1172 Else 1173 strTemp = strTemp & " AS " & strCol 1174 End If 1175 Else 1176 If grdTabFrom.Rows.Count > 1 Then 1177 strTemp = strTemp & strTab & "." & strCol 1178 Else 1179 strTemp = strTemp & "." & strCol 1180 End If 1181 1182 If String.IsNullOrEmpty(strASName) = False Then 1183 strTemp = strTemp & " AS " & strASName 1184 End If 1185 End If 1186 1187 If String.IsNullOrEmpty(strASName) = False Then 1188 strTemp = strTemp & " AS " & strASName 1189 End If 1190 1191 strTemp = strTemp & " "")" 1192 1193 If String.IsNullOrEmpty(strCom) = False Then 1194 strTemp = strTemp & " ‘" & strCom 1195 End If 1196 strSelString = strSelString & strTemp & vbCrLf 1197 Next 1198 1199 Return strSelString 1200 End If 1201 1202 ‘更新 1203 If rdoUpdate.Checked = True Then 1204 strSelString = "" 1205 1206 Dim dtSel As DataTable 1207 dtSel = grdSelCol.DataSource 1208 For index As Integer = 0 To dtSel.Rows.Count - 1 1209 strTab = dtSel.Rows(index)("d1") 1210 strCol = dtSel.Rows(index)("d2") 1211 strCom = dtSel.Rows(index)("d3") 1212 strType = dtSel.Rows(index)("d4") 1213 strLen = dtSel.Rows(index)("d5") 1214 strASName = dtSel.Rows(index)("d6") 1215 1216 strTemp = " strSql.Append("" " 1217 If index > 0 Then 1218 strTemp = strTemp & ", " 1219 End If 1220 strTemp = strTemp & strCol & " = " & strASName & " "")" 1221 1222 If String.IsNullOrEmpty(strCom) = False Then 1223 strTemp = strTemp & " ‘" & strCom 1224 End If 1225 1226 strSelString = strSelString & strTemp & vbCrLf 1227 1228 strParmat = strParmat & " paras(" & intParmat & ") = New SqlParameter(""" 1229 strParmat = strParmat & strASName & """, SqlDbType." 1230 strParmat = strParmat & strType 1231 If strType.ToUpper <> "Date".ToUpper And strType.ToUpper <> "DateTime".ToUpper Then 1232 strParmat = strParmat & ", " & strLen 1233 End If 1234 strParmat = strParmat & " )" 1235 1236 If String.IsNullOrEmpty(strCom) = False Then 1237 strParmat = strParmat & " ‘" & strCom 1238 End If 1239 strParmat = strParmat & vbCrLf 1240 1241 strParmatValue = strParmatValue & " paras(" & intParmat & ").Value = " 1242 strParmatValue = strParmatValue & "1" 1243 If String.IsNullOrEmpty(strCom) = False Then 1244 strParmatValue = strParmatValue & " ‘" & strCom & " (" & strType & ")" 1245 Else 1246 strParmatValue = strParmatValue & " ‘" & " (" & strType & ")" 1247 End If 1248 strParmatValue = strParmatValue & vbCrLf 1249 intParmat = intParmat + 1 1250 Next 1251 1252 Return strSelString 1253 End If 1254 1255 ‘插入 1256 If rdoInsert.Checked = True Then 1257 strSelString = "" 1258 strInsValue = "" 1259 Dim dtSel As DataTable 1260 dtSel = grdSelCol.DataSource 1261 For index As Integer = 0 To dtSel.Rows.Count - 1 1262 strTab = dtSel.Rows(index)("d1") 1263 strCol = dtSel.Rows(index)("d2") 1264 strCom = dtSel.Rows(index)("d3") 1265 strType = dtSel.Rows(index)("d4") 1266 strLen = dtSel.Rows(index)("d5") 1267 strASName = dtSel.Rows(index)("d6") 1268 1269 strTemp = " strSql.Append("" " 1270 strTValue = " strSql.Append("" " 1271 If index > 0 Then 1272 strTemp = strTemp & ", " 1273 strTValue = strTValue & ", " 1274 End If 1275 strTemp = strTemp & strCol & " "")" 1276 strTValue = strTValue & strASName & " "")" 1277 1278 If String.IsNullOrEmpty(strCom) = False Then 1279 strTemp = strTemp & " ‘" & strCom 1280 strTValue = strTValue & " ‘" & strCom 1281 End If 1282 1283 strSelString = strSelString & strTemp & vbCrLf 1284 strInsValue = strInsValue & strTValue & vbCrLf 1285 1286 1287 strParmat = strParmat & " paras(" & intParmat & ") = New SqlParameter(""" 1288 strParmat = strParmat & strASName & """, SqlDbType." 1289 strParmat = strParmat & strType 1290 If strType.ToUpper <> "Date".ToUpper And strType.ToUpper <> "DateTime".ToUpper Then 1291 strParmat = strParmat & ", " & strLen 1292 End If 1293 strParmat = strParmat & " )" 1294 1295 If String.IsNullOrEmpty(strCom) = False Then 1296 strParmat = strParmat & " ‘" & strCom 1297 End If 1298 strParmat = strParmat & vbCrLf 1299 1300 strParmatValue = strParmatValue & " paras(" & intParmat & ").Value = " 1301 strParmatValue = strParmatValue & "1" 1302 If String.IsNullOrEmpty(strCom) = False Then 1303 strParmatValue = strParmatValue & " ‘" & strCom & " (" & strType & ")" 1304 Else 1305 strParmatValue = strParmatValue & " ‘" & " (" & strType & ")" 1306 End If 1307 strParmatValue = strParmatValue & vbCrLf 1308 intParmat = intParmat + 1 1309 Next 1310 1311 strSelString = strSelString & " strSql.Append("" ) "")" & vbCrLf 1312 strSelString = strSelString & " strSql.Append(""VALUES ( "")" & vbCrLf 1313 strSelString = strSelString & strInsValue 1314 strSelString = strSelString & " strSql.Append("" ) "")" & vbCrLf 1315 1316 Return strSelString 1317 End If 1318 1319 Return "" 1320 End Function 1321 1322 Private Function strFromSql() As String 1323 Dim strFromstring As String = "" 1324 Dim strTemp As String 1325 1326 If rdoSelect.Checked = True Then 1327 Dim strType As String 1328 Dim strFromTab As String 1329 Dim strToTab As String 1330 Dim strCom As String 1331 Dim strLan1 As String 1332 Dim strLan2 As String 1333 Dim strLan3 As String 1334 1335 1336 Dim dtFrom As DataTable 1337 dtFrom = grdTabFrom.DataSource 1338 1339 For index As Integer = 0 To dtFrom.Rows.Count - 1 1340 strType = dtFrom.Rows(index)("d1") & "" 1341 strFromTab = dtFrom.Rows(index)("d2") & "" 1342 strToTab = dtFrom.Rows(index)("d3") & "" 1343 strCom = dtFrom.Rows(index)("d4") & "" 1344 strLan1 = dtFrom.Rows(index)("d5") & "" 1345 strLan2 = dtFrom.Rows(index)("d6") & "" 1346 strLan3 = dtFrom.Rows(index)("d7") & "" 1347 1348 strTemp = "" 1349 If index = 0 Then 1350 strTemp = strTemp & " strSql.Append("" FROM "")" & vbCrLf 1351 strTemp = strTemp & " strSql.Append("" " 1352 strTemp = strTemp & strFromTab & " "")" 1353 If String.IsNullOrEmpty(strCom) = False Then 1354 strTemp = strTemp & " ‘" & strCom 1355 1356 End If 1357 strTemp = strTemp & vbCrLf 1358 1359 Else 1360 strTemp = strTemp & " strSql.Append("" " 1361 strTemp = strTemp & strType & " " & strToTab & " "")" 1362 If String.IsNullOrEmpty(strCom) = False Then 1363 strTemp = strTemp & " ‘" & strCom 1364 1365 End If 1366 strTemp = strTemp & vbCrLf 1367 Dim intOnNum As Integer 1368 intOnNum = 0 1369 1370 If String.IsNullOrEmpty(strLan1) = False Then 1371 strTemp = strTemp & " strSql.Append("" " 1372 If intOnNum = 0 Then 1373 strTemp = strTemp & " ON " 1374 Else 1375 strTemp = strTemp & " AND " 1376 End If 1377 strTemp = strTemp & strLan1 & " "")" & vbCrLf 1378 intOnNum = intOnNum + 1 1379 End If 1380 1381 If String.IsNullOrEmpty(strLan2) = False Then 1382 strTemp = strTemp & " strSql.Append("" " 1383 If intOnNum = 0 Then 1384 strTemp = strTemp & " ON " 1385 Else 1386 strTemp = strTemp & " AND " 1387 End If 1388 strTemp = strTemp & strLan2 & " "")" & vbCrLf 1389 intOnNum = intOnNum + 1 1390 End If 1391 1392 If String.IsNullOrEmpty(strLan3) = False Then 1393 strTemp = strTemp & " strSql.Append("" " 1394 If intOnNum = 0 Then 1395 strTemp = strTemp & " ON " 1396 Else 1397 strTemp = strTemp & " AND " 1398 End If 1399 strTemp = strTemp & strLan3 & " "")" & vbCrLf 1400 intOnNum = intOnNum + 1 1401 End If 1402 1403 strTemp = strTemp 1404 End If 1405 1406 strFromstring = strFromstring & strTemp 1407 Next 1408 Else 1409 Dim strTab As String 1410 Dim strCom As String 1411 1412 Dim dtFrom As DataTable 1413 dtFrom = grdTabFrom.DataSource 1414 1415 strTab = dtFrom.Rows(0)("d2") 1416 strCom = dtFrom.Rows(0)("d4") 1417 1418 strTemp = "" 1419 strTemp = strTemp & " strSql.Append("" " 1420 If rdoInsert.Checked = True Then 1421 strTemp = strTemp & "INSERT INTO " & strTab & " ( "")" 1422 End If 1423 If rdoDelete.Checked = True Then 1424 strTemp = strTemp & "DELETE FROM " & strTab & " "")" 1425 End If 1426 If rdoUpdate.Checked = True Then 1427 strTemp = strTemp & "UPDATE " & strTab & " SET "")" 1428 End If 1429 1430 If String.IsNullOrEmpty(strCom) = False Then 1431 strTemp = strTemp & " ‘" & strCom 1432 1433 End If 1434 strFromstring = strTemp & vbCrLf 1435 End If 1436 1437 1438 Return strFromstring 1439 End Function 1440 1441 Private Function strWhSql() As String 1442 Dim strWhString As String = "" 1443 1444 Dim strTemp As String 1445 1446 Dim strTab As String 1447 Dim strCol As String 1448 Dim strCom As String 1449 Dim strType As String 1450 Dim strLen As String 1451 Dim strASName As String 1452 1453 If rdoInsert.Checked = True Then 1454 Return "" 1455 End If 1456 1457 Dim dtWh As DataTable 1458 dtWh = grdWhCol.DataSource 1459 For index As Integer = 0 To dtWh.Rows.Count - 1 1460 strTab = dtWh.Rows(index)("d1") 1461 strCol = dtWh.Rows(index)("d2") 1462 strCom = dtWh.Rows(index)("d3") 1463 strType = dtWh.Rows(index)("d4") 1464 strLen = dtWh.Rows(index)("d5") 1465 strASName = dtWh.Rows(index)("d6") 1466 If index > 0 Then 1467 strTemp = "" 1468 Else 1469 strTemp = " strSql.Append("" WHERE "")" & vbCrLf 1470 End If 1471 strTemp = strTemp & " strSql.Append("" " 1472 If index > 0 Then 1473 strTemp = strTemp & "AND " 1474 End If 1475 1476 If grdTabFrom.Rows.Count > 1 Then 1477 strTemp = strTemp & strTab & "." & strCol & " = " & strASName & " "")" 1478 Else 1479 strTemp = strTemp & strCol & " = " & strASName & " "")" 1480 End If 1481 If String.IsNullOrEmpty(strCom) = False Then 1482 strTemp = strTemp & " ‘" & strCom 1483 End If 1484 strTemp = strTemp & vbCrLf 1485 strWhString = strWhString & strTemp 1486 1487 strParmat = strParmat & " paras(" & intParmat & ") = New SqlParameter(""" 1488 strParmat = strParmat & strASName & """, SqlDbType." 1489 strParmat = strParmat & strType 1490 If strType.ToUpper <> "Date".ToUpper And strType.ToUpper <> "DateTime".ToUpper Then 1491 strParmat = strParmat & ", " & strLen 1492 End If 1493 strParmat = strParmat & " )" 1494 1495 If String.IsNullOrEmpty(strCom) = False Then 1496 strParmat = strParmat & " ‘" & strCom 1497 End If 1498 strParmat = strParmat & vbCrLf 1499 1500 strParmatValue = strParmatValue & " paras(" & intParmat & ").Value = " 1501 strParmatValue = strParmatValue & "1" 1502 If String.IsNullOrEmpty(strCom) = False Then 1503 strParmatValue = strParmatValue & " ‘" & strCom & " (" & strType & ")" 1504 Else 1505 strParmatValue = strParmatValue & " ‘" & " (" & strType & ")" 1506 End If 1507 strParmatValue = strParmatValue & vbCrLf 1508 intParmat = intParmat + 1 1509 Next 1510 Return strWhString 1511 End Function 1512 1513 1514 #End Region 1515 ‘执行检索sql问返回结果集 1516 Private Function SelsectDb(ByVal strsql As String) As DataTable 1517 strConnectstring = "Password=" & txtDBPasswd.Text & ";Persist Security Info=True;User ID=" & txtDBUser.Text & ";Initial Catalog=" & txtDBName.Text & ";Data Source=" & txtDBIp.Text 1518 1519 Dim conn As SqlConnection = Nothing 1520 Dim comm As SqlCommand = Nothing 1521 Dim datap As SqlDataAdapter = Nothing 1522 Dim dt As DataTable = New DataTable() 1523 Try 1524 conn = New SqlConnection(strConnectstring) 1525 datap = New SqlDataAdapter() 1526 comm = New SqlCommand() 1527 conn.Open() 1528 comm.Connection = conn 1529 comm.CommandType = CommandType.Text 1530 comm.CommandText = strsql 1531 datap.SelectCommand = comm 1532 datap.Fill(dt) 1533 Return dt 1534 Catch ex As Exception 1535 MessageBox.Show(ex.Message) 1536 Return Nothing 1537 Finally 1538 If datap IsNot Nothing Then 1539 1540 datap.Dispose() 1541 End If 1542 If comm IsNot Nothing Then 1543 1544 comm.Dispose() 1545 End If 1546 datap.Dispose() 1547 1548 If conn IsNot Nothing Then 1549 If conn.State = ConnectionState.Open Then 1550 conn.Close() 1551 End If 1552 End If 1553 End Try 1554 End Function 1555 1556 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 1557 1558 End Sub 1559 End Class
标签:
原文地址:http://www.cnblogs.com/shenvsxian/p/5223442.html