0
I need a flexible connection string so that it automatically searches the program folder for file . mdf.
My following code is like this:
public Form1()
{
string relative = @"..\..\Database1.mdf";
string absolute = System.IO.Path.GetFullPath(relative);
AppDomain.CurrentDomain.SetData("DataDirectory", absolute);
InitializeComponent();
MessageBox.Show(absolute);
SqlConnection conex = new SqlConnection(absolute);
}
The variable Absolute on MessageBox
works perfectly, gives the exact location of my file .mdf.
But when placing in the parameter sqlConnection
, generates a error:
The boot chain format does not conform to the specification initiated in index 0.
In the app.config file of the program folder I also edited for something like this:
connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Initial Catalog=Cos;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
So the only problem is that I can’t use the variable Absolute as a parameter in the method SqlConnection
.
related to the theme: http://answall.com/questions/156488/comorprepare-programa-parausage
– novic