Alena
Alena

Reputation: 1214

C# application deployment issue with .mdf database

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:

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)

But none of this work for me.

NOTE:

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.

Upvotes: 0

Views: 787

Answers (2)

Atlas_Gondal
Atlas_Gondal

Reputation: 2552

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! :)

Upvotes: 1

Element Zero
Element Zero

Reputation: 1751

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;

Upvotes: 0

Related Questions