您好我正在尝试使用以下代码连接到数据库。但我收到一条错误消息
无法打开登录请求的数据库“CPL”。登录失败。 用户'Dell-PC \ Dell'登录失败。
以前工作正常。
C#代码
protected void Page_Load(object sender, EventArgs e)
{
string CurrentDate = DateTime.Today.ToShortDateString();
string CS = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
DataTable dtMatchDetails = new DataTable();
string query = "Select MatchTeam1,MatchTeam2 from tblSchedule Where MatchDate =" + CurrentDate;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtMatchDetails);
con.Close();
da.Dispose();
}
Web.Config中的连接字符串
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;User Instance=true;database = CPL"
providerName="System.Data.SqlClient=" />
</connectionStrings>
答案 0 :(得分:0)
这意味着您的连接字符串正常。用户和密码都可以。
您需要授予对特定数据库的访问权限。
例如,以管理员身份登录sql server后:
USE CPL
EXEC sp_addrolemember 'db_datareader', '(your user name)'