你能幫助我嗎?我需要連接到SqlServer 2016上的本地數據庫,但是當我嘗試打開連接時,出現此錯誤。我不嘗試其他方式登錄,因爲我不記得密碼(我總是用Windows驗證。)本地SqlServer 2016 - 登錄失敗
其他信息:無法打開數據庫「MY_PROJECT」登錄所請求的 。登錄失敗。
用戶'DESKTOP-C448DNE \ Denis'登錄失敗。
這裏是我的C#代碼,我用:
public class DbHelper
{
public SqlConnection Connection { get; private set; }
public string Language { get; set; }
public DbHelper()
{
Connection = new SqlConnection();
Language = "en";
}
public bool Connect(string conString)
{
if (Connection.State != ConnectionState.Open)
{
Connection.ConnectionString = conString;
Connection.Open();
}
return true;
}
public bool Connect()
{
string connString = "Data Source=(localdb)\\MSSQLLocalDB; Initial Catalog = My_Project; Integrated Security=true;";
return Connect(connString);
}
public void Close()
{
Connection.Close();
}
public int ExecuteNonQuery(SqlCommand sqlCommand)
{
int rowNumber = 0;
try
{
rowNumber = sqlCommand.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
return rowNumber;
}
}
在這裏,我使用這個類:
...
private DbHelper _db;
public SqlEmployeeMapper()
{
_db = new DbHelper();
}
public int Create(Employee entity)
{
var conn = _db.Connect();
string queryString = "insert into \"employees\" (first_name, last_name, email, phone_number, password, city, address, holiday, role_id, departmant_id)" +
"values(@first_name, @last_name, @email, @phone_number, @password, @city, @address, @holiday, @role_id, @departmant_id);";
SqlCommand command = new SqlCommand(queryString);
PrepareCommand(command, entity);
var id = _db.ExecuteNonQuery(command);
_db.Close();
return id;
}
...
編輯: 我嘗試打開服務器資源管理器在VS2015,然後單擊「添加連接「...我輸入」(localdb)\ MSSQLLocalDB「作爲服務器名稱,當我打開數據庫的下拉列表時,看不到我的數據庫
您是否安裝了SSMS?如果是的話,你可以通過它連接? –
聽起來像您需要爲您的WIndows帳戶創建登錄 – stuartd
@ destination-data是SSMS正常工作 –