Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用vb6代码创建DSN之前,请检查现有DSN_Vb6 - Fatal编程技术网

在使用vb6代码创建DSN之前,请检查现有DSN

在使用vb6代码创建DSN之前,请检查现有DSN,vb6,Vb6,我用DSN做了一个连接,在其中我创建了一个DSN throgh程序。因为我调用了一个函数来创建DSN,所以我不想在每次运行软件时调用该函数,而是想检查系统中是否已经存在同名DSN,如果不存在,则只调用该函数` Public Sub ConnectDB(Con As ADODB.Connection) Call CreatSQLDSN("TRDSN", VarSrvNm, VarDbName) If Cn.State = 1 Then Cn.Close On Error Resume Next

我用DSN做了一个连接,在其中我创建了一个DSN throgh程序。因为我调用了一个函数来创建DSN,所以我不想在每次运行软件时调用该函数,而是想检查系统中是否已经存在同名DSN,如果不存在,则只调用该函数`

Public Sub ConnectDB(Con As ADODB.Connection)

Call CreatSQLDSN("TRDSN", VarSrvNm, VarDbName)
If Cn.State = 1 Then Cn.Close
On Error Resume Next
Con.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=TRDSN;Initial Catalog='" & VarDbName & "'"

Con.Open Con.ConnectionString
If Err.Number <> 0 Then
If Err.Number = -2147467259 Then
    If MsgBox(ServerName & " Server not Found. Connect to Other Server?", vbQuestion + vbDefaultButton2 + vbYesNo, "") = vbYes Then
        PrintFile = Trim(Left(FindWindowPath, 3) & "DosPrint.Bat")
        FileSystemObject.CreateTextFile PrintFile, True
        Set TextStream = FileSystemObject.OpenTextFile(PrintFile, ForAppending)
        TextStream.WriteLine "Del " & Left(FindWindowPath, 3) & "ServerName.dat"
        TextStream.Close
        Shell PrintFile, vbHide
    End If
    End
Else
If MsgBox(Err.Description, vbQuestion + vbOKOnly, "") = vbOK Then
        Cancel = True
        Exit Sub
    End If
End If
End If
0
End Sub

Public Function CreatSQLDSN(SqlDsnName As String, SqlServerName As String, SqlDataName As String)
Dim Ret%, Driver$, Attributes$
Driver = "SQL Server" & Chr(0)
Attributes = "Server=" & SqlServerName & Chr(0)
Attributes = Attributes & "DSN=" & SqlDsnName & Chr(0)
Attributes = Attributes & "Database=" & SqlDataName & Chr(0)
Ret = SQLConfigDataSource(vbAPINull, ODBC_Add_User_DSN, Driver, Attributes)

'ret is equal to 1 on success and 0 if there is an error
If Ret <> 1 Then
    MsgBox "User DSN Creation Failed"
End If
End Function`
Public Sub ConnectDB(Con As ADODB.Connection)
调用createSQLDSN(“TRDSN”,VarSrvNm,VarDbName)
如果Cn.State=1,则Cn.Close
出错时继续下一步
Con.ConnectionString=“Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=TRDSN;Initial Catalog=”&VarDbName&“”
Con.打开Con.连接字符串
如果错误号为0,则
如果错误号=-2147467259,则
如果MsgBox(ServerName&“找不到服务器。是否连接到其他服务器?”,vbQuestion+vbDefaultButton2+vbYesNo,”)=vbYes,则
PrintFile=Trim(左(FindWindowPath,3)和“DosPrint.Bat”)
FileSystemObject.CreateTextFile打印文件,真
设置TextStream=FileSystemObject.OpenTextFile(打印文件,用于显示)
TextStream.WriteLine“Del”和Left(FindWindowPath,3)和“ServerName.dat”
文本流,关闭
Shell打印文件,vbHide
如果结束
终点
其他的
如果MsgBox(Err.Description,vbQuestion+vbOKOnly,”)=vbOK,则
取消=真
出口接头
如果结束
如果结束
如果结束
0
端接头
公共函数createSQLDSN(SqlDsnName作为字符串,SqlServerName作为字符串,SqlDataName作为字符串)
Dim Ret%,驱动程序$,属性$
Driver=“SQL Server”&Chr(0)
Attributes=“Server=”&SqlServerName&Chr(0)
Attributes=Attributes&“DSN=”&SqlDsnName&Chr(0)
Attributes=Attributes&“Database=“&SqlDataName&Chr(0)
Ret=SQLConfigDataSource(vbAPINull、ODBC\U Add\U User\U DSN、驱动程序、属性)
'成功时ret等于1,出现错误时等于0
如果Ret 1那么
MsgBox“用户DSN创建失败”
如果结束
端函数`

当需要知道DSN是否存在时,会想到几个选项。您可以读取注册表,也可以利用现有的API调用。我更喜欢第二种选择。这似乎是检查DSN是否存在的更干净的方法。下面是我所说的一个例子:

Option Explicit

Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long

Private Const ODBC_ADD_DSN = 1
Private Const ODBC_CONFIG_DSN = 2
Private Const ODBC_REMOVE_DSN = 3
Private Const ODBC_ADD_SYS_DSN = 4
Private Const ODBC_CONFIG_SYS_DSN = 5
Private Const ODBC_REMOVE_SYS_DSN = 6
Private Const ODBC_REMOVE_DEFAULT_DSN = 7

Private Sub cmdCreate_Click()
   Dim VarSrvNm As String
   Dim VarDbName As String

   VarSrvNm = "MyServer"
   VarDbName = "MyDB"

   If Not SQLDSNExists("TRDSN", VarSrvNm, VarDbName) Then
      If Not CreateSQLDSN("TRDSN", VarSrvNm, VarDbName) Then
         MsgBox "User DSN Creation Failed"
      End If
   End If
End Sub

Public Function CreateSQLDSN(SqlDsnName As String, SqlServerName As String, SqlDataName As String) As Boolean
   Dim Ret%, Driver$, Attributes$

   Driver = "SQL Server" & Chr(0)
   Attributes = "Server=" & SqlServerName & Chr(0)
   Attributes = Attributes & "DSN=" & SqlDsnName & Chr(0)
   Attributes = Attributes & "Database=" & SqlDataName & Chr(0)

   Ret = SQLConfigDataSource(0&, ODBC_ADD_DSN, Driver, Attributes)

   'ret is equal to 1 on success and 0 if there is an error
   CreateSQLDSN = (Ret = 1)
End Function

Public Function SQLDSNExists(SqlDsnName As String, SqlServerName As String, SqlDataName As String) As Boolean
   Dim Ret%, Driver$, Attributes$

   Driver = "SQL Server" & Chr(0)
   Attributes = "Server=" & SqlServerName & Chr(0)
   Attributes = Attributes & "DSN=" & SqlDsnName & Chr(0)
   Attributes = Attributes & "Database=" & SqlDataName & Chr(0)

   Ret = SQLConfigDataSource(0&, ODBC_CONFIG_DSN, Driver, Attributes)

   'ret is equal to 1 on success and 0 if there is an error
   SQLDSNExists = (Ret = 1)
End Function

这里的主要思想是尝试修改要添加的DSN。如果调用失败,则DSN不存在。

我无法帮助您,但您可以尝试包括其他目标更明确的标记,例如sql server,您可以在其中找到在这方面有经验的人。我进行了一些搜索,许多人检查了注册表以查看DSN是否存在。谢谢@brian,这正是我要找的,我用注册表键检查了它,但那只是检查dsn是否存在,它没有配置它,我能用同样的配置方法吗?