【发布时间】:2017-05-10 11:05:51
【问题描述】:
我想使用ASP.NET 中的网络服务从我的应用程序中检索所有注册用户。但是,我的连接字符串有问题。
public static String getRegisteredUsers(string usernameCode)
{
string username = "";
SqlConnection con = new SqlConnection("MyDatabaseConnectionString1");
SqlCommand cmd = new SqlCommand("Select Username from Users where usernameCode = '" + usernameCode.ToUpper() + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
username = dr["username"].ToString();
}
dr.Close();
con.Close();
return username;
}
这个MyDatabaseConnectionString1 是我用于要从中检索信息的表的连接字符串。它适用于该表,但不适用于 Web 服务。
<add name="MyDatabaseConnectionString1" connectionString= "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Korisnik\Desktop\Fitness\App_Data\MyDatabase.mdf;Integrated Security=True"/>
我应该为 Web 服务使用相同的连接字符串还是以某种不同的方式完成?
【问题讨论】:
-
连接字符串缺少初始目录?查看MSDN page. 但是,除非您在该服务器上有多个数据库,否则it shouldn't matter...possibly?
-
SqlConnection 需要连接字符串,而不是连接字符串的名称。
标签: c# asp.net web-services