I am creating a Windows application with a local database.
Now I need to define a connection string for that local database in C#.
What is the connection string for local .mdf files in Winforms C#?
Do the following steps:
In your Server Explorer window right click on the database that
you've created and select Properties.
in the Properties window copy the value of the connection string
property and paste it to your application.
It should be something like this:
Data Source=(localdb)\v11.0;Initial Catalog=yourDB;Integrated Security=True
Just you need to add one extra \ to it to work in c#:
string address = "Data Source=(localdb)\\v11.0;Initial Catalog=yourDB;Integrated Security=True";
You could also have a look at
The Connection Strings Reference.
Here you go
<add name="YourDataBase" connectionstring="Data Source=.\SQLEXPRESS; AttachDbFilename=Path\YourDataFile.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True" />
Also you can visit connectionstring.Com as well for more.
Use SqlServeCe as a namespace and then give the following codings
using System.Data.SqlServerCe;
SqlCeConnection conn = new SqlCeConnection(#"Data Source=D:\PROJECT\data\db.mdf;");
Related
Specifically, in VS 2008, I want to connect to a data source that you can have by right-clicking on the automatically-generated App_Data folder (an .mdf "database"). Seems easy, and it is once you know how.
A great resource I always keep around is connectionstrings.com.
It's really handy for finding these connection strings when you can't find an example.
Particularly this page applied to your problem
Attach a database file on connect to a local SQL Server Express instance
Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
So here's the answer from MSDN:
Choos[e] "Add New Data Source" from the
Data menu.[And follow the connection wizard]
Very easy, except that I have no Data menu. If you don't have a Data menu, do the following:
Click on Tools >> Connect to Database...
Select "Microsoft SQL Server Database File", take the default Data provider, and click OK
On the next screen, browse to your Database file, which will be in your VS Solution folder structure somewhere.
Test the connection. It'll be good. If you want to add the string to the web.config, click the Advanced button, and copy the Data Source line (at the bottom of the dialog box), and paste it into a connection string in the appropriate place in the web.config file. You will have to add the "AttachDbFilename" attribute and value. Example:
The raw text from the Advanced panel:
Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;User Instance=True
The actual entry in the web.config:
<add name="SomeDataBase" connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=C:\Development\blahBlah\App_Data\SomeDataFile.mdf;
Integrated Security=True; Connect Timeout=30; User Instance=True" />
Just one more -- i've always kept a udl file on my desktop to easily create and test connection strings. If you've never done it before - create a new text file and name it to connection.udl (the ext is the only important part). Open the file, start on the Provider tab and work your way through. Once you're happy with the connection rename the file giving it a .txt extension. Open the file and copy the string - it's relatively easy and lets you test the connection before using it.
<add name="Your Database" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Expanse.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
In your Login.aspx.cs (the code behind file for your login page in the submit button click event) add
string constr = #"Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\myData.mdf; Integrated Security=True; Connect Timeout=30;";
using (SqlConnection conn = new SqlConnection(constr))
string constr = ConfigurationManager.ConnectionStrings["myData"].ToString();
using (SqlConnection conn = new SqlConnection(constr))
{
sqlQuery=" Your Query here"
SqlCommand com = new SqlCommand(sqlQuery, conn);
com.Connection.Open();
string strOutput = (string)com.ExecuteScalar();
}
I am creating an Excel file in C# with SQL Server. I've tried several connection strings, but it keeps saying that it does not recognise my user ID (the ID is valid and can connect directly to SQL with this)
Can you help me, or tell me where is the error that comment?
Below I show the connections I've tried
connectionstring = "Integrated Security = SSPI;Initial Catalog='DATABASENAME'; Data Source=XXX.XXX.X.XX; ";
connectionstring = "SERVER=XXX.XXX.X.XX;DATABASE=DATABASENAME;UID=sa;PASSWORD=pass;";
connectionstring = "Persist Security Info=False;User ID=sa;Password=pass;Initial Catalog=DATABASENAME;Server=XXX.XXX.X.XX";
connectionstring = "Data Source=192.168.0.18;Initial Catalog=DATABASENAME;Persist Security Info=True;User ID=sa;Password=pass";
connectionstring = "Persist Security Info=False;Integrated Security=true;Initial Catalog=DATABASENAME;server=(XXX.XXX.X.XX)";
connectionstring = "Data Source=XXX.XXX.X.XX,1433;Network Library=DBMSSOCN; Initial Catalog=DATABASENAME;User ID=sa;Password=pass";
cnn = new SqlConnection(connectionstring);
Here is the sample connection string format
Data Source=[server];Initial Catalog=[databasename];User ID=[sa];Password=[password]
example - named instance
Data Source=localhost;Initial Catalog=master;User ID=sa;Password=sa123
in case of the default instance
Data Source=localhost\sqlexpress;Initial Catalog=master;User ID=sa;Password=sa123
if you are trying to access express version from another machine please refer and replace localhost with ip or machine name.
Navigate to C:\Windows\System32\odbcad32.exe. Go to the Drivers tab and then check to see which ODBC drivers you have installed on your system.
Then go to this website and follow the links at the top for whichever ODBC driver you have installed on your system. For C# you may or may not need the Driver= buts its usually safe.
This is the connection string which usually works for me in C# asp.net, however:
Server=[SERVER];Database=[DATABASE];Uid=[USER];Pwd=[PASSWORD]
(note how it is different than yours: Pwd= as opposed to Password=)
"HP\\ADMIN;Initial Catalog = PAY_PROCESSING_GO; User ID=sa; Password=admin#123"
this is my connection string but connection consist Keywords. How to solve this problem
I have developed a web application in asp.net , and have published that website in iis 7.
I have given full access to the published folder and it works perfectly.When i browse my published application the login page has been loaded initially. If i fill the details and tried to login there connection fails.SQL connection has noot been opened
webConfig i have
<add name="SQLSERVER2008ConString" connectionString="Data Source=(local);Initial Catalog=REPOSITY;Integrated Security=True" providerName="System.Data.SqlClient"/>
and for opening connection
SqlConnection con = new SqlConnection(conString);
con.Open();
here the connection is not getting opened.But it works perfectly in my development environment.But not working after i published the code.
this how i connect my sql in local
Note:
Development environment is my system.
Used SQL Server 2008 and that is also in my system.
Published the website from my system only.
thanks
use this code
String conString= System.Configuration.ConfigurationManager.ConnectionStrings["SQLSERVER2008ConString"].ConnectionString;
SqlConnection con = new SqlConnection(conString);
con.Open();
Are you sure your SQL Local Login Name is (local) ? Check the name once. But If Yes, then try this: <add name="SQLSERVER2008ConString" connectionString="Data Source=(local);Initial Catalog=REPOSITY;Integrated Security=True" User Id = [ENTER YOUR WINDOWS LOGIN USERNAME];Password = [ENTER YOUR WINDOWS LOGIN PASSWORD]"/>
I'm working on a small project to track information. This C# application will take information uploaded via CSV/Excel and store/sort it.
My current connection string is an absolute path (off thumb drive). I'm worried that when I publish it, the database connection won't work on a random users computer.
<add name="PPP_Project.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Other PPP Projects\PPP_Project_Test\PPP_Project\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
How do I have this set so it will work when the application is published?
you can use this:
Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|Customers.mdf;Integrated Security=True;User Instance=True
as described here.
For a winform application, the default |DataDirectory| is the same where the application is installed. Of course you can use aa a part of a more nested path.
If you want to specifyng something else you can use the AppDomain.SetData method.
The easiest way to manipulate connection strings is with the ConnectionStringBuilder classes:
OleStringBuilder = new OleDbConnectionStringBuilder(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
OleStringBuilder.DataSource = MapPath(#"~\App_Datav\MyExcelWorksheet.xls");
Specifically, in VS 2008, I want to connect to a data source that you can have by right-clicking on the automatically-generated App_Data folder (an .mdf "database"). Seems easy, and it is once you know how.
A great resource I always keep around is connectionstrings.com.
It's really handy for finding these connection strings when you can't find an example.
Particularly this page applied to your problem
Attach a database file on connect to a local SQL Server Express instance
Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
So here's the answer from MSDN:
Choos[e] "Add New Data Source" from the
Data menu.[And follow the connection wizard]
Very easy, except that I have no Data menu. If you don't have a Data menu, do the following:
Click on Tools >> Connect to Database...
Select "Microsoft SQL Server Database File", take the default Data provider, and click OK
On the next screen, browse to your Database file, which will be in your VS Solution folder structure somewhere.
Test the connection. It'll be good. If you want to add the string to the web.config, click the Advanced button, and copy the Data Source line (at the bottom of the dialog box), and paste it into a connection string in the appropriate place in the web.config file. You will have to add the "AttachDbFilename" attribute and value. Example:
The raw text from the Advanced panel:
Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;User Instance=True
The actual entry in the web.config:
<add name="SomeDataBase" connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=C:\Development\blahBlah\App_Data\SomeDataFile.mdf;
Integrated Security=True; Connect Timeout=30; User Instance=True" />
Just one more -- i've always kept a udl file on my desktop to easily create and test connection strings. If you've never done it before - create a new text file and name it to connection.udl (the ext is the only important part). Open the file, start on the Provider tab and work your way through. Once you're happy with the connection rename the file giving it a .txt extension. Open the file and copy the string - it's relatively easy and lets you test the connection before using it.
<add name="Your Database" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Expanse.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
In your Login.aspx.cs (the code behind file for your login page in the submit button click event) add
string constr = #"Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\myData.mdf; Integrated Security=True; Connect Timeout=30;";
using (SqlConnection conn = new SqlConnection(constr))
string constr = ConfigurationManager.ConnectionStrings["myData"].ToString();
using (SqlConnection conn = new SqlConnection(constr))
{
sqlQuery=" Your Query here"
SqlCommand com = new SqlCommand(sqlQuery, conn);
com.Connection.Open();
string strOutput = (string)com.ExecuteScalar();
}