我试图在一个执行登录功能的SQLite web服务中打开一个用.NET编写的数据库(在本地主机中)。数据库有一个名为user的表,我希望从该表访问用户名和密码。为此,我使用SqlDataAdapter和SQLDataSource访问用户名和密码。但是,当我运行登录get服务时,我会得到以下错误:
在建立到Server的连接时,发生了与Message=A网络相关或特定于实例的错误。找不到或无法访问服务器。验证实例名是否正确,以及Server是否配置为允许远程连接。(提供者:命名管道提供程序,错误: 40 -无法打开到Server的连接) Source=.Net SqlClient数据提供程序 内部例外1: Win32Exception:系统找不到指定的文件
虽然我已将数据库文件放置在bin文件夹中,但仍会出现错误。Visual指向这一行:
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>如何克服此错误并访问数据库?
发布于 2018-07-10 11:51:57
你可以试试这个。
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>
发布于 2018-07-10 11:53:11
要使用SQLite DB,就不能使用SqlDataAdapter类。
SqlDataAdapter类用于与SQLServer的连接
下面是关于SQLLite in C#的链接
如果你用这个Libary。这是一些给你的示例代码。
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();https://stackoverflow.com/questions/51264207
复制相似问题