Search code examples
c#sqlsql-serverpublish

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?


Solution

  • 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