如何在 C# C-Sharp 中打开网络共享上的 SQLite3
How to Open SQLite3 on Network Share in C# C-Sharp
我在使用打开两个 SQLite 文件并读取内容的非常简单的 C# 程序时遇到问题。
当数据库文件在本地机器上时程序工作正常(按钮 1),但当文件在另一台 LAN PC 上时我无法让它工作(按钮 2)(两者 运行 Windows10).
我正在使用 Visual Studio 2019 编写这个 Windows 使用 .Net Framework 4.7.2 的应用程序
我的代码很简单。这是两个功能相同的按钮,但一个用于本地文件,另一个用于 LAN PC 上的文件。这是我的代码:
using System;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Oak_SQLite3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//-----------------------------------------------------------------------------
// Local Database
//-----------------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
string dbPath = Path.Combine(@"C:\Temp\", @"testing.db");
string connString = string.Format("Data Source={0}", dbPath);
Debug.Print(connString);
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
StringBuilder query = new StringBuilder();
query.Append("SELECT * ");
query.Append("FROM TEST");
using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), conn))
{
conn.Open();
using (SQLiteDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Console.WriteLine("{0} {1} {2}",
dr.GetValue(0),
dr.GetValue(1),
dr.GetValue(2));
}
}
}
}
}
//-----------------------------------------------------------------------------
// LAN PC Database (Named Oak with OCH-POS share)
//-----------------------------------------------------------------------------
private void button2_Click(object sender, EventArgs e)
{
//string dbPath = Path.Combine(@"\OAK\OCH-POS\", @"testing.db");
string dbPath = Path.Combine("\\OAK\OCH-POS\", @"testing.db");
string connString = string.Format("Data Source={0}", dbPath);
Debug.Print(connString);
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
StringBuilder query = new StringBuilder();
query.Append("SELECT * ");
query.Append("FROM TEST");
using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), conn))
{
conn.Open();
using (SQLiteDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Console.WriteLine("{0} {1} {2}",
dr.GetValue(0),
dr.GetValue(1),
dr.GetValue(2));
}
}
}
}
}
}
}
本地按钮输出完美:
Data Source=C:\Temp\testing.db
Native library pre-loader is trying to load native SQLite library "C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\bin\Debug\x86\SQLite.Interop.dll"...
'Oak-SQLite3.exe' (CLR v4.0.30319: Oak-SQLite3.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Oak-SQLite3.exe' (CLR v4.0.30319: Oak-SQLite3.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
1 John Doe
2 Jane Smith
局域网PC输出结果错误:
Data Source=\OAK\OCH-POS\testing.db
SQLite error (14): os_win.c:47433: (3) winOpen(C:\OAK\OCH-POS\testing.db) - The system cannot find the path specified.
SQLite error (14): cannot open file at line 47434 of [bed3e2b686]
Exception thrown: 'System.Data.SQLite.SQLiteException' in System.Data.SQLite.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>Oak-SQLite3.exe</AppDomain><Exception><ExceptionType>System.Data.SQLite.SQLiteException, System.Data.SQLite, Version=1.0.114.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139</ExceptionType><Message>unable to open database file</Message><StackTrace> at System.Data.SQLite.SQLite3.Open(String strFilename, String vfsName, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, Int32 maxPoolSize, Boolean usePool)
at System.Data.SQLite.SQLiteConnection.Open()
at Oak_SQLite3.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\Form1.cs:line 68
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp; m)
at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
at System.Windows.Forms.Button.WndProc(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Oak_SQLite3.Program.Main() in C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\Program.cs:line 19</StackTrace><ExceptionString>code = CantOpen (14), message = System.Data.SQLite.SQLiteException (0x800007FF): unable to open database file
at System.Data.SQLite.SQLite3.Open(String strFilename, String vfsName, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, Int32 maxPoolSize, Boolean usePool)
at System.Data.SQLite.SQLiteConnection.Open()
at Oak_SQLite3.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\Form1.cs:line 68
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp; m)
at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
at System.Windows.Forms.Button.WndProc(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Oak_SQLite3.Program.Main() in C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\Program.cs:line 19</ExceptionString></Exception></TraceRecord>
An unhandled exception of type 'System.Data.SQLite.SQLiteException' occurred in System.Data.SQLite.dll
unable to open database file
'Oak-SQLite3.exe' (CLR v4.0.30319: Oak-SQLite3.exe): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.Desktop.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[13128] Oak-SQLite3.exe' has exited with code -1 (0xffffffff).
我认为最相关的错误是:
- SQLite error (14): os_win.c:47433: (3) winOpen(C:\OAK\OCH-POS\testing.db) - 系统找不到指定的路径。
- SQLite 错误 (14):无法在 [bed3e2b686]
的第 47434 行打开文件
看来我的UNC路径Path.Combine("\\OAK\OCH-POS\", @"testing.db");正在转换进入 winOpen(C:\OAK\OCH-POS\testing.db) 我不知道为什么。
当我将一个驱动器映射到一个字母时,我得到了这个工作但是,这样做会使我的客户端机器上的其他程序崩溃,所以我真的很想避免这种情况。 事实上,我不得不删除映射,因为其他程序会混淆。
有人知道我该如何解决这个问题吗?
谢谢你,杰森
我 运行 遇到了同样的问题。在我的实验中,我发现将双反斜杠更改为三重反斜杠是可行的。显然,System.Data.SQLite 库中存在重大错误。
Bob Hodge 是对的,您必须更改此行:
string dbPath = Path.Combine("\\\OAK\\OCH-POS\\", @"testing.db");
至
string dbPath = Path.Combine("\\\\\\OAK\\OCH-POS\\", @"testing.db");
或
string dbPath = Path.Combine(@"\\\OAK\OCH-POS\", @"testing.db");
我在使用打开两个 SQLite 文件并读取内容的非常简单的 C# 程序时遇到问题。
当数据库文件在本地机器上时程序工作正常(按钮 1),但当文件在另一台 LAN PC 上时我无法让它工作(按钮 2)(两者 运行 Windows10).
我正在使用 Visual Studio 2019 编写这个 Windows 使用 .Net Framework 4.7.2 的应用程序
我的代码很简单。这是两个功能相同的按钮,但一个用于本地文件,另一个用于 LAN PC 上的文件。这是我的代码:
using System;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Oak_SQLite3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//-----------------------------------------------------------------------------
// Local Database
//-----------------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
string dbPath = Path.Combine(@"C:\Temp\", @"testing.db");
string connString = string.Format("Data Source={0}", dbPath);
Debug.Print(connString);
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
StringBuilder query = new StringBuilder();
query.Append("SELECT * ");
query.Append("FROM TEST");
using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), conn))
{
conn.Open();
using (SQLiteDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Console.WriteLine("{0} {1} {2}",
dr.GetValue(0),
dr.GetValue(1),
dr.GetValue(2));
}
}
}
}
}
//-----------------------------------------------------------------------------
// LAN PC Database (Named Oak with OCH-POS share)
//-----------------------------------------------------------------------------
private void button2_Click(object sender, EventArgs e)
{
//string dbPath = Path.Combine(@"\OAK\OCH-POS\", @"testing.db");
string dbPath = Path.Combine("\\OAK\OCH-POS\", @"testing.db");
string connString = string.Format("Data Source={0}", dbPath);
Debug.Print(connString);
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
StringBuilder query = new StringBuilder();
query.Append("SELECT * ");
query.Append("FROM TEST");
using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), conn))
{
conn.Open();
using (SQLiteDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Console.WriteLine("{0} {1} {2}",
dr.GetValue(0),
dr.GetValue(1),
dr.GetValue(2));
}
}
}
}
}
}
}
本地按钮输出完美:
Data Source=C:\Temp\testing.db
Native library pre-loader is trying to load native SQLite library "C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\bin\Debug\x86\SQLite.Interop.dll"...
'Oak-SQLite3.exe' (CLR v4.0.30319: Oak-SQLite3.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Oak-SQLite3.exe' (CLR v4.0.30319: Oak-SQLite3.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
1 John Doe
2 Jane Smith
局域网PC输出结果错误:
Data Source=\OAK\OCH-POS\testing.db
SQLite error (14): os_win.c:47433: (3) winOpen(C:\OAK\OCH-POS\testing.db) - The system cannot find the path specified.
SQLite error (14): cannot open file at line 47434 of [bed3e2b686]
Exception thrown: 'System.Data.SQLite.SQLiteException' in System.Data.SQLite.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>Oak-SQLite3.exe</AppDomain><Exception><ExceptionType>System.Data.SQLite.SQLiteException, System.Data.SQLite, Version=1.0.114.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139</ExceptionType><Message>unable to open database file</Message><StackTrace> at System.Data.SQLite.SQLite3.Open(String strFilename, String vfsName, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, Int32 maxPoolSize, Boolean usePool)
at System.Data.SQLite.SQLiteConnection.Open()
at Oak_SQLite3.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\Form1.cs:line 68
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp; m)
at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
at System.Windows.Forms.Button.WndProc(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Oak_SQLite3.Program.Main() in C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\Program.cs:line 19</StackTrace><ExceptionString>code = CantOpen (14), message = System.Data.SQLite.SQLiteException (0x800007FF): unable to open database file
at System.Data.SQLite.SQLite3.Open(String strFilename, String vfsName, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, Int32 maxPoolSize, Boolean usePool)
at System.Data.SQLite.SQLiteConnection.Open()
at Oak_SQLite3.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\Form1.cs:line 68
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message&amp; m)
at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
at System.Windows.Forms.Button.WndProc(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Oak_SQLite3.Program.Main() in C:\Users\Jason\Documents\Visual Studio 2019\Projects\Oak-SQLite3\Oak-SQLite3\Program.cs:line 19</ExceptionString></Exception></TraceRecord>
An unhandled exception of type 'System.Data.SQLite.SQLiteException' occurred in System.Data.SQLite.dll
unable to open database file
'Oak-SQLite3.exe' (CLR v4.0.30319: Oak-SQLite3.exe): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio19\Professional\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.Desktop.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The program '[13128] Oak-SQLite3.exe' has exited with code -1 (0xffffffff).
我认为最相关的错误是:
- SQLite error (14): os_win.c:47433: (3) winOpen(C:\OAK\OCH-POS\testing.db) - 系统找不到指定的路径。
- SQLite 错误 (14):无法在 [bed3e2b686] 的第 47434 行打开文件
看来我的UNC路径Path.Combine("\\OAK\OCH-POS\", @"testing.db");正在转换进入 winOpen(C:\OAK\OCH-POS\testing.db) 我不知道为什么。
当我将一个驱动器映射到一个字母时,我得到了这个工作但是,这样做会使我的客户端机器上的其他程序崩溃,所以我真的很想避免这种情况。 事实上,我不得不删除映射,因为其他程序会混淆。
有人知道我该如何解决这个问题吗?
谢谢你,杰森
我 运行 遇到了同样的问题。在我的实验中,我发现将双反斜杠更改为三重反斜杠是可行的。显然,System.Data.SQLite 库中存在重大错误。
Bob Hodge 是对的,您必须更改此行:
string dbPath = Path.Combine("\\\OAK\\OCH-POS\\", @"testing.db");
至
string dbPath = Path.Combine("\\\\\\OAK\\OCH-POS\\", @"testing.db");
或
string dbPath = Path.Combine(@"\\\OAK\OCH-POS\", @"testing.db");