System.Data.SqlClient.SqlException with VS 2017 - c#

I've got a problem with a project with Visual Studio 2017.
I want to save data in a local SQL Server database but when I run the script this error always happens at connection.open();:
System.Data.SqlClient.SqlException: "An attempt to attach an auto-named database for file C: \Users\mrman\onedrive\dokumente\visual studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\DB_Nährwertrechner.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
Here's the code for the connection:
SqlConnection connection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\mrman\onedrive\dokumente\visual studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\DB_Nährwertrechner.mdf;Integrated Security=True;Connect Timeout=30");
connection.Open();
Do you guys know why that error comes and how to solve it?

Related

Unable connect to SQL Server from Winforms application after deploy

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.

error in Connection String in C#

I am new to C#. I want to use my Microsoft SQL Server database file test.mdf in my output software in C#. In the past, I had just copied the connection string in Visual Studio like this :
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Home\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
as you see the database file path is : C:\Users\Home\Documents\test.mdf;
When I create setup for my sofware in Visual Studio 2008, and install the software on another PC, it errors :
An attempt to atach an auto-named database for file C:\User\Home\Document\test.mdf failed ...
So I want to address the file with the installation folder path whith this :
string dir = Application.StartupPath + "\\" + "test.mdf";
but when I want to run program in Visual Studio 2008 it erros
string dir = Application.StartupPath + "\\" + "test.mdf";
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=" + dir + ";Integrated Security=True;Connect Timeout=30;User Instance=True");
Error 1 A field initializer cannot reference the non-static field,
method, or property
'phonebook.Form1.dir' C:\Users\Home\Documents\Visual Studio
2008\Projects\phonebook\phonebook\Form1.cs 25 95 phonebook
UPDATE
When I use
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename="+ Application.StartupPath +" \\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
it errors :
One or more files do not match the primary file of the database. If
you are attempting to attach a database, retry the operation with the
correct files. If this is an existing database, the file may be
corrupted and should be restored from a backup. Cannot open user
default database. Login failed. Login failed for user 'Home-PC\Home'.
While I have copied right test.mdf file there
As the error message says, you can't use the value of one instance field when initializing another. You probably don't want dir as a field anyway. Just move all of this into the body of the constructor... or ideally, only create your SqlConnection when you need it anyway. Don't use a single instance throughout your application, but go through a "create, use, dispose" cycle every time you need database access. (Ideally, don't do this in your GUI code, either...)

Access DB connected with local directory

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"

this is error ORA-12154: TNS:could not resolve the connect identifier specified?

I've this code :
OracleConnection con = new OracleConnection("data source=localhost;user id=fastecit;password=fastecit");
con.Open(); string sql="Select userId from tblusers";
OracleCommand cmd = new OracleCommand(sql, con);
OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{ messageBox.Show(dr[0].Tostring()); }
Same code in both projects,
in project1 "WinForm" Is Working correctly
in project2 "Excel 2007 addins" Following error appears:
ORA-12154: TNS:could not resolve the connect identifier specified
I'm using C#.net 2010 ,office 2007 , windows8, oracle 10g.
When preparing a manual connection to the database, as shown in the picture
Visual Studio, open View menu + Server Explorer.
Right mouse click on Data Connection + Add Connection + Select Oracle Database
server Name : localhost or name of my machine, set username & password and click on Test Connection, test is no succeeds.
ORA-12154: TNS:could not resolve the connect identifier specified?
In case the TNS is not defined you can also try this one:
If you are using C#.net 2010 or other version of VS and oracle 10g express edition or lower version, and you make a connection string like this:
static string constr = #"Data Source=(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=yourhostname )(PORT=1521)))
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));
User Id=system ;Password=yourpasswrd";
After that you get error message ORA-12154: TNS:could not resolve the connect identifier specified then first you have to do restart your system and run your project.
And if Your windows is 64 bit then you need to install oracle 11g 32 bit and if you installed 11g 64 bit then you need to Install Oracle 11g Oracle Data Access Components (ODAC) with Oracle Developer Tools for Visual Studio version 11.2.0.1.2 or later from OTN and check it in Oracle Universal Installer
Please be sure that the following are checked:
Oracle Data Provider for .NET 2.0
Oracle Providers for ASP.NET
Oracle Developer Tools for Visual Studio
Oracle Instant Client
And then restart your Visual Studio and then run your project ....
NOTE:- SYSTEM RESTART IS necessary TO SOLVE THIS TYPES OF ERROR.......
The database must have a name (example DB1), try this one:
OracleConnection con = new OracleConnection("data source=DB1;user id=fastecit;password=fastecit");
In case the TNS is not defined you can also try this one:
OracleConnection con = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=DB1)));
User Id=fastecit;Password=fastecit");
run the below command in command prompt
tnsping Datasource
This should give a response like below
C:>tnsping *******
TNS Ping Utility for *** Windows: Version *** - Production on *****
Copyright (c) 1997, 2014, Oracle. All rights reserved.
Used parameter files:
c:\oracle*****
Used **** to resolve the alias
Attempting to contact (description=(address_list=(address=(protocol=tcp)(host=)(port=)))(connect_data=(server=)(service_name=)(failover_mode=(type=)(method=)(retries=)(delay=))))**
OK (**** msec)
Add the text 'Datasource=' in beginning and credentials at the end.
the final string should be
Data Source=(description=(address_list=(address=(protocol=tcp)(host=)(port=)))(connect_data=(server=)(service_name=)(failover_mode=(type=)(method=)(retries=)(delay=))));User Id=;Password=;**
Use this as the connection string to connect to oracle db.

service based database run in another PC

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.

Categories