How can I use variable in connection string?

onurcano

I'm having trouble with variables for connection strings. In my app.config database path seems fine but on form, I'm getting error for that connection string. When I try to add:

_connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; " +
    "AttachDbFilename = \"|DataDirectory|\\gazi_db.mdf\"; " +
    "Integrated Security = True; Connect Timeout = 30";

the database won't work correctly; it can't save data on exit. However this Works fine:

connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; " +
        "AttachDbFilename = \"C:\\Users\\Can\\Desktop\\c_sharp_gazi_installer" +
        "\\Gazi Installer\\gazi_installer\\gazi_db.mdf\"; " +
        "Integrated Security = True; Connect Timeout = 30";

How can I fix this?

EDIT: I hoped this would work

string DataDirectory = "";
string folder = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
AppDomain.CurrentDomain.SetData("DataDirectory", folder);

_connectionString = _connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; " +
        "AttachDbFilename = \"" + DataDirectory + "\\gazi_db.mdf\"; " +
        "Integrated Security = True; Connect Timeout = 30";

But it's still giving me sqlclient sqlexception error.

EDIT2: This error indicates connection open. I don't understand why this doesn't work either:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder["Data Source"] = "(LocalDB)\\MSSQLLocalDB";
builder["AttachDbFilename"] = @"C:\Users\Can\\Desktop\c_sharp_gazi_installer\Gazi Installer\gazi_installer\gazi_db.mdf";
builder["Integrated Security"] = true;
builder["Connect Timeout"] = 30;

Please someone help. I'm going to go mad.

WereGoingOcean

.NET has a connection string builder class. Using this you can set each value i.e. Data Source, Integrated Security separately to variables and then use the .ConnectionString property to get the connection string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i use one connection string in multi Domain Services?

How can I concatenate a string with a variable and use it as another variable?

PHP - How can I use the content of a string variable as a variable in empty()?

How can I use a string variable in .bat for-loop?

How can I use a variable in String.fromCharCode()?

How can I use the contents of a cell as a variable which must be a string

How can I use a string variable as a path url in C++

How can i use dynamically generated string inside `list() = $variable`?

How can i use string variable as an object name in JAVA?

How can I use connection pool in aiomysql

How i can use Koala on FTP connection?

How can I include a variable for my connection string in the default settings value in the settings constructor and get it later?

Can I use a variable to determine a string in a list?

How can I define SQL server connection string inside a class and use everywhere in project?

How I can use two or more than two connection string in Blazor in two or more than two servers?

How can I use parameter to replace a mongodb connection string in a lambda function in AWS SAM?

How can I use token instead of azure connection string for blob storage access

How I can use a django variable in if statement of django template into a javascript string variable

Console App using entity, can i use the same connection string?

How can I use a for loop to modify a variable inside a string while I download a list of images using BS?

How do I use a variable as a string in JavaScript?

How can I convert my variable into a string?

how can I convert two string to variable

How can I call variable by string name

How can I call a variable from a string?

How can I remove a variable that contains a string?

How can i delete string in variable powershell?

How can I use loop variable in array?

How can i use a variable in sbt