In my app.config:
<add name ="connSpionshopString" connectionString="Data
Source=.\SQLEXPRESS;;AttachDbFileName=|DataDictionary|Spionshop.mdf;
Integrated Security=True;User Instance=True"/>
And in my form.cs:
SqlConnection conn2 = new SqlConnection();
conn2.ConnectionString = ConfigurationManager
.ConnectionStrings["connSpionshopString"]
.ConnectionString;
This always gets an error: "Invalid v alue for key 'attachdbfilename'."
I don't know what's wrong with this.. :(
You should be able to fix it by modifying it just a tad:
<add name ="connSpionshopString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|Spionshop.mdf;Integrated Security=True;User Instance=True"/>
You had DataDictionary instead of DataDirectory
The invalid value for key 'attachdbfilename' error can be thrown if the database server/host name is considered incorrect by the SqlConnection class. When using SQL Server Express you may only use an instance of the local host (., (local), or NETBIOS or DNS name of local machine).
that means either use like this
<add name ="connSpionshopString" connectionString="Data
Source=.;AttachDbFileName=|DataDictionary|Spionshop.mdf;Integrated Security=True;User
Instance=True"/>
or
<add name ="connSpionshopString" connectionString="Data
Source=machinename;AttachDbFileName=|DataDictionary|Spionshop.mdf;Integrated Security=True;User
Instance=True"/>
or
<add name ="connSpionshopString" connectionString="Data
Source=(local);AttachDbFileName=|DataDictionary|Spionshop.mdf;Integrated Security=True;User
Instance=True"/>
I suppose expecting the server name of the sql database to be a local address and not /sqlexpress in DataSource variable
I believe you have a typo:
connectionString="Data Source=.\SQLEXPRESS; **HERE!! ;**
AttachDbFileName=|DataDictionary|Spionshop.mdf;
Integrated Security=True;User Instance=True"
You put 2 semicolons instead of one.
Related
i'm unable to connect to SQL server when i use connection string:
this code works well:
SqlConnection db_connection = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
db_connection.Open();
but if put this on Web.config:
<connectionStrings>
<add name="conn" connectionString="Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" />
</connectionStrings>
Then on my page:
SqlConnection db_connection = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
db_connection.Open(); // fail - error 50
So it will only work if i don't use the configuration manager...
The issue was that the slashes between the server and the instance name were escaped for the code to interpret the string, but in the [app|web].config file, it understands there are going to be interesting characters in there, and handles them accordingly, and do not need the escaping that the code does.
Removing the double slash between the (localdb) and MSSQLLocalDB fixed the issue for me. Try the following in your connectionStrings config:
<connectionStrings>
<add name="conn" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" />
</connectionStrings>
Hello try to use this and
1. in reference add System.Configuration
2. write in cs Using System.Configuration
3. var connectionString = ConfigurationManager.ConnectionStrings["conn"].ToString();
put this into config file
I have the following connection string through which i connect to an Access database(.mdb) located in a sub-folder inside the root-folder of my application :
OleDbConnection con = new OledbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory() + "Data\rctts.mdb;Jet OLEDB:Database Password=mypassword;")
My question is, how do i put the connection string in the app.config file? Generally, i use :
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
The issue i am facing is that in my connection string, i am doing some concatenations and also using System.AppDomain.CurrentDomain.BaseDirectory() to set the Data Source...
How do i do the same concatenation in app.config ? Is it possible to do so ?
You could try the following:
connectionString="Data Source=|DataDirectory|\rctts.mdb;Initial Catalog=OmidPayamak;Integrated Security=True"
and then try to set the value of DataDirectory as below:
var currentDomain = AppDomain.CurrentDomain;
var basePath = currentDomain.BaseDirectory;
currentDomain.SetData("DataDirectory", basePath+"\Data");
at the corresponding startup file of your application.
Although configuration APIs offer no facilities to manipulate connection strings for you, you could place connection string "template" into configuration, and do the rest of manipulation in your code using string.Format:
Config:
<connectionStrings>
<add name="Test" connectionString="Data Source={0}Data\rctts.mdb;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
C# code:
string conStr = string.Format(
ConfigurationManager.ConnectionStrings["Test"].ConnectionString
, System.AppDomain.CurrentDomain.BaseDirectory()
);
Cant seem to get my connection string to work.
app.config file:
<add name="PalisadeWorld.Properties.Settings.PalisadeWorldDatabaseConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PalisadeWorldDatabase.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
Where I use the connection string:
SqlCeConnection Con = new SqlCeConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename="C:\\Users\\Win8User\\Documents\\Visual Studio 2010'Projects\\PalisadeWorld\\PalisadeWorld\\PalisadeWorldDatabase.mdf";Integrated Security=True;User Instance=True");
I've tried almost everything I could think of or find online.
I keep getting the error:
Unknown connection option in connection string: attachdbfilename.
Am I missing something?
Thank your for you time
Turns out I was not using the compact version of SQL (SqlCeConnect)
so I need to use SqlConnect instead
SqlConnection Con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PalisadeWorldDatabase.mdf;Integrated Security=True;User Instance=True");
Thanks everyone, Kindly
Template for connection string is
<add name="TrempimModel"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
database=YourDatabaseName;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
please remove '\' from the attachdbfilename attibute after datadirectoy so new connectionstring will be
<add name="PalisadeWorldDatabaseConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|PalisadeWorldDatabase.mdf; Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
and when getting connection string use.
SqlCeConnection Con = new SqlCeConnection(ConfigurationSettings.AppSettings["PalisadeWorldDatabaseConnectionString"].ToString());
I have a program that generates reports by using DataTables created by TableAdapters. Now my client has a new database, and he wants to be able to switch between the new one and the old one. I found that I can do it by changing the connection string in the app.config, but I don't know how to do it during run time. Can you suggest me a way?
Thanks
I don't know how to do it during run time
Don't. You can have multiple connection strings in the app.config and access each when needed.
Configuration:
<connectionStrings>
<add name="conn1" providerName="System.Data.SqlClient"
connectionString="..." />
<add name="conn2" providerName="System.Data.SqlClient"
connectionString="..." />
</connectionStrings>
In code:
var conn1 = ConfigurationManager.ConnectionStrings["conn1"];
var conn2 = ConfigurationManager.ConnectionStrings["conn2"];
You can define more than one connection strings like this:
<add name="Conn" connectionString="Data Source=PC\SQLEXPRESS;Initial Catalog=NHIB;Integrated Security=True" providerName="System.Data.SqlClient"/>-->
<add name="Conn1" connectionString="Data Source=WINSERVER;Initial Catalog=NHIB1;Integrated Security=True;" providerName="System.Data.SqlClient"/>
And after that you can use conn or conn1 based on your requirement..like:
SqlConnection con;
con = new SqlConnection(ConfigurationManager.AppSettings.Get("Conn")); Or
con = new SqlConnection(ConfigurationManager.AppSettings.Get("Conn1"));
You can switch between them as below:
string connectionString = HttpContext.Current.Request.IsLocal ?
ConfigurationManager.ConnectionStrings["Conn"].ConnectionString :
ConfigurationManager.ConnectionStrings["Conn1"].ConnectionString;
yourDataContext = new YourApplicationDataContext(connectionString);
I'm new to .net c#, hope this question is not sound silly. How can I connect to the database in web.config by using the username and password??
Example:
Following is the connection strings that I used to connect to the database. How could I write/set so that I can set the username (username = test) and password (password = abc123) for this connection strings and so that it will allows me to access to the database?
<connectionStrings>
<add name="aConnectionString" connectionString="Data Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name;Connect Timeout=1;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Try something like this.
<connectionStrings>
<add name="aConnectionString" connectionString="Data
Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name;User Id=myUsername;Password=myPassword"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Use this to pull your connection string from the web.config.
http://msdn.microsoft.com/en-us/library/ms178411.aspx
Then you will use the .net Sql Provider to create a connection.
using(SqlConnection con = new SqlConnection(connectionstring))
{
con.Open();
// Perform operations here
}
Are you trying from IIS?
Then you need to grant your App Pools account access to SQL Server.
CREATE LOGIN [IIS APPPOOL\ASP.NET v4.0] FROM WINDOWS WITH DEFAULT_DATABASE=[AAA-LLLL-SQL-00], DEFAULT_LANGUAGE=[us_english]
GO
Reference for ADO and ADO.NET connection strings http://www.sqlstrings.com/
<connectionStrings>
<add name="aConnectionString" connectionString="Server={ip};Database= {databasename}; User Id={username}; Password={password}"/>
</connectionStrings>
This should work replace {...} with your values.
<connectionStrings>
<add name="aConnectionString" connectionString="Data Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name; User Id=your username; Password=pwd;" providerName="System.Data.SqlClient"/>
</connectionStrings>