无法打开数据库 - 登录请求 - 为什么我无法连接到我的数据库?

6 c# sql sql-server ado.net connection-string

我正在尝试将一些数据插入我的数据库(Microsoft SQL Server)

我的连接没有打开,我收到此消息:

无法打开登录请求的数据库"[销售和库存系统] \".登录失败.\ r \n登录用户'Mostafa-PC\Mostafa'失败.

这是我的代码:

public void InsertProduct(List<string> _myName, 
                      List<int> _myAmount, 
                      List<int> _myPrice, string _date)
{
string connectionString = @"Data Source=MOSTAFA-PC;Initial Catalog=[Sales and Inventory System];Integrated Security=True";
string query = @"INSERT INTO dbo.product(Name, Amount, Price, [date]) 
                             VALUES(@Name, @Amount, @Price, @Date);";

using (SqlConnection Con = new SqlConnection(connectionString))
using (SqlCommand Cmd = new SqlCommand(query, Con))
{

    Cmd.Parameters.Add("@Name", SqlDbType.NVarChar);
    Cmd.Parameters.Add("@Amount", SqlDbType.Int);
    Cmd.Parameters.Add("@Price", SqlDbType.Int);
    Cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = Convert.ToDateTime(_date);

    Cmd.Connection = Con;
    Con.Open();

    int recordsToAdd = _myName.Count();
    for(int x = 0; x < recordsToAdd; x++)
    {
        Cmd.Parameters["@Name"].Value = _myName[x];
        Cmd.Parameters["@Amount"].Value = _myAmount[x];
        Cmd.Parameters["@Price"].Value = _myPrice[x];
        Cmd.ExecuteNonQuery();
    }
}
Run Code Online (Sandbox Code Playgroud)

我做了一切,我到处搜寻.我无法弄清楚为什么.

ped*_*ram 2

看来您需要检查以下内容来解决您的错误。

1. 检查权限。用户必须有权访问此Database.Most Important One

2.另外,最好能访问 App.config key代码端。因此,声明连接字符串app.config并尝试如下设置。

string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["keyname"]
Run Code Online (Sandbox Code Playgroud)

3.您还需要尝试连接实例,SSMS并且不应失败。

链接可以帮助您了解更多信息。