I have created a desktop app it is working fine in my PC with the connection string as follows:
SqlConnection conn = new SqlConnection(#"Data Source=DESKTOP-IAS4D;Initial Catalog= MConsole;Integrated Security=True");
But it is not working on another PC. So I changed the connection string so that the application can be used in any PC:
SqlConnection conn = new SqlConnection(#"Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\MConsole.mdf;Trusted_Connection=Yes;Integrated Security=True;");
But I get the following error:
System.Data.SqlClient.SqlException: 'An attempt to attach an auto-named database for file C:\Users\Hp\source\repos\ MConsole\bin\Debug\Databases\MConsole.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I want my application to work in different machines with my database. Please resolve my issue. Any kind of help will be appreciated.
Thanks in advance.
Related
I'm working on a project in C#/ASP.NET and I've come across an error which I can't solve. I have a database in which I created some tables, and whenever the application tries to open the database, I get the following error:
"Unable to open the physical file "path\App_Data\Cars.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)". An attempt to attach an auto-named database for file path\App_Data\Cars.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
I have SQL Express 2014 installed,named the instance 'SQLEXPRESS', wrote this instance ID in the Visual -> Options -> Tools -> Data Connections. In the web.config file, I have :
< connectionStrings>
< add name="ConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='path\App_Data\Cars.mdf';Integrated Security=True;User Instance=True"/>
< add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='path\App_Data\AspnetDB.mdf';Integrated Security=True;User Instance=True"/>
< /connectionStrings>
And the way I am trying to open the database is:
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Cars.mdf;Integrated Security=True");
con.Open();
Can you please tell me what to do? Thanks!
UPDATE: Managed to solve it, the argument in the SqlConnection should have been the same as the ConnectionString in web.config. Now there is another one: When I try to open the default database that Visual Studio provides(with Users,Roles,ecc), I receive the following error: The database „path\App_Data\AspnetDB.mdf” cannot be opened because it is version 852. This server supports version 782 and earlier. A downrade path is not supported.
Could not open new database „path\App_Data\AspnetDB.mdf”. CREATE DATABASE is aborted.
An attempt to attach an auto-named database for file'path\App_Data\AspnetDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share." Any ideas?
Taken from OP's edit to the question:
Managed to solve it, the argument in the SqlConnection should have been the same as the ConnectionString in web.config.
I'm working on C# .net application that needs to connect to a simple database. This database is btw in the same visual studio project and is called Playerdatabase.mdf.
In the application I want to connect to the database by using the following code:
string connectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\Users\\User1\\Documents\\Visual Studio 2013\\Projects\\ProreactorTest\\ProreactorTest\\Playerdatabase.mdf;Integrated Security=True;Connect Timeout=30";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
But when I try to run the application and when it calls the open() method I got the exception:
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL
Inner exception: can not find network path.
I looked at the path (by browsing to it) and it looks fine. I also read that disabling the firewall works, tried that but got the same exception.
Does anybody have a idea or suggestion? Any feedback is welcome!
On a c# application, I'm trying to get a connexion to a distant Oracle Server.
This is my connexion chain:
OracleConnection conn = new OracleConnection("Data Source=xx.xx.xx.xx:xxxx;User Id=xxxxx;Password=xxxxxx; Integrated Security=True")
I have never performed an OracleConnection and I'm not at ease with this..
Firstly, Visual studio told me I had to install install an Oracle Client. I have now the Oracle 11g Client Release 2.
Now when I try to connect to the OracleDB, I have the error message: ORA-12504:Listener was not given the SERVICE_NAME in CONNECT_DATA
I suppose I have to add a Service Name into my OracleConnection chain like this:
OracleConnection conn = new OracleConnection("Data Source=xx.xx.xx.xx:xxxx;User Id=xxxxx;Password=xxxxxx;Integrated Security=True;Service Name=Oracle")
There I have a new error message: Keyword not handled:service name
If anybody has already performed an Oracle connection from visual studio (I'm on Ultimate 2013), a few help won't be in denial.
Old question, but thought I ran into the issue today so I thought it may help someone:
I had a similar issue and it ended up being that my Connection string was specifying the incorrect Data Source name. It has the match the entry you have in the TNSNAMES file on the server, and mine didn't.
Recently, I've made a simple website with a database (in an App_Data folder). I made a class file (in an App_Code folder) with the following method that will create a connection to the database:
public static SqlConnection ConnectToDb(string dbFileName)
{
string path = HttpContext.Current.Server.MapPath("App_Data/");
path += dbFileName;
string connString = #"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
path +
";Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connString);
return conn;
}
When I need to open that connection, I get the following message:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
What I don't understand is that I used the -same- method in another website that I built over a year ago in Visual Studio 2010, where it worked fine.
I checked the path and it was correct, same with the database file name in the parameters.
What is wrong with the code or with VS's settings? Thanks in advance.
EDIT: Just tried to use (localDB)\v11.0 instead of .\SQLEXPRESS
and now I get this error message:
The user instance login flag is not allowed when connecting to a user instance of SQL Server.
You're getting that error because the (localDB)\v11.0 is already a user instance of SQL Server. When you were using the SQLEXPRESS SQL Server instance, well, it's not. So, just get rid of the User Instance=true on the connection string.
I have the following code to connect to a sql server compact edition 2008:
private SqlConnection sqlConn;
public void createConnection()
{
String connectionString = #"Data Source=C:\Projects\somefile.sdf;Persist Security Info=False";
sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
}
However, I keep getting the following error when sqlConn.Open() is executed:
"A network-related or
instance-specific error occurred while
establishing a connection to SQL
Server. The server was not found or
was not accessible. Verify that the
instance name is correct and that SQL
Server is configured to allow remote
connections. (provider: SQL Network
Interfaces, error: 26 - Error Locating
Server/Instance Specified)"
Does anyone have any ideas what the problem might be? I can create a connection to the db in the database explorer but it doesn't seem to work in code.
The Sql* classes in System.Data.SqlClient can only be used to connect to a regular SQL Server instance.
To connect to a SQL CE database, you need to create a SqlCeConnection object in System.Data.SqlServerCe.dll.
Is the sdf file in the same directory as the executing application?
How to specify the location of the SDF file
Often times the .SDF database is not running in the current directory so it becomes necessary to programatically set the path to the SDF file. This is an example (.net C#) on how to do this when the SDF file is located in the same directory as the executing application.
Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyData.sdf;Persist Security Info=False;
Source