My application is working in debug mode or from Visual Studio. But after deployment / publish, we are not able to connect to the database from setting file connection string.
BILLING_SYSTEM.Settings setting = new BILLING_SYSTEM.Settings();
We are using
string connectionString = setting.ConnectionString_Web.ToString()
SqlConnection conn = new SqlConnection(connectionString);
Connection String is:
Data Source=.;Initial Catalog=<databaseName>;Integrated Security=True
Its working on local. But after deploy on another machine not able to connect database.
Is there any method in set wizard which resolve my problem?
Please suggest. Thanks in advance.
Related
Here is my connection string:
SqlConnection myConnection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\m_bou\Desktop\PCOTools\Numbers.mdf;Integrated Security=True;Connect Timeout=10");
I have tried using the |DataDirectory|\Numbers.mdf, and also many other options people have tried but no luck! I just want it to load the mdf file that is in the executable directory when it loads... in my case its the debug folder, but when I deploy on a different machine it needs to be wherever the exe resides. Can anyone tell me what i'm doing wrong? This is a Winform applicagtion...
I'm trying to create a working installer or setup file for an application I created using C# in VS 2010.
I used InstallShield and also the built-in one and they do created the setup files and I installed the app with no problem.
But when I ran the program, this thing pops up:
My database connections:
On the forms:
SqlConnection cn = new SqlConnection(#"SERVER=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;DATABASE=Database;Integrated Security=True;User Instance=True");
App.Config:
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
What could be wrong? Should my connection string on the form the same as the one in the app.config file?
Visual Studio 2010 Ultimate
SQL Server 2008 Express
It looks like you ran the installer on your development box.
If you use AttachDbFilename in the connection string and you omit the option Database from the connection string the sqlclient will try to create a database with the filename as the name of the database. This fails if the database already exists.
You need to make sure that your SqlConnection uses the connection string from the app.config:
SqlConnection cn = new SqlConnection(
ConfigurationManager.
ConnectionStrings["MyDatabaseNameInConfig"].
ConnectionString);
In your app.config you'll need:
<connectionStrings>
<add name="MyDatabaseNameInConfig"
connectionString="Database=PRODUCTION;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf; ...."/>
</connectionStrings>
A way to fix this is to add a Database parameter to your connectionstrings for release builds:
connectionString="Database=PRODUCTION;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf; ...."
You can automate that in Visual Studio with XML Document Transform
Another option is to change your setup to make these changes on install but I'm not sure how easy that is.
A final solution might be to have your application accept a special startup argument to update the connectionstrings. The installer can start your application with that special argument at the end of the setup.
Basically I want the connection of my Access DB always available, so if i move the folder project to another computer it has to keep working, without changing the folder path.
I'm working with Windows Forms in Visual Studio 2012.
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data source= Z:\Tempesta\Area Progetto\Area_Progetto_27_02_2014\Area_Progetto_DATA_MAGAZINE\Data_Magazine\Data_Magazine\DB\DataMG.mdb";
That's the code I have right now for the connection to the DB.
Try this
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=|DataDirectory|DataMG.mdb"
I have been working on a project related to database (.mdf). I have created some windows forms in visual studio using C#. Basically these forms work together to store, update and delete data from the Service Based Database i created.when I run this project in another PC there is problem in connection to data base,I change the connection string as in another PC but the smae problem.
SqlConnectionStringBuilder s = new SqlConnectionStringBuilder("Data Source=.\\SQLEXPRESS;AttachDbFilename=H:\\PDF_to_TEXT_PROJECT_MBRM\\PDF-to-Text Convertor\\PDF-to-Text Convertor\\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
can you tell me how solve this problem?
this error:
An attempt to attach an auto-named database for file H:\PDF_to_TEXT_PROJECT_MBRM\PDF-to-Text Convertor\PDF-to-Text Convertor\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
How can I connect Visual Studio 2012 to SQL Server 2012 express on localhost. My server name :
HACEGAN\SQLEXPRESS
what must I write to my connection string i.e
SqlConnection con = new SqlConnection("Data Source=localhost\\HACEGAN.SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123");
Just write your server name to your Data Source part ?
Data Source=HACEGAN\SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123
Change your
localhost\\HACEGAN.SQLEXPRESS
to
HACEGAN\SQLEXPRESS
Check out: Visual Studio 2012 and MS Sql Server 2012 - connect with Server Explorer
You can find Server Explorer in Visual Studio 2012 -> View -> Server Explorer
You shouldn't be specifying both a servername & localhost. One or the other.
SqlConnection con = new SqlConnection(#"Data Source=HACEGAN\SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123");
Please note the # symbol to prevent escaping issues.
If your sql server is also a default instance, you can use . notation:
Server=.;Database=Register;User Id=sa;Password=123;
If non-default, use .\SQLExpress.
i think you write it directly,
Data Source=HACEGAN\\SQLEXPRESS;Initial Catalog=Register;User ID=xxx;Password=xxx
All other answers in this post are correct, but in your case you should write:
SqlConnection con = new SqlConnection("Data Source=HACEGAN\\SQLEXPRESS;Initial Catalog=Register;User ID=sa;Password=123");