当前位置:编程学习 > VB >>

请教高手用VB向SQL数库中添加数据的问题,急急急!!!!!!!!

Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim res As ADODB.Recordset
Dim StrSQL As String
Set cn = New ADODB.Connection
Set res = New ADODB.Recordset
 
cn.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=lzy;Initial Catalog=lzy"
StrSQL = "select * from dd  "
res.Open StrSQL, cn, adOpenKeyset, adLockOptimistic
res.AddNew
res.Fields(0) = 11
res.Fields(1) = 22
res.Fields(2) = 33

res.Update
res.Close
Set res = Nothing
Set cn = Nothing

End Sub
我在窗体中编写这样一段代码,但是在运行中提示我用户定义类型未定义,请教高手帮忙解决 --------------------编程问答-------------------- 是因为你用到ado了
你只需要在工程/引用 miscrosoft Activex data objects 2.x library 就可以了 --------------------编程问答-------------------- 在工程引用中中添加引用
microsoft activex data objects 引用
就行了 --------------------编程问答-------------------- Private Sub Command1_Click()

Dim cn As ADODB.Connection
Dim res As ADODB.Recordset
Dim StrSQL As String
Set cn = New ADODB.Connection
Set res = New ADODB.Recordset
cn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=lzy;Initial Catalog=lzy"
cn.Open
StrSQL = "select * from cs"
res.Open StrSQL, cn, adOpenKeyset, adLockOptimistic
res.AddNew
res.Fields(0) = Now
res.Fields(1) = 20
res.Fields(2) = 13

res.Update
res.Close
Set res = Nothing
Set cn = Nothing
请教给位高手,我要是想添加10行3列的数据应该怎么编写代码,我这个代码只能填写1行3列的数据,请教各位了 --------------------编程问答-------------------- 用for i=1 to 30循环添加就行了啊.. --------------------编程问答-------------------- 用一个循环,遍历10次。

或者使用如下语句放弃AddNew
用10条
Insert into 表(字段1,字段2,字段3) Values (值1,值2,值3)
来写入数据。 --------------------编程问答-------------------- 楼上的能不能具体点,把For循环语句能写全吗?一共是10行3列,这3列的名称分别是时间、名称、值,请教给位了 --------------------编程问答-------------------- 能写的具体点吗?我不知道当写完一行后,怎么能换到第二行去,请教各位了 --------------------编程问答-------------------- 请教各位高手怎么用For循环去换行,我不知道列用什么表示 --------------------编程问答-------------------- for i= 1 to 10 
    with res
    .AddNew
    .Fields(0) = Now
    .Fields(1) = 20
    .Fields(2) = 13
    .Update
    end if
next
--------------------编程问答-------------------- for i= 1 to 10 
    with res
    .AddNew
    .Fields(0) = Now
    .Fields(1) = 20
    .Fields(2) = 13
    .Update
    end with
next
上面打错了 把"if" 改成 “with”
--------------------编程问答-------------------- for i= 1 to 10 
    with res
    .AddNew
    .Fields(0) = Now
    .Fields(1) = 20
    .Fields(2) = 13
    .Update
    end with
next

作个重复循环就行了
补充:VB ,  数据库(包含打印,安装,报表)
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,