0

I am working on a C# database application for learning and I'm trying to deploy it on client machine and getting connection problem.

//NOTE: path and database variables have correct info because it works on my dev machine
"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=" + path + "\\" + databaseName + ";Integrated Security=True"

I am using Install Shield for creating setup and tried Advance Installer also.

On the other test machine I have installed:

  • .Net Framework 4.5.2
  • SQLLocalDB.msi (x64 bit)

Lastly, I installed my deployed setup file and I was getting an Exception:

System.Data.SqlClient.SqlException (0x80131904). A network related or instance-specific error occured while establishing a connection to SQL server. 
The server was not found or not accessible.
Localdatabase Runtime error occured.

I also tried these in Connection String: (found in similar question on stackoverflow)

  • localhost
  • localhost\\SQLExpress
  • .\\SQLExpress

But none of this work for me.

NOTE:

  • My LocalDB connection string is working on dev machine.
  • Using Visual Studio 2015 Enterprise

PS: What I am trying to learn is a way to create an installer which installs some per-requeisites like .Net Framework, LocalDB etc and can run database based application on client machines without installing SQL Server separately. I am not sure if the .mdf way is the good fit for this or not.

2 Answers 2

1

I would suggest you to use SQLite Database because the process and function are almost similar. And the deployment is super easy; it just requires few DLLs to make it work.

Guideline:

First you will need System.Data.SQLite library and then needs to add System.Data.SQLite.dll as a reference in your project. Keep in mind that SQLite.Interop.dll also needs to be in your executables directory but doesn’t need to be added as a reference in your project.

Moreover, if your application is targeting Any CPU it is likely that you will get an exception. So make sure to navigate to Project properties -> Build and set the Platform target to the bit version of the System.Data.SQLite.dll binary you have downloaded.

As you are beginner so here is the step by step guideline along with sample code.

Steps:

  1. Navigate to: Tools > NuGet Package Manager > Manage NuGet Packages for Solution
  2. Search For: SQLite
  3. Select System.Data.SQLite (would be first result)
  4. Click on Install
  5. Import library

    using System.Data.SQLite;
    

The connection is ready and here is the Sample Code:

System.Data.SQLite.SQLiteConnection.CreateFile("sqlite.db3");

using(System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=sqlite.db3")){
    using(System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(conn)){
        conn.Open();
        cmd.CommandText = @"CREATE TABLE IF NOT EXISTS
                            [persons](
                            [id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                            [name] VARCHAR(50) NULL
                            )";

        cmd.ExecuteNonQuery();

        cmd.CommandText = "INSERT INTO [persons] (name) values('Atlas')";
        cmd.ExecuteNonQuery();

        cmd.CommandText = "SELECT * FROM [persons]";

        using(System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader()){
            while(reader.Read()){
                MessageBox.Show(reader["name"].ToString());
            }

        conn.Close();

        }

    }
}

Tips:

  • always use try/catch block for database operations
  • If you don't prefer to use NuGet Package Manager then you can download library from here

Hope this helps! :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Atlas_Gondal, I will try this method and update back.
Thank you so much for step by step guide. I was able to deploy my first database application.
0

Does one of these help?

Attach a database file on connect to a local SQL Server Express instance

Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;

Attach a database file, located in the data directory, on connect to a local SQL Server Express instance

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;
Trusted_Connection=Yes;

3 Comments

As mentioned in question, I have tried these method but no luck :(
It's hard to say what the issue is without seeing more information then. I would wonder whether SQLExpress was installed properly. Does it show up under Windows Services? Also I would probably advise something like SQLite which would need no install and would be easy to package with a C# project BUT it might not fit all your needs - just something to look into.
what more information do you need??? SQL Server VSS Writer service is installed and the current status is started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.