Visual studio 2012 发布带有外部数据库的项目 c#

Visual studio 2012 publish project c# with external DataBase

我需要你们帮助我如何正确发布我在 Visual Studio 上创建的项目。

这是我的项目 运行 在我的本地计算机(以及发布文件)上成功运行的东西,但是当我尝试 运行 它时(出于测试目的,我收到了另一台计算机由于 sql 连接导致的错误消息)。 我的应用程序使用连接字符串连接到 Sql 数据库(数据源= HP-PC\SQLEXPRESS;初始目录=Stock;集成安全性=True)。

我使用 Sql 管理器处理数据库,这里是我尝试 运行 我的应用程序到另一台计算机时收到的一些错误:

System.Data.SqlClient.SqlException (0x80131904): Παρουσιάστηκε ένα σφάλμα δικτύου ή ένα σφάλμα που αφορά μια συγκεκριμένη παρουσία κατά τη δημιουργία μιας σύνδεσης σε SQL Server. Δεν ήταν δυνατή η εύρεση του διακομιστή ή ο διακομιστής δεν ήταν προσβάσιμος. Βεβαιωθείτε ότι το όνομα παρουσίας είναι σωστό και ότι ο SQL Server έχει ρυθμιστεί ώστε να επιτρέπει τις απομακρυσμένες συνδέσεις. (provider: SQL Network Interfaces, error: 26 - Προσδιορίστηκε σφάλμα εντοπισμού διακομιστή/περιόδου λειτουργίας)
   σε System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   σε System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   σε System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   σε System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   σε System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   σε System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   σε System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   σε System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   σε System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   σε System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   σε System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   σε System.Data.SqlClient.SqlConnection.Open()
   σε System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
Error Number:-1,State:0,Class:2

据我所知,我的发布选项可能根本不包含数据库。

如何使用外部数据库正确发布我的项目?或者最好将我的数据库包含在项目中?

英文错误(使用Google翻译)是:

There was a network error or a specific instance error when you created a connection to SQL Server. The server could not be found or the server was not accessible. Make sure the instance name is correct and that SQL Server is configured to allow remote connections. (Provider: SQL Network Interfaces, error: 26 - Server / session detection error)

这意味着您的应用程序无法连接到您的 SQL 服务器。首先要检查的是:

您能否从部署应用程序的计算机手动连接到 SQL 服务器?

可能 SQL 服务器未配置为接受来自远程计算机的连接。

有一篇关于解决连接问题的分步方法的好文章:

https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/troubleshoot-connecting-to-the-sql-server-database-engine

大家好,经过大量搜索和测试,我找到了解决我的问题的最佳方法,我想与大家分享。 好吧,问题是服务器名称,因为(我没有任何用户名或密码)所以在我的代码中我只是更改连接字符串 from[=12= 上的单个值]

"Data Source= HP-PC\SQLEXPRESS;Initial Catalog=Stock;Integrated Security=True"

string hostName = System.Net.Dns.GetHostName();

"Data Source= '"+hostname+'"\SQLEXPRESS;Initial Catalog=Stock;Integrated Security=True"

一切正常,现在我可以在任何计算机上测试我的应用程序了!!!!

感谢大家的宝贵时间和帮助,

亲切的问候, 吉姆