注册 登录
编程论坛 VB6论坛

怎么往 SQL 表里插入数据,求大师帮忙,谢谢!!

cailinru 发布于 2014-09-01 10:36, 460 次点击
让表里自动生成数据

代码如下
Private Sub Command1_Click()
 Dim c As Integer
    Dim d As Integer
    Dim e As Integer
    Dim k As String
    Dim i As Integer
Set rs = Nothing
      c = 200
      d = 200
For e = 200 To 206
      e = e + 1
      k = CStr(c) + "." + CStr(d) + "." + CStr(e)
    For i = 0 To 255
      i = i + 1
       sql = "Insert into IPDB (号段,IP号) VALUES (" & k & "," & i & ")"
    Set rs = SelectSQL(sql, msg)
    Set Me.DataGrid1.DataSource = rs
    DataGrid1.Refresh
    Next
    Next
  End Sub
2 回复
#2
风吹过b2014-09-01 14:37
sql = "Insert into IPDB (号段,IP号) VALUES (" & k & "," & i & ")"
    Set rs = SelectSQL(sql, msg)

我记得 SQL Insert 没有返回,也就是说      Set rs = SelectSQL(sql, msg) 取不到结果。

使用 数据连接的 执行 SQL 的功能直接执行这个 SQL 命令。
#3
cailinru2014-09-02 09:05
回复 2 楼 风吹过 b
你说的对,现在这个提示'.200'附近有语法错误
Private Sub Command1_Click()
   On Error GoTo ErrMsg
    Dim sq1 As String
    Dim c As Integer
    Dim d As Integer
    Dim e As Integer
    Dim k As String
    Dim i As Integer

Set cn = New ADODB.Connection
cn.ConnectionString = "provider=msdasql;dsn=ss"
cn.Open
      c = 200
      d = 200
For e = 200 To 206
      For i = 1 To 255
      k = CStr(c) + "." + CStr(d) + "." + CStr(e) + "." + CStr(i)
       cn.Execute ("Insert into IPDB (IP号) VALUES (" & k & ")")
  
    Next
Next
Set rs = Nothing
sql = "select * from IPDB "
    Set rs = SelectSQL(sql, msg)
    Set Me.DataGrid1.DataSource = rs
    DataGrid1.Refresh
    rs.Close
ErrMsg:
    MsgBox err.Description, vbExclamation, "出错"
End Sub
1