0

I am writing code for a small company that is all about purchase and sell. I wrote C# code with using external database (SQL Server 2014), now it's time to make the exe file of that code.

I tried my best but it doesn't work.

How can I connect SQL Server 2014 database files with my application so that when someone installs it on his computer he also got SQL Server connected with that application?

6
  • Its all in the connection string, but your customer will have to install/configure an SQL server first. Aside from that its hard to help answer your question unless you tell us what problems you are having. Commented Aug 3, 2015 at 17:58
  • 1
    You need to install a version of SQL Server as well on that machine. If you're going to use an existing installation of SQL Server you need to build configuration dialogs into the setup tool to input authentication information like username and password (or use integrated security), as well as the ip address, possibly port, etc. of the sql server instance to use. This is a really broad topic, perhaps you should start with how far you got? Commented Aug 3, 2015 at 17:58
  • i have installed sql server 2014 in my computer and i developed custom software that use database from sql server... now i am going to make its exe file.. is it possible that my exe file will work correctly when in install it on any other PC (that is actually my question) Commented Aug 3, 2015 at 18:12
  • No, because if they also don't have an SQL server, you making your program "an exe" doesn't mean it automatically installs SQL server too. There is no guarantee that it will work on "any other PC" because it may not have the same SQL Server installed. Commented Aug 3, 2015 at 18:13
  • 1
    You need to ensure that either your own setup program installs SQL Server, or that the customer/client themselves do that. Commented Aug 3, 2015 at 18:26

2 Answers 2

1

Hi actually we deploy code as setup file (.msi), you can create setup project using Wix or install-shield ( maybe other too,but these two are most popular and widely used)

You can do database deployment in two ways

1.) you can provide SQL script for generating SQL server and give instruction for generating Database from Script. It is simple and easy to do.

2.) Other way is provide option to create database from setup. It need some work, but it is more good way.

Now come to your question, you can provide some UI in setup that take input from user and connect to appropriate DB. Or create a UI for Updating configurations, and save configuration (connection string ) in some file ( ex: .ini or .config file).

Below are the some URL for creating Setup and connecting to Db using Wix:

WIXDataBase

installing-databases-using-wix

Creating-an-installer-using-Wix

using-wix-to-install-sql-databases-and-execute-sql-scripts

WiX Samples

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

Comments

1

Ideally you'd have a SQL server set up somewhere. If not you'll have to install sql server. In your app it's just a matter of setting up the connection string and running sql commands against it.

string connectionString = "Data Source=server //rest of connection string"

Then in c# you can do

using (SqlConnection con = new SqlConnection(connectionString))
{
    using (SqlCommand command = new SqlCommand("SELECT TOP 100 * FROM SomeTable", con))
    using (SqlDataReader reader = command.ExecuteReader())
    {
    while (reader.Read())
    {
        Console.WriteLine("{0} {1} {2}",
        reader.GetString(0), reader.GetString(1), reader.GetString(2));
    }
    }

 }

GetString can be changed to GetInt or whatever the datatable will be. This should be enough information to get you started. If you know the structure of the database its simple to put the results into your classes for the application.

1 Comment

Sample connection string for a local sqlexpress would be: connectionString = "Data Source=MyPCsNAME\SQLEXPRESS;Integrated Security = true;";

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.