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

C# Win32Exception:系统找不到指定的文件,c#,asp.net,C#,Asp.net,我试图在一个.NETWeb服务中打开一个用SQLite编写的数据库(存在于localhost中),该数据库执行登录功能。数据库有一个名为user的表,我希望从中访问用户名和密码。为此,我使用SqlDataAdapter和SQLDataSource访问用户名和密码。但是,当我运行登录Web服务时,会出现以下错误: Message=建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(

我试图在一个.NETWeb服务中打开一个用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服务
{
/// 
///服务1的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
//[System.Web.Script.Services.ScriptService]
公共类服务1:System.Web.Services.WebService
{
[网络方法]
公共数据集登录(字符串uname、字符串pwd)
{
SqlDataAdapter da=new SqlDataAdapter(“从用户中选择*,其中用户名=”+uname+”,用户密码=“+pwd+”;”,
@“数据源=本地主机;初始目录=EMP;集成安全性=True”);
数据集ds=新数据集();
da.填充(ds);
返回ds;
}
}
}
Web.Debug.Config中指定的连接字符串为:

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

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

您可以尝试此方法

 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;
        }
    }
}
命名空间登录Web服务
{
/// 
///服务1的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
//[System.Web.Script.Services.ScriptService]
公共类服务1:System.Web.Services.WebService
{
字符串cn=ConfigurationManager.ConnectionString[“UserDataManager”].ConnectionString;
SQLiteConnection con=新的SQLiteConnection(cn)
[网络方法]
公共数据集登录(字符串uname、字符串pwd)
{
SQLiteDataAdapter da=new SQLiteDataAdapter(“从用户中选择*,其中用户名=”+uname+“,用户密码=”“+pwd+”;”,con);
数据集ds=新数据集();
da.填充(ds);
返回ds;
}
}
}

如果要使用
SQLite
DB,则不能使用
SqlDataAdapter

SqlDataAdapter
类用于连接
SQLServer

这里有一个关于

如果你用这个图书馆。下面是一些示例代码

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();

在Web.Debug.Config文件的连接字符串中,缺少初始目录属性。谢谢@shamishikh,但即使在添加初始目录后,我也会收到相同的错误。我编辑了我的答案,因为sqlLite有SQLiteDataAdapter。尝试this@User98SQLite不是SQL Server。您不能使用这些类中的任何一个。安装并使用类似以下代码的SQLite提供程序修复了旧问题,但我现在遇到了一个新错误:System.NullReferenceException:“对象引用未设置为对象的实例。”System.Configuration.ConnectionStringSettingsCollection.This[string]。get返回null。如何修复此问题?您是否更改了配置文件?