当我尝试打开SQL Server 2014数据库时,出现错误
无法打开登录请求的数据库“EmployeeDB”。登录失败。
我可以打开我的SQL Server并创建数据库和表,但是当我尝试从Visual Studio连接它时,我收到此错误。
请帮助我,我想练习,所以我不希望这种情况长时间阻止我。
我在Visual Studio中编写此代码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please Enter the Employee Id of whose Details has to be Updated");
int Id = int.Parse(Console.ReadLine());
SqlConnection conn = new SqlConnection("server=.;database=EmployeeDB;integrated security=true");
string Query = "select * from EmployeeInfo where Id=" + Id;
SqlCommand cmd = new SqlCommand(Query, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
bool isRecordExists = dr.Read();
if (isRecordExists)
{
Console.WriteLine("Employee Details are : \n");
Console.WriteLine("Id is : {0}, Name is : {1}, Location is : {2} and Salary is : {3}",
dr[0], dr[1], dr[2], dr[3]);
}
else
{
Console.WriteLine("No Employee Exists with the Above Entered Id");
}
conn.Close();
if (isRecordExists)
{
Console.WriteLine("Please Enter the Updated Name");
string Name = Console.ReadLine();
Console.WriteLine("Please Enter Updated Location");
string location = Console.ReadLine();
Console.WriteLine("Please Enter Updated Salary");
int salary = int.Parse(Console.ReadLine());
cmd.CommandText = "UpdateEmployee";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Id", Id));
cmd.Parameters.Add(new SqlParameter("@Name", Name));
cmd.Parameters.Add(new SqlParameter("@Location", location));
cmd.Parameters.Add(new SqlParameter("@Salary", salary));
conn.Open();
int noOfRowsAffected = cmd.ExecuteNonQuery();
conn.Close();
if (noOfRowsAffected > 0)
{
Console.WriteLine("Data Updated Successfully");
}
else
{
Console.WriteLine("Updation failed");
}
}
}
}
提前致谢
答案 0 :(得分:0)
见https://www.connectionstrings.com/sqlconnection/。 如果您没有启用集成安全性或可信连接,则应传递用户ID和密码属性。