How to connect to Oracle DB from .NET? - c#

When I open SQL Command Line, I write CONNECT username/password#[//]host[:port][/service_name] and it connects me to the database just fine. However, I'm unable to connect from a .NET project using a connection string. I have tried many things such as <add name="ConnectionString" connectionString="Data Source=username/password#[//]host[:port][/service_name];" /> and <add name="ConnectionString" connectionString="server=tcp:host;Initial Catalog=service_name; user id=username; password=password; Connection Timeout=180;" providerName="System.Data.OracleClient" />, but nothing worked so far. Whenever I get to sconn.Open(); in the following:
var CurrentConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection sconn = new SqlConnection(CurrentConnectionString);
sconn.Open();
I practically always get the following error:
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: 25 - Connection string is not valid)
How can I connect to the database properly?

Try following connection string
string con = "Data Source=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST = 000.00.0.00)(PORT = 0000)))(CONNECT_DATA =(SERVICE_NAME = database)));User ID=User/Schema;Password=password;Unicode=True";
& then use this conection string as follow
using (OracleConnection objConn = new OracleConnection(con))
{
\\ code
}

I highly recommend using the "Official Oracle ODP.NET, Managed Driver".
https://www.nuget.org/packages/Oracle.ManagedDataAccess/
Or the one for Entity framework:
https://www.nuget.org/packages/Oracle.ManagedDataAccess.EntityFramework/
After installing via Visual Studio it opens a readme that I also recommend you read.
Oracle provide massive amounts of documentation. This is where to start:
http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.html

You use System.Data.SqlClient.SqlConnection which is used to connect to a (Microsoft) SQL Server, it cannot be used for an Oracle connection.
You should use System.Data.OracleClient.OracleConnection or Oracle.ManagedDataAccess.Client.OracleConnection
Have a look at this answer to see other possibilities:
How to connect to Oracle 11 database from . net

You can always use EF, and use create one ADO.Net Entity Data Model
And use the wizard to create and test the connection

Related

Problems with SQL Server connection in console app

The SQL server is located on a remote machine.
I can connect to it with MVC app.
I can connect to it with SSMS.
But I can not do it in console app.
Neither entity nor SqlConnection is working.
Connection string is the same as in console and mvc app (which works). What can it be? Any ideas?
Error I am getting
System.Data.SqlClient.SqlException: '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: Named Pipes Provider, error: 40 -
Could not open a connection to SQL Server)'
What can it be?
<add name="DBContext" connectionString="data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "data source=serverip;initial catalog=catalogue;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient";
cn.Open();
Console.WriteLine(cn.State.ToString());
The problem is that console app uses 445 and 139 port. In terms of security. But why MVC app doesn't use it. And how I can I use it without opening this port. Even dividing db part in different library didn't work.

Unable to connect to MySql Server in Visual C#

I am trying to create a local database using MySql in Visual C# but connection is not getting established in the code but when i add the server in the server explorer under data connections its working. I tried test connection it says connection succeeded but its not working in the code.
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
con = new SqlConnection(connStr);
This throws an error saying
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
You cannot use the SqlConnection class to connect to a MySQL server; that is for Microsoft SQL Server only.
Instead, install the MySqlConnector NuGet package and use the following code:
string connStr = "server=localhost;user=root;database=entry_database;password=superadmin";
using (var con = new MySqlConnection(connStr))
{
// ...
}
Your connection string is wrong.. change keys "user" to "Uid" and "password" to "Pwd".
follow eg:
string connStr = "server=localhost;Uid=root;database=entry_database;Pwd=superadmin";
con = new SqlConnection(connStr);
Correct, the format used by you to create the connection string is incorrect, giving you the error you mentioned above.
Actually, the way the connection string is written in MS-SQL is very different from MY-SQL. Every database has its own way of connecting and its individual connection tags:-
Please find the list of connection details for MS_SQL below. This would help you not only in the current mentioned scenario but also will provide a reference for future changes:-
https://www.connectionstrings.com/mysql/

asp.net connecting to an external database

I have a an external database hosted by Strato and I would like to connect my asp.net c# to the external database and insert a new user in to the database but I got an error when I tried to established the connection. The error occured at con.open(); Can anyone help me with this? I am new to asp.net so any sample code may be helpful. For now I am just trying to establish a connection to the database and transfer a static data to the table upon clicking the submit button.
web.config
<connectionStrings>
<add name="ApplicationServices"
connectionString="server=rdbms.strato.de;database=DB33321; UID=abc123; password=pass123"
providerName="System.Data.SqlClient" />
</connectionStrings>
aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("insert into USER"+"(name)values(kim)");
Response.Write("Registration success!");
con.Close();
}
The error i received:
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
You probably found examples of connecting to a database without realizing there are several providers for ADO.Net. For MySql you need a nuget package:
https://www.nuget.org/packages/MySql.Data/
You will then change your connection string so the Provider is not System.Data.SqlClient. I believe it should be MySql.Data.MySqlClient
Next you change your code so that it uses the correct Types. Wherever you see "Sql..." you change it to "MySql...". for instance:
SqlConnection becomes MySqlConnection.

C# Update Connection String Visual Studio

I am having some connectivity issues with Visual Studio and SQL Server.
This is the connections string in my web.config file;
<connectionStrings>
<add name="WintipToysConnectionString" connectionString="Data
Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated
Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
According to my DB connection string found in Visual Studio, my connection string is;
Data Source=localhost\sqlexpress;Initial Catalog=WINGTIPTOYS;Integrated Security=True
Which seems to add up - but, I get this error when my program hits a certain line;
System.Data.SqlClient.SqlException: '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: 50
- Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
I am actually following the WingTipToys tutorial on the Microsoft website and I am struggling to get it to use the Database I attached to SSMS.
I can write queries inside VS which works fine. So I don't really understand why my program is having this error when running?
Thanks.
Admittedly haven't used SQLExpress in a looong time. It looks like the syntax you're using is for an "instance" - which is pointed out in the error message:
...Cannot create an automatic instance...
What you have does look like server\instance conn string syntax...
Connect to the "default" instance of SQL Express like so: Data Source=.\\SQLExpress
Refer to this:
SSE Connection string sample
Hth...

Connection to SQL Server Express from Visual Studio

I created a database using SQL Server Express from Visual Studio 2012 and I fail to open a connection pointing that database.
I've tried many way like for example:
SqlConnection connectionLocale = new SqlConnection(#"Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\Users\Max\Desktop\presentoir\Application\WpfAppTest\BDD.mdf;Integrated Security=True");
But I keep on getting the same error 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326)
As I am a total beginner, even with two days of research, I really can't figure out what the problem is.
I can get a connection using :
SqlConnection connectionLocale = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|BDD.mdf;Integrated Security=True");
But I can't change my database using C# code by connecting like this (I think it creates a temporary database in appdata, and that all the modifications are done in this database).
Every idea helping me to create a connection to my database is welcome.
Okay, a coworker found the solution.
God, it was obvious...
With the # behind the string, the correct data source was :
#"Data Source=(LocalDB)\v11.0
and not :
#"Data Source=(LocalDB)\\v11.0

Categories