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());
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 myself tried a lot but i couldn't find out what is the problem of my below connection string. i have put both my webhost connection string and winform connection string help please?
<connectionStrings> <add name="ReGdbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
provider=System.Data.SqlClient;
provider connection string="data source=.\SQLEXPRESS;
initial catalog=ReGdb;
integrated security=True;
MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
and here is my webhostdatabase connectionstring
Data Source=SQL5000322.Smarterasp.net;
Initial Catalog=DB_9BDB7789_ReGdb;
User Id=DB_9BDB7789_ReGdb_admin;
Password=happy;
hey bro you can modify your connection string this way and it must work. just copy and replace it with your connection string
<connectionStrings>
<add name="ReGdbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
provider=System.Data.SqlClient;
provider connection tring=quot;
datasource=SQL5000322.Smarterasp.net;
initial catalog=DB_9BDB7789_ReGdb;
User Id=DB_9BDB7789_ReGdb_admin;
Password=happy;
MultipleActiveResultSets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
here you go bro it must work
Pay attention to nested quotes:
provider connection string="data source=...
I'm trying to write Connection string to Web.config like this:
<connectionStrings>
<add name="Dbconnection" connectionString="Server=localhost;
Database=OnlineShopping ; Integrated Security=True"/>
</connectionStrings >
and read from it like this:
string strcon =
ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
SqlConnection DbConnection = new SqlConnection(strcon);
when run the program I get an error because of the null reference. but when I use this code:
SqlConnection DbConnection = new SqlConnection();
DbConnection.ConnectionString =
"Server=localhost; Database=OnlineShopping ; Integrated Security=True";
I don't get any error and the program works correctly!
What is the problem?
Add reference to add System.Configuration:-
System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;
Also you can change the WebConfig file to include the provider name:-
<connectionStrings>
<add name="Dbconnection"
connectionString="Server=localhost; Database=OnlineShopping;
Integrated Security=True"; providerName="System.Data.SqlClient" />
</connectionStrings>
Web.config:
<connectionStrings>
<add name="ConnStringDb" connectionString="Data Source=localhost;
Initial Catalog=DatabaseName; Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
c# code:
using System.Configuration;
using System.Data
SqlConnection _connection = new SqlConnection(
ConfigurationManager.ConnectionStrings["ConnStringDb"].ToString());
try
{
if(_connection.State==ConnectionState.Closed)
_connection.Open();
}
catch { }
Try this
After open web.config file in application and add sample db connection in connectionStrings section like this
<connectionStrings>
<add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient"/>
</connectionStrings >
Are you sure that your configuration file (web.config) is at the right place and the connection string is really in the (generated) file? If you publish your file, the content of web.release.config might be copied.
The configuration and the access to the Connection string looks all right to me. I would always add a providername
<connectionStrings>
<add name="Dbconnection"
connectionString="Server=localhost; Database=OnlineShopping;
Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
try this
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();
Try to use WebConfigurationManager instead of ConfigurationManager
After opening the web.config file in application, add sample db connection in connectionStrings section like this:
<connectionStrings>
<add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System.Data.SqlClient" />
</connectionStrings>
Declaring connectionStrings in web.config file:
<add name="dbconnection" connectionString="Data Source=Soumalya;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System.Data.SqlClient" />
There is no need of username and password to access the database server.
Now, write the code to get the connection string from web.config file in our codebehind file. Add the following namespace in codebehind file.
using System.Configuration;
This namespace is used to get configuration section details from web.config file.
using System;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
//Get connection string from web.config file
string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
//create new sqlconnection and connection to database by using connection string from web.config file
SqlConnection con = new SqlConnection(strcon);
con.Open();
}
}
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.
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);