我试图在执行登录功能的.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>
如何克服此错误并访问数据库?
答案 0 :(得分:1)
您要使用SQLite
数据库,而不能使用SqlDataAdapter
类。
SqlDataAdapter
类用于与SQLServer
这里是有关SQLLite in C#
的链接讨论如果您使用此库。这是一些示例代码。
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();
答案 1 :(得分:0)
您可以尝试一下。
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>