I'm almost finish with my project that was made in visual studio 2008 using C#, so i want to ask this ahead of time.

I want the project to be an executable file, this is my first time so how could I do it?

One more thing, my form has SQLConnection strings that has a data source which is specific to a computername\\SQLEXPRESS. What if I make the project to exe, then use it to other computer? I believe it will produce an error.

Recommended Answers

All 30 Replies

That is useful and by reading some tutorials I know now how to build an executable file.

Though a question is still left unanswered. My SQL connection string is specific to a computername, what if I used the form to a different computer.

will create problem whenever u set up the project for over come u have to create connection string dynamicaly or better option create your database in your application
by right clicking your solution explorer and choose add new item and then database


there one more idea deatache your database whish your application use from server and add it in your project and configure your connection string according to that that will solve your problem try it

then what is the use of the sql database I made if I would just create the database within my application?

dear will use sql server but path will remain same every time you set up the project

if ok marked as solved dear

and thanks for raputation

I really appreciate the help! I'll report back as soon as I get home.

The .mdf file is the extension of the deattached database?

To expand on this a bit your two options are:

  • Create a flat-file database that is portable within the application
  • Create a database connection string that points to the server/db location where your db will be hosted (for multi-user/multi-machine programs you will want a centralized database to share information vs a localized db that is unique to each install)

The way you have it now, because you developed it off a db on your local machine, it's only set up to connect to that DB and only if the program is installed/run from that machine.

Something else to consider, if it's using computername\\SQLEXPRESS as it's connection, odds are it's also using windows authentication and so, if you host it off a centralized DB server, you will likely want to ensure it's using either un/pw style authentication or that you know how to set up accounts and permissions for individual login rights (easiest and common is to use the sa account to log into the DB unless you have groups setup within the db to accomodate different read/write/access rules for different categories of users which is likely far more advanced than your project requires). If you do set your connection string to include un/pw login you'll want to make sure that the connection string details are secure . Generally I bury it within a separate class and call it like so:

blConn loadConn = new blConn();
string connStr = @loadConn.cString;

Where blConn is a class containing a method cString to expose my connection string details.

The .mdf file is the extension of the deattached database?

Yes, it is :) Again, read my previous post regarding the pros and cons of using a localized flat db vs a centralized db depending on intended user distribution.

deatach it from database ans place some where in your system says desctop and add it in your project like riclick on sol explorer-> add existing item and browse that datbase once done check in your project opent it in server explorer

Create a flat-file database that is portable within the application

That is what I need.

This is what haren is telling me, right? That there are two way to do this, by creating a database in my application or deattach the database from sql and add it into my project.

Yes, if you want to use the existing DB as a flat-file db within your application you can follow hirenpatel's instructions there, just be sure to adjust your data sources and bindings to match the new db location instead of the old or you're in for a world of debugging hurt :twisted:

@Lusiphur, Then what will I put on data source if I do this? I only know that it must always be called computername\\sqlexpress, but I don't need to declare a computername if it will be a portable within my application.

Install the Server to your client system, take the path of the sql Express and then make the connection string accordingly, later make it as executable file.

I'm confused because of the fact that i was thinking about some other matter which is a built-in database within my application, wherein the datasource is universal. You know what I mean?

What I was thinking is an 'embedded' database within my application. Sorry guys, it didn't explain it well, but thanks for the help.

I tried detaching the database and opened the mdf file through visual studio. What now? How to enable the attached database with the use of connection string?

My connection string is

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=LibraryDB;Integrated Security=True;

What do I need to change or maybe add? It doesn't work anymore after detaching the database obviously.

Couple of reference points related to this topic:

Now I could be wrong here but it looks more and more likely that even though you are able to detatch and distribute your mdf file with your application it will still only be able to read the mdf if sql server (express, or whichever) is on your target machine as well. At least one of the above links does have a connection string sample of connecting to a local mdf file however.

Hope that helps :)

Ok, I accept the fact that I need to install the sql server, what I need is to attach my database to prevent me from creating it again from other computers.

Someone said he has done it using this

string connectionString = @"Data Source=*****\SQLEXPRESS2;Database=DATA;Integrated Security=SSPI";

to solve your connectionstring problem, you can add one Config file to your project and write the connection string within it.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="SqlCon" value="data source=SERVERNAME;initial catalog=DATABASENAME; user Id=USERID; password=PWD;persist security info=false;"/>
  </appSettings>
</configuration>

Add this code within your config file, then in your cs file you must add System.Configuration namespace. Now you can write your connection string like this.

SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["SqlCon"].ToString());

after installing your application you will get one app.config file in your installation directory with the above xml code, you need to change the appropriate servername, database,uid, pwd in SqlCon tag.... Hope this will solve your problem.....:)

What is the difference of naming a connection config file from mine?

If you hardcode the connectionstring withing your cs file then after installation problem arises to connect with your database......I personaly use this method very mcuh and i felt easy with it..... :)... after installation i just need to change the connectionstring accordin to client machine and thats it....

I see. Then I should add system.configuration namespace, right?

By the way, as indicated here you can use the following method to read the MDF file without attaching it to your installed SQL Server:

Data Source='.\SQLExpress'; Initial Catalog=; Integrated Security=true; AttachDBFileName='...\Db.mdf'

Where your path in Data Source matches the installation path (generally a default path) for SQLExpress and the path in AttachDBFileName matches the installed location of your mdf file at time of your program being run.

Of course you need to ensure that other details are a match to your particular DB file like the catalogue and such.

Hope that helps :)

I have successfully build the project.
I used all the suggestion given by you guys, from detaching the database and adding it into my project and changing the connection string to use the one in app.config. I also manage to make in a .msi for installing purposes, then I include the .mdf file for installation, i thought that when I choose primary it will also find the .mdf.

Thanks guys! Solved!

by the way. I tried installing the application from other computers with SQL Server and all is working fine. Even though the computer names are different, it's still working. How is this possible? I used datasource = .\\SQLEXPRESS, what does '.' means as computer name?

I just used '.' because the sql server of the computer where i worked at has a default server .\\SQLEXPRESS.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.