我正在尝试连接到我在计算机上的 SQL Server 2008 中创建的数据库。我有一个 Windows 身份验证登录,如下所示:
try
{
string sCon = @"Data Source=David-PC\SQLEXPRESS; Initial Catalog=ZarTrackDB; Integrated Security = true";
SqlConnection dbConn;
dbConn = new SqlConnection(sCon);
dbConn.Open();
}
catch (Exception ex)
{
MessageBox.Show(this, "An error occurd while attemtpting to connect to DB.\n" + ex.Message, "DB Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
但是它不断抛出异常:
用户 David-PC\David 登录失败
我可以连接到主数据库:
string sCon = @"Data Source=David-PC\SQLEXPRESS; Initial Catalog=Master; Integrated Security = true";
所以我想问题可能是我没有权限?尽管当我通过 SQL Server Management Studio 登录时,我可以如何在 ZarTrackDB 上完美地插入创建查询等,但我该如何授予 myslef 访问该数据库的权限。
我真的不知道该怎么办了,请帮忙!
编辑:
这是我尝试登录的帐户的用户映射的屏幕截图:
用户映射:
服务器角色:
更新:
如果我登录到 master 并尝试访问 ZarTrack :
string sCon = @"Data Source=David-PC\SQLEXPRESS; Initial Catalog=Master; Integrated Security = true";
SqlConnection dbConn;
dbConn = new SqlConnection(sCon);
dbConn.Open();
string sql = "USE ZarTrackDB "+
"CREATE TABLE Customers ("+
"CustomerID int IDENTITY PRIMARY KEY NOT NULL," +
"CompanyName varchar(30) NOT NULL,"+
"CompanyAddress varchar(40) NOT NULL,"+
"Email varchar(40) NOT NULL,"+
"PhoneNumber varchar(15) NOT NULL"+
");";
SqlCommand dbCmd = new SqlCommand();
dbCmd.CommandText = sql;
dbCmd.Connection = dbConn;
dbCmd.ExecuteNonQuery();
我得到例外: