Click here to Skip to main content
15,896,501 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i am using visual studio 2013 and sql server 2012 . i create a wpf form project at c# which include add update delete features and database is made in sql server 2012 my project is working 100% . so after i get complete my project with database i try to make a setup and deployment of my project and its all done perfectly and my my setup of project run with all features which i put on it but when i run my application on other fresh pc where no visual studio and sql server 2012 installed its run but just showing windows form add update delete not working and its give me error that database is not connected ....then i search so many places then i found how to add database mdf file during project and deployment of project i just deatached my database from sql server then add file into setupdeploy of my project and also change the string of app.config and C# code file but its still not working and show data base not conected error can plz any buddy give me some detailed guide line or referenece that how can i make it possible 


below is my app.config which  work fine when i dont deatach database from sql server dont add mdf file to setup 


XML
<connectionStrings>
   <add name="WpfApplication2.Properties.Settings.testingConnectionString"
       connectionString="Data Source=EMIII-PC;Initial Catalog=testing;Integrated Security=True"
       providerName=".NET Framework Data Provider for SQL Server" />
 </connectionStrings>


this is my app.config after changes


XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="WpfApplicaion2"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\testing.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>



this is my C# Code connection string  after changes



C#
 public void BindMyData()
        {
            string dbconnection = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\testing.mdf;Integrated Security=True;User Instance=True");
            SqlConnection conn = new SqlConnection(dbconnection);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT * from Custom", conn);
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                myDataGrid.ItemsSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
             
            
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            BindMyData();
        }

    }
}
Posted
Updated 12-Jul-14 14:16pm
v3
Comments
[no name] 12-Jul-14 20:22pm    
Your problem is, is that you did not install SQL Server on your target computer.
Member 10925486 13-Jul-14 6:08am    
dear i dont want to creates setup who cannot be depend on sql server at every target computer i want to attach database with my application
[no name] 13-Jul-14 7:24am    
Well Sweetie, that is the price you pay for using SQL Server. You MUST have it installed somewhere where your application can access it. Either that or use a different database engine, one that does not require a server.

1 solution

Hi Member,

From your question and your comments I deduce you have no clue what your are doing. ;-) Sounds harsh?

* You swapped SQLSever for SQLExpress (so to target full SQL-Server was a mistake in the first place?), So you didn't think about server features, installation problems. etc.
* Developed you project to "100% working" (whatever that means) and then first considered installation on target platform? Big mistake - start with that on your next project.
* Your question "add mdf file, app config"??? So you don't see that something has to "understand" the mdf file and use the config?
* And no, creating an installer to include SQL-Server Installation is not a recommendet procedure. Did you ever install (a non trivial) SQL environment? This is nothing you want your users to do.

So what to do?

From your comments I understand your problem started before the Project begun, when you decided to use SQL-Server while not appropriate. Because now you have the requirement "run without SQL-Server Installation", and you can't.

Solution could be to use a database (file, whatever) embedded inside your application.
If you want to embed the database into you project there are serveral good solutions, but all with limitations - you have to find out for yourself what fit's your needs. And - Yes - You may have to change a lot of your "100% working" project now...
You could use:
Firebird - an embedded database I used in the past, worked well.
SQLite - I would recommend that in general because it's not platform dependend.
SQLCompact - maybe easiest for you (may need not too much code changes, depends what you have now).

So good luck with your project, I know how "learning the hard way" can feel... :-(

Kind regards Johannes
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900