I am looking for a suggestion, how can I connect database without writing the connection string in the native application.
I am using this but looking for another so that, when I rename database, the software will connect automatically.
static string conStr = #"server=myServerName;Integrated Security=true;Connection Timeout=5;Database=myDatabaseName;";
You can put the connection string in the configuration file.
<connectionStrings>
<add name="MyDatabase" connectionString="server=myServerName;Integrated Security=true;Connection Timeout=5;Database=myDatabaseName;" providerName="System.Data.SqlClient" />
</connectionStrings>
Then to retrieve the string you want you use:
ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString
Asking for it to connect automatically even if you rename the database isn't possible. Think about it, a single server can host hundreds of databases. How would it know which one to connect to?
Related
I am writing Integration tests for a WPF application and I'm using a local database that is placed inside the solution for integration testing and this database has data inside it.
When I read data from this database after connecting it to SQL Server, it returns valid data, but once I go back to the application, add its connection string and try to get data, the code returns an empty set. I'm on a tight deadline and I don't know why this is happening.
Any help will be greatly appreciated.
Simple code is:
var xsystem = context.Species.ToList();
My connection string is:
<add name="MaxDatabase"
connectionString="Data Source=(LocalDb)\ProjectsV13;Initial Catalog=Catalog=MaxLocalEmbeded;Integrated Security=SSPI"
providerName="System.Data.SqlClient" />
Just replace your connection string with this below code.
Also remove "catalog=catalog" from your connection.
<add name="MaxDatabase" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=MaxLocalEmbeded;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MaxLocalEmbeded.mdf" providerName="System.Data.SqlClient" />
I have two connection strings saved in Properties that I need interchangeability.
One is for a database that is used to test my application and the other is for the actuall database that the application will be used on.
My problem is that I have to use the connection string many times in different classes and as it stands now I am manually changing to the connection string by copy and paste.
Is it possible to do something similar as Rename Refactoring (C#) ,but not actually rename, but replace the connection string with the second one?
Ie, my first connection string is this,
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString_One))
Replace every connection string value into the second connection string,
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString_Two))
One is for a database that is used to test my application and the
other is for the actuall database that the application will be used
on.
If I understand correctly, you are trying to replace the connection string in the production build. Here's the step how to do it.
Save the connection string in config file
Sample code: (web.config OR app.config)
<connectionStrings>
<add name="ConnectionString" connectionString="{testing}" providerName="System.Data.SqlClient"/>
</connectionStrings>
Create a build transformation (by default, it would have debug build and release build
Replace the testing connection string with production connection string in release build
Sample code: (web.release.config OR app.release.config)
<connectionStrings>
<add xdt:Transform="Replace" xdt:Locator="Match(name)" name="ConnectionString" connectionString="{production}" providerName="System.Data.SqlClient"/>
</connectionStrings>
Hope it helps.
Read more at: https://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx
I have been trying to learn how to use sql express with C# and I am having difficulty connecting to a database that already has data in it. When I connect to the sql server my program adds a new database instead of reading the one that is currently in the sql server.
Here is my connection string which I think is where the issue is.
<add name="GolfLeague1" providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=aspnet-WebUI-20140205175325;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WebUI-20140205175325.mdf" />
This gives me a new database called GolfLeague1(WebUI). Which I can read and write to, but it is not what I would like to do.
I have tried the string with and without a "Database=".
What part of the big picture am I missing here?
You have specified 'AttachDbFileName' property when you already have specified the database name. This part is not required here.
You can use the below mentioned connection string.
<add name="GolfLeague1" providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\v11.0;Initial Catalog=aspnet-WebUI-20140205175325;Integrated Security=SSPI;" />
I have built a window form application and I use ADO.NET entity Data model to make connection with my local database and it work good with my own PC (the database and application in one PC).
Now I need the application to be used by many users 3 or 4. I have no idea how to install my database on the server and whether ADO.NET entity Data model will work again. I use SQL Server 2008 and C# window Form. Please I am waiting to hear what your suggestion will be.
all you need to do is just change the Data Source value in your connection string.
Follow the below Steps:
Step 1: Install and Maintain Your Database in your server machine.
Step 2 : Specify your databse Server HostName/IP Address in your Connection String as below:
String connectionString="Data Source=ServerName;Intial Catalog=DatabaseName;UID=userID;Password=password;Integrated Security=True;"
Add app.config file to your project and specify the connection string in there. Then access the config file using c#
<connectionStrings>
<add name="MyDBConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
</connectionStrings>
in c#
System.Configuration.ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
Source Accessing database connection string using app.config in C# winform
You have host your database on server
Get server ip,username,password for the database
change your connection string
replace server name ,username,password
Data Source=serverip;Initial Catalog=dbname;Persist Security Info=True;User ID=username;Password=password
then this will be goto server database
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");