我正在构建一个 VB.net 应用程序,它可以查询 sql server 数据库,然后根据返回的结果生成插入语句。我有一个 case 语句,它检查组合框中的数据类型并根据数据类型对其进行格式化。我遇到的下一个问题是我要问的是,是否可以将日期转换为整数到十六进制值,以便我可以将十六进制值转换为 sql server datetime 数据类型?我希望我的代码不是太菜鸟。
For Each dr As DataRow In dt.Rows
x = 0
Dim splitout = Split(TextBox1.Text, " ")
builder.Append("INSERT INTO " & splitout(3).ToUpper & " (")
Do Until x = DataGridView1.Columns.Count
builder.Append(DataGridView1.Columns(x).HeaderText.ToString)
If x = DataGridView1.Columns.Count - 1 Then
builder.Append("")
Else
builder.Append(",")
End If
x = x + 1
Loop
builder.Append(") VALUES (")
For Each dc As DataColumn In dt.Columns
Do Until y = dt.Columns.Count
Select Case ComboBox1.Items(y).ToString
Case "char"
builder.Append("'" & LTrim(RTrim(dr(y))) & "'")
Case "smalldatetime"
If dr(y) Is DBNull.Value Then
builder.Append("NULL")
Else
Dim hexbuilder As New StringBuilder
Dim var2INT As Integer
var2INT = CInt(dr(y))
For x = 0 To var2INT.ToString.Length - 1
hexbuilder.Append(Conversion.Hex(var2INT.ToString.Substring(x, 1)))
Next x
builder.Append("CAST(" & hexbuilder.ToString & " as smalldatetime)")
End If
Case "numeric"
builder.Append(dr(y))
Case "bit"
builder.Append("CAST('" & dr(y) & "' as BIT)")
Case "uniqueidentifier"
If dr(y) Is DBNull.Value Then
builder.Append("NULL")
Else
builder.Append("N'")
builder.Append(dr(y))
builder.Append("'")
End If
Case "varchar"
builder.Append("'" & LTrim(RTrim(dr(y))) & "'")
End Select
If y = dt.Columns.Count - 1 Then
Else
builder.Append(",")
End If
y = y + 1
Loop
Next
builder.Append(")")
builder.AppendLine()
y = 0
Next
ds.Dispose()
Con.Close()