VB6当字段为空时如何按原样从resultset插入表
上表定义VB6当字段为空时如何按原样从resultset插入表,vb6,Vb6,上表定义 CREATE TABLE [dbo].[TABLE001] ( [T001_int] [int] NULL, [T001_char] [char](100) NULL, [T001_decimal] [decimal](5, 0) NULL ) CREATE TABLE [dbo].[TABLE002] ( [T002_int] [int] NULL, [T002_char] [char](100) NULL,
CREATE TABLE [dbo].[TABLE001]
(
[T001_int] [int] NULL,
[T001_char] [char](100) NULL,
[T001_decimal] [decimal](5, 0) NULL
)
CREATE TABLE [dbo].[TABLE002]
(
[T002_int] [int] NULL,
[T002_char] [char](100) NULL,
[T002_decimal] [decimal](5, 0) NULL
)
INSERT INTO TABLE002 VALUES (NULL, NULL, NULL)
上面是我的VB6代码
什么时候开始!T002_int=null,发生错误
什么时候开始!T002_char=null,插入为“”
什么时候开始!T002_decimal=null,发生错误
我想将NULL作为NULL插入
我怎样才能解决这个问题
请不要告诉我我不应该这样做
我想知道如何以空值插入。您可以尝试以下方法:
Dim strQuery As String
Dim rdoEnv As rdoEnvironment
Dim rdoCon As rdoConnection
Dim rdoRS As rdoResultset
strQuery = ""
strQuery = strQuery & " SELECT * FROM TABLE002"
Set rdoEnv = rdoEnvironments(0)
Set rdoCon = rdoEnv.OpenConnection("", rdDriverNoPrompt, True, MyCon)
Set rdoRS = rdoCon.OpenResultset(strQuery, rdOpenKeyset, rdConcurReadOnly)
Do Until rdoRS.EOF = True
strQuery = ""
strQuery = strQuery & " INSERT INTO TABLE001"
strQuery = strQuery & " VALUES"
strQuery = strQuery & " (" & rdoRS!T002_int & ", '" & rdoRS!T002_char & "'," & rdoRST002_decimal & ")"
rdoCon.Execute (strQuery)
rdoRS.MoveNext
Loop
一些注意事项:
- 插入时,始终指定列
- 如果
列包含引号或其他 弄乱了SQL语法。T002\u char
最好使用事先准备好的陈述
Dim int_value As String
Dim str_value As String
Dim dec_value As String
int_value = IIf(IsNull(myrs!T002_int), "NULL", CStr(myrs!T002_int))
str_value = IIf(IsNull(myrs!T002_char), "NULL", "'" & myrs!T002_char & "'")
dec_value = IIf(IsNull(myrs!T002_decimal), "NULL", CStr(myrs!T002_decimal))
strQuery = ""
strQuery = strQuery & " INSERT INTO TABLE001"
strQuery = strQuery & " (T001_int, T001_char, T001_decimal)"
strQuery = strQuery & " VALUES"
strQuery = strQuery & " (" & int_value & ", " & str_value & ", " & dec_value & ")"