connection string error - c#

I have a connection string like this
<add name="rackleyConnectionString1" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\rackley.mdb;Persist Security Info=True;Jet OLEDB:Database Password=Password"
providerName="System.Data.OleDb" />
Now I copied that mdb file into my sql server. Now I changed connection string to this
<add name="rackleyConnectionString" connectionString="Data Source=ODO-SRV004;Initial Catalog=rackley;Integrated Security=True"
providerName="System.Data.SqlClient" />
when I run the program it shows error: "OLE DB provider was not specified in the connection string. An example would be Provider=SQLOLEDB"
Please help to solve this issue.

I hope you are opening sql connection as below
Dim cn As SqlConnection= New SqlConnection(sConn)
and not
Dim cn As OleDbConnection = New OleDbConnection(sConn)

I think You misplaced \ instead of /
<add name="rackleyConnectionString1" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/rackley.mdb;Persist Security Info=True;Jet OLEDB:Database Password=Password"
providerName="System.Data.OleDb" />
I always used /. please try this

<add name="rackleyConnectionString" connectionString="Provider=SQLOLED;Data Source=ODO-SRV004;Initial Catalog=rackley;Integrated Security=SSPI" />
Try this connection string hope this helps.

Related

Can't connect to local database when using connection string

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

c# connection string and concatenation app.config

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()
);

Webconfig file not formed correclty

On my live box I am getting an error that the connection string is malformerd but when i try it on my dev box it is fine.
<add name="aperturenetEntities" connectionString="metadata=res://*/ApertureModel.csdl|res://*/ApertureModel.ssdl|res://*/ApertureModel.msl;provider=System.Data.SqlClient;provider connection string="data source=APR-AZ-DB01;initial catalog=aperturenet;user id=aperturenet;password=dw13£sowwoq;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="aperturenetSql" connectionString="Data Source=APR-AZ-DB01;database=aperturenet;User ID=username;Password=password" providerName="System.Data.SqlClient" />
They are both .net 4.5 boxes with no difference in sql version of the driver but yet i get malformed elements when running the application any idea?.
<add name="aperturenetEntities" connectionString="metadata=res://*/ApertureModel.csdl|res://*/ApertureModel.ssdl|res://*/ApertureModel.msl;provider=System.Data.SqlClient;provider connection string="data source=APR-AZ-DB01;initial catalog=aperturenet;user id=aperturenet;password=dw13£sowwoq;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
try this,
i have changed Application Name to App in your connection string.

how to put a webhostdatabase connection string in a winform application?

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=...

How to change connection string in app.config

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);

Categories

Resources