一下是我的一段代码,可以正常运行Sub dzzaddarticlep(cid)
Set conn = Server.CreateObject("ADODB.CONNECTION")
connstr = "DBQ=" + Server.MapPath("/shujuku.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
conn.open connstr
sqla = "select * From bbsClass"
Set rsa = Server.CreateObject("adodb.recordset")
rsa.open sqla, conn, 1, 1
Do While Not rsa.EOF
Response.Write "<option value='" & rsa("classid") & "'"
If cid <> "" Then
If CLng(cid) = CLng(rsa("classid")) Then
Response.Write " selected"
End If
End If
Response.Write ">" & rsa("classname") & "</option>"
rsa.movenext
Loop
rsa.Close
Set rsa = Nothing
End Sub我怎么把这一段代码写道全局变量呢?否则每一个function都要写这个,太麻烦了!
Set conn = Server.CreateObject("ADODB.CONNECTION")
connstr = "DBQ=" + Server.MapPath("/shujuku.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
conn.open connstr高手帮忙!!

解决方案 »

  1.   

    放在modules
    Option Explicit
    Public gconSys As ADODB.Connection
    Public Function SystemInitialize() As Boolean
        On Error GoTo errhandle
        Set conn = Server.CreateObject("ADODB.CONNECTION")
    connstr = "DBQ=" + Server.MapPath("/shujuku.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
    conn.open connstr
    End Function
      

  2.   

    我编写的是dll,是不是放到最上边就可以拉?我试试
      

  3.   

    把conn设成全局变量,适当地方运行 一次 上面几行代码就成了
      

  4.   

    放在modules
    Option Explicit
    Public gconSys As ADODB.Connection 。。我方在最上边了,可是提示 gconSys As ADODB.Connection 编译错误:用户定义类型未定义。
    把conn设成全局变量,适当地方运行 一次 上面几行代码就成了
    我晕了,我就是不会设定全局变量啊我编写的是dll,只有类模块
      

  5.   

    不行啊,我把
    Public Function SystemInitialize() As Boolean
    Set conn = Server.CreateObject("ADODB.CONNECTION")
    connstr = "DBQ=" + Server.MapPath("/shujuku.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
    conn.open connstr
    End Function这段代码放在最前边,可是提示:
    变量或者类型不正确,或者不在可以接受的范围之内,要不就是与其他数据冲突。
    以前没有错的
    这是怎么回事?
      

  6.   

    放在一个xx.inc文件里
    以后用到它的地方就<#include file=xx.inc#>
    就ok 了
      

  7.   

    <#include file=xx.inc#>这么写法不正确啊,显示红色,有正确写法么?谢谢!
      

  8.   

    Public conn As Object
    Private Sub Class_Initialize()
    Set conn = Server.CreateObject("ADODB.CONNECTION")
    connstr = "DBQ=" + Server.MapPath("/shujuku.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
    conn.open connstr
    End Sub
      

  9.   

    hhjjhjhj(大头),有qq么?我实在不行了!唉,帮忙啊。拜托!我的qq 108828368 谢谢!在线吧,我给你穿过去,你看看!谢了!实在不行了,弄了2天了,他们都等急了。谢了!
      

  10.   

    我把代码上传了,谢谢各位老师了。谢谢!帮忙修改,帮忙看看http://jk123.net/test.rar
      

  11.   


    这样的代码也不会加,有点吃力Private MyScriptingContext As ScriptingContext
    Private Application As Application
    Private Request As Request
    Private Response As Response
    Private Server As Server
    Private Session As SessionPublic conn As Object
    Private Sub Class_Initialize()
    Set conn = Server.CreateObject("ADODB.CONNECTION")
    connstr = "DBQ=" + Server.MapPath("/shujuku.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
    conn.open connstr
    End Sub'======================定义变量开始======================
    '======================定义变量结束======================
      

  12.   

    我试了,怎么提示原来的也错误了啊asp2dll 错误 '800a005b' 未设置对象变量或 With block 变量 /wzgl/dll/0018/test.asp,行4 
    拜托了大头,帮人帮到底吧,我真的很吃力啊我从来没有弄过dll,一直是弄web程序,这是第一次,只要数据库弄上,就没有问题了。谢谢!!
    可不可以qq 啊,这么等太累了。谢谢了啊!
      

  13.   

    帮忙啊大头!
    你没有空间上传么?我送你一个啊,100m的空间,可以么?谢谢!!!qq 108828368请指教啊,在线等!谢谢了大头
      

  14.   

    可以在模块中设置变量,public var1 ,这样var1对每个窗体都有效!
      

  15.   

    我希望能用ini文件来实现数据库连接。文章我看过4——5篇,但是一直调试不正确!高手帮忙了!!谢谢!我把代码上传了,谢谢各位老师了。谢谢!帮忙修改,帮忙看看http://jk123.net/test.rar
      

  16.   

    简单问题真诚请教高手了!!
    我的qq 108828368 解决问题送100M 网络空间。谢谢!
      

  17.   

    放在模块里:
    Global con As New ADODB.Connection
    Global cmn As New ADODB.Command
    Global rstRec As New ADODB.Recordset
      

  18.   

    dll里边只有模块,没有窗体帮忙修改,帮忙看看http://jk123.net/test.rar很简单的一段程序。我希望数据库调用的路径可以写在ini里边
      

  19.   

    如果写asp,可以<!--#include file="conn.inc"-->
    用request读进相应参数,构造con
      

  20.   

    是asp,但是写进<!--#include file="conn.inc"-->在dll调用的时候参数无效,也就是无论怎么定义,参数都没有值
      

  21.   

    vb中读取要用到GetPrivateProfileString,WritePrivateProfileString等几个api,可参见
    http://expert.csdn.net/Expert/topic/2189/2189110.xml?temp=.7792932
      

  22.   

    用这个:
    '********************************************************************************
    '** 函数功能:从ini配置文件中读取指定段名、关键字名的值
    '** 调用语法: GetInIKeyValue(SectionName as string,KeyName As String,FileName As String)
    '** 参数说明:
    '**         SectionName :段名
    '**         KeyName     :关键字名
    '**         FileName    :ini文件名包括路径
    '** 返 回 值:
    '**         String      :返回关键字值
    '** 处理说明:
    '**         调用API函数GetPrivateProfileString
    '******************************************************************************
    Public Function GetInIKeyValue(ByVal SectionName As String, _
                                   ByVal KeyName As String, _
                                   ByVal FileName As String) As String
        Dim KeyValue$
        Dim strTmp As String
        
        KeyValue$ = String$(512, " ")
        GetPrivateProfileString SectionName, KeyName, "", KeyValue$, 512, FileName
        strTmp = Trim(KeyValue$)
        GetInIKeyValue = Left(strTmp, Len(strTmp) - 1)
    End Function'********************************************************************************
    '** 函数功能:从ini配置文件中写入指定段名、关键字名及值
    '** 调用语法: SetInIKeyValue(SectionName as string,KeyName As String,KeyValue as string ,FileName As String)
    '** 参数说明:
    '**         SectionName :段名
    '**         KeyName     :关键字名
    '**         KeyValue    :关键字值
    '**         FileName    :ini文件名包括路径
    '** 返 回 值:
    '** 处理说明:
    '**         调用API函数WritePrivateProfileString
    '******************************************************************************
    Public Sub SetInIKeyValue(ByVal SectionName As String, _
                               ByVal KeyName As String, _
                               ByVal KeyValue As String, _
                               ByVal FileName As String)
        Dim lng As Long
        
        lng = WritePrivateProfileString(SectionName, KeyName, KeyValue, FileName)
    End Sub
      

  23.   

    我也想到过,但是就是提示GetPrivateProfileString错误
    另外不会把得到的ini里边的数值设定为全局变量dublue你有qq 么?108828368 随时恭候!谢谢!!
      

  24.   

    我写了啊,这么写你看对么?Sub opendb(db)
    Set conn = Server.CreateObject("ADODB.CONNECTION")
    connstr = "DBQ=" + Server.MapPath("test.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
    conn.open connstr
    End Sub但是调用的时候怎么还是不行?我通过opendb调用的
      

  25.   

    连接时,还需要用户名和密码,举个简单例子:
    Public Sub InitDB()
        '初始化数据库
        '建立数据库连接并打开
        strDBName = "dbRecords"
        strcnn = "Provider=MSDASQL.1;UID=sa;Data Source=Records;Extended Properties='DSN=dbRecords;DBQ=" & strDBName & ";DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5'"
        cnn.Open strcnn, "", ""
        
        '设置记录集
        Set rstRec = New ADODB.Recordset
        rstRec.CursorType = adOpenKeyset
        rstRec.LockType = adLockOptimistic
        rstRec.Open "tblRec", cnn, , , adCmdTable
        iRecNum = rstRec.RecordCount
    End Sub
      

  26.   

    server.mappath我没在vb中用过,不知对否,如果是我,就用app.path
    在你的应用程序中,最开始的地方,比如form_load,module等调用此函数就行了
      

  27.   

    这是sql链接,我的是access的关键是调用无效,直接写就可以唉
      

  28.   

    Sub opendb()
    Set conn = Server.CreateObject("ADODB.CONNECTION")
    connstr = "DBQ=" + Server.MapPath("test.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
    conn.open connstr
    End SubSub test2(cid)
    Opendb
    sqla = "select * From Class"
    Set rsa = Server.CreateObject("adodb.recordset")
    rsa.open sqla, conn, 1, 1
    Response.Write rsa("classname")
    rsa.Close
    Set rsa = Nothing
    End Sub这样就是无效的而Sub test1(cid)
    Set conn = Server.CreateObject("ADODB.CONNECTION")
    connstr = "DBQ=" + Server.MapPath("test.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
    conn.open connstr
    sqla = "select * From Class"
    Set rsa = Server.CreateObject("adodb.recordset")
    rsa.open sqla, conn, 1, 1
    Response.Write rsa("classname")
    rsa.Close
    Set rsa = Nothing
    End Sub这样运行正常
      

  29.   

    单步跟踪一下,看看db,con等是否获得值
      

  30.   

    在过程外定义global conn as adodb.connection
      

  31.   

    老哥啊,我捣鼓了好几天了,实在没辙了实在不是直接要代码的懒人啊,可是实在是捣鼓不好了!唉能不能给看看代码啊。谢谢了asp程序编写我是一点问题没有,但是调用dll我是第一次做,非做不可。这几天头都大了好几圈了@#$%%%&^*$^&%*&能上qq指点老弟一下么?不会耽误太长时间的,谢谢了!另有重谢!108828368
      

  32.   

    你的意思是不是就是想把Connection对象设置为全局的,那每次就不用都去连接一次库了,对不对?
    这样可以在类模块里用一个方法来得到Connection对象呀。
      

  33.   

    代码我看了'======================子过程/函数结束======================Public Sub OnStartPage(PassedScriptingContext As ScriptingContext)
    Set MyScriptingContext = PassedScriptingContext
    Set Application = MyScriptingContext.Application
    Set Request = MyScriptingContext.Request
    Set Response = MyScriptingContext.Response
    Set Server = MyScriptingContext.Server
    Set Session = MyScriptingContext.Session
    Set conn = Server.CreateObject("ADODB.CONNECTION")
    connstr = "DBQ=" + Server.MapPath("test.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
    conn.open connstr
    End SubPublic Sub OnEndPage()
    Set MyScriptingContext = Nothing
    Set Application = Nothing
    Set Request = Nothing
    Set Response = Nothing
    Set Server = Nothing
    Set Session = Nothing
    Set conn = Nothing
    End Sub
      

  34.   

    zizaiwang的代码可以运行
    虽然没有生成ini文件,但是只有如此了。谢谢大家!谢谢参与的人!ini我再想办法。