无法连接到本地数据库 SQL

Unable to connect to local database SQL

我用 SQL 服务器创建的本地数据库(本地数据库)有问题。 我可以连接到我计算机上的数据库,但是如果我尝试连接到另一台计算机,我会收到以下错误消息:

我想要一个本地数据库来存储数据,我不需要服务器来管理数据库。

这是我的连接字符串:

`<connectionStrings>
    <add name="stocksDB" connectionString="Data Source= (LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\myDB.mdf;Integrated Security=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>`

我已将 "SQL Server 2012 Express LocalDB" 包含在先决条件中。

我做错了什么?

如果你有两台计算机(假设它们的机器名称是 "pc_a" 和 "pc_b",它们联网在一起,程序在计算机 "pc_a" 上是 运行 并且数据库驻留在计算机 "pc_b" 上,那么您的连接字符串需要包含计算机 "pc_b" 的机器名称。

即使是本地机器,您也可以提供机器名称,因此如果程序 运行 与数据库在同一台机器上,或者程序是 运行 在一台机器上,而数据库在另一台机器上,只要两台机器联网并且您所在的帐户 运行 下的程序就可以访问机器、实例和数据库。

请注意在下面的示例中,安装 SQL 时使用了 "default" 实例名称 (MSSQLSERVER)。当数据库实例名称是默认名称时,您不得显式提供实例名称(如果这样做,您将收到显示的错误)。您唯一一次明确提供实例名称是当它不是默认实例名称时。下面的代码可以处理这两种情况(通过将 dbInstanceName 变量设置为“”或实例名称,例如“\SQLEXPRESS”)。参见 S.O。 。当它怀疑时,尝试一个空的实例名称和一个您认为是实例名称的名称,看看有什么用。

string databaseMachineName = "pc_b";
string databaseInstanceName = ""; 
string dbName = "stocksDb";

using (SqlConnection sqlConnection = new SqlConnection("Data Source=" + databaseMachineName + databaseInstanceName + "; Initial Catalog=" + dbName + "; Integrated Security=True;Connection Timeout=10"))
{
   .
   .
   .
}

已解决!问题是另一台计算机上的 SQL 服务器版本错误。在我的主计算机上,我有 SQL Server 2014,在另一台计算机上有 2012 版本,所以 "database instance name" 是不同的。感谢@Nova Sys Eng 的输入!

现在我更改了连接字符串: 首先,我使用代码检索计算机上安装的所有 SQL 服务器实例,如 Nova Sys Eng 发布的 link 中所述。

   var instances = GetLocalDBInstances();
   var connString= string.Format("Data Source= (LocalDB)\{0};AttachDbFilename=|DataDirectory|\myDB.mdf;Integrated Security=True;",instances[0]);

 internal static List<string> GetLocalDBInstances()
        {
            // Start the child process.
            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.Arguments = "/C sqllocaldb info";
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            p.Start();
            // Do not wait for the child process to exit before
            // reading to the end of its redirected stream.
            // p.WaitForExit();
            // Read the output stream first and then wait.
            string sOutput = p.StandardOutput.ReadToEnd();
            p.WaitForExit();

            //If LocalDb is not installed then it will return that 'sqllocaldb' is not recognized as an internal or external command operable program or batch file.
            if (sOutput == null || sOutput.Trim().Length == 0 || sOutput.Contains("not recognized"))
                return null;
            string[] instances = sOutput.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            List<string> lstInstances = new List<string>();
            foreach (var item in instances)
            {
                if (item.Trim().Length > 0)
                    lstInstances.Add(item);
            }
            return lstInstances;
        }