简体   繁体   中英

Visual studio 2012 publish project c# with external DataBase

I need your help guys on how to publish correctly a project that I have created on Visual Studio.

Here is the thing my project runs successfully on my local computer (and also the publish file) but when I try to run it (for test purposes to a different computer I receive error messages due to sql connection). My application is connected to Sql Database with a connection string (Data Source= HP-PC\\SQLEXPRESS;Initial Catalog=Stock;Integrated Security=True).

I handle the database with Sql Manager, and here is some of the errors that I receive when I try to run my application to another computer:

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

propably as I understand my publish option was not contain at all the db.

How can I publish my project correctly with the external database? or is better to include my db inside the project?

The English error (using Google translate) is:

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)

This means that your application is not able to connect to your SQL Server. The first thing to check would be:

Are you able to connect to the SQL Server manually from the machine in which you have deployed your application ?

It is likely that the SQL Server is not configured to accept connections from remote machines.

There is a good article on step-by-step ways to troubleshoot the connection issue:

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

Hi guys after a lot of search and testing I found the best solution to my issue and I want to share with you. Well the problem was the server name and nothing else since (I don't any user name or password) so in my code I just change a single value on connection string from

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

To

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

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

and everything is fine now I can test my app to any computer!!!!

Thank you guys for your time and help,

Friendly regards, Jim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM