Win32Exception:系统找不到指定的文件



我正试图在执行登录功能的.NET web服务中打开一个用SQLite编写的数据库(位于localhost中(。数据库有一个名为user的表,我希望从中访问用户名和密码。我使用SqlDataAdapter和SQLDataSource来访问用户名和密码。但当我运行登录Web服务时,我会收到以下错误:

Message=在建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到或无法访问服务器。请验证实例名称是否正确,以及SQL Server是否已配置为允许远程连接。(提供程序:命名管道提供程序,错误:40-无法打开与SQL Server的连接(

Source=.Net SqlClient数据提供程序

内部异常1:

Win32Exception:系统找不到指定的文件

尽管我已将数据库文件放在bin文件夹中,但仍显示错误。Visual Studio指向这条线:

da.Fill(ds);

WebService.cs:

namespace loginwebservice
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public DataSet login(string uname, string pwd)
{
SqlDataAdapter da = new SqlDataAdapter("select * from user where user_name = '" + uname + "' and user_password='" + pwd + "' ;",
@"Data Source=localhost;Initial Catalog=EMP;Integrated Security=True");
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}

Web.Debug.Config中指定的连接字符串为:

<connectionStrings>
<add name="UserDataManager" 
connectionString="Data Source=localhost;Integrated Security=True" 
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

如何克服此错误并访问数据库?

如果要使用SQLiteDB,则不能使用SqlDataAdapter类。

SqlDataAdapter类用于连接SQLServer

以下是关于C#中SQLLite的链接

如果你用这个图书馆。这里有一些示例代码。

SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=localhost;Initial Catalog=EMP;Integrated Security=True");
m_dbConnection.Open();
SQLiteDataAdapter myAdapter = new SQLiteDataAdapter("select * from user where user_name = '" + uname + "' and user_password='" + pwd + "' ;", m_dbConnection);
DataSet ds = new DataSet();
myAdapter.Fill(ds);
m_dbConnection.Close();

你可以试试这个。

namespace loginwebservice
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
string cn = ConfigurationManager.ConnectionStrings["UserDataManager"].ConnectionString;
SQLiteConnection con = new SQLiteConnection(cn)
[WebMethod]
public DataSet login(string uname, string pwd)
{
SQLiteDataAdapter da = new SQLiteDataAdapter("select * from user where user_name = '" + uname + "' and user_password='" + pwd + "' ;",con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}

<connectionStrings>
<add name="UserDataManager" 
connectionString="Data Source=localhost; Initial Catalog=EMP; Integrated 
Security=True" 
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

最新更新