SQLiteConnectionStringBuilder生成无效的连接字符串

时间:2013-08-13 08:19:45

标签: vb.net .net-4.0 system.data.sqlite

我正在尝试使用数据源或URI与SQLiteConnectionStringBuilder(System.Data.SQLite v1.0.88.0)构建有效的连接字符串:

    Dim connectionBuilder = New SQLiteConnectionStringBuilder
    connectionBuilder.DataSource = Path.Combine(datadir, "mydatabase.sqlite")
    connectionBuilder.Version = 3

    ' Open connection
    Dim connection As New SQLiteConnection(connectionBuilder.ConnectionString)

或者:

    Dim connectionBuilder = New SQLiteConnectionStringBuilder
    connectionBuilder.Uri = New Uri(Path.Combine(datadir, "mydatabase.sqlite")).AbsolutePath
    connectionBuilder.Version = 3

    ' Open connection
    Dim connection As New SQLiteConnection(connectionBuilder.ConnectionString)

两者都生成相同的连接字符串:

    uri=file:///C:/Users/myusername/Documents/Visual%20Studio%202010/Projects/myproject/myproject/bin/Debug/data/mydatabase.sqlite;version=3

URI本身是有效的,连接字符串实际上看起来不错,但是当我尝试打开连接时,由于SQLite中的路径格式,它会生成NotSupportedException

       bei System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
       bei System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
       bei System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[]         pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
       bei System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates,         Boolean needFullPath)
       bei System.IO.Path.GetFullPath(String path)
       bei System.Data.SQLite.SQLiteConnection.ExpandFileName(String sourceFile, Boolean toFullPath) in c:\dev\sqlite\dotnet\System.Data.        SQLite\SQLiteConnection.cs:Zeile 2690.
       bei System.Data.SQLite.SQLiteConnection.Open() in c:\dev\sqlite\dotnet\System.Data.SQLite\SQLiteConnection.cs:Zeile 2009.

如何防止自己构建连接字符串的缺点?

1 个答案:

答案 0 :(得分:0)

问题与以两个\\开头的UNC路径问题有关。不知何故,SQLiteConnectionStringBuilder错误地逃避了它们。

我已将第一个\\替换为\\\

,从而解决了我的问题
connectionBuilder.DataSource = pathToFile.Replace("\\", "\\\")