Connect to SQL using OleDbConnection failing - c#

I'm connecting to a remote SQL database using the OleDbConnection but it's failing.
var connection = new OleDbConnection
{
ConnectionString = "Provider=SQLOLEDB;server=MyRemoteIPAddress;Initial Catalog=MyDatabase;User ID=sa;Password=MyPassword"
};
try
{
connection.Open();
connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
The error I am getting is : [DBNETLIB][ConnectionOpen (Invalid Instance()).]Invalid connection.
However if I connect using SqlConnection, it connects ok.
var testConnection = new SqlConnection
{
ConnectionString = "server=MyIPAddress;database=MYDatabase;user id=MyUser;pwd=MyPassword"
};
try
{
testConnection.Open();
testConnection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Why can i connect via SqlConnection and not OleDbConnection?

The environment is not clustered but explicitly specifying the port fixed the issue.
This worked :
"Provider=SQLOLEDB;server=MyRemoteIPAddress,1433;Initial Catalog=MyDatabase;User ID=sa;Password=MyPassword"

Try using a server side code which does all the required functions. so that your application does not directly talk with the remote database. Ping me for further assistance.

you should specify the port 1433 when using OLEDB
Provider=SQLOLEDB;server=MyRemoteIPAddress,1433;Initial
Catalog=MyDatabase;User ID=sa;Password=MyPassword;

Related

How to test for database availability in c# script task for an oledb connection

I have to check for database availability using c# scripting for an "oledb" connection in an ssis package.
Since my connection is oledb I'm not able to use AcquireConnection method.
I tried using ConnectionString property of ConnectionManager but I get the connection string even when database is offline.
Any idea how to go about with unmanaged Objects here oledb.
You can find the complete documentation here https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ado-net-code-examples
what you may need to test the oledb connection is then something like this:
string connectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ "c:\\Data\\Northwind.mdb;User Id=admin;Password=;";
OleDbConnection connection = new OleDbConnection(connectionString);
try
{
connection.Open();
connection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return true;

Cannot connect to MySQL Server and have no idea why [duplicate]

This question already has answers here:
ASP.NET use SqlConnection connect MySQL
(3 answers)
Closed 2 years ago.
I am trying to connect to MySQL server
Screenshot from MySQL Workbench:
using System;
using System.Data.SqlClient;
namespace C_sharp_test
{
class Program
{
static void Main(string[] args)
{
try
{
string connectionString = "server=localhost;uid=root;pwd=***********;database=terra_incognita_online";
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
Console.WriteLine("open");
sqlConnection.Close();
Console.WriteLine("close");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
I get this error
System.Data.SqlClient.SqlException (0x80131904): 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)
I have no idea what to do, please help
Replace "SqlConnection" with "MySqlConnection" and that will work.
namespace C_sharp_test
{
class Program
{
static void Main(string[] args)
{
try
{
string connectionString = "server=localhost;uid=root;pwd=***********;database=terra_incognita_online";
MySqlConnection sqlConnection = new MySqlConnection();
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
Console.WriteLine("open");
sqlConnection.Close();
Console.WriteLine("close");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
Also, Install nuget package MySql.Data;
And Add Reference
using MySql.Data.MySqlClient;
You are trying to connect to a MySQL database server with classes intended for SQL Server. They use different protocols, so that doesn't work (and almost certainly never will).
Have a look at the MySQL connector for .NET, if you want to use MySQL rather than SQL Server.

Connect other server database

I have a console app and want to connect other windows server where I have already created user, database table. In conf.xml I have
<configuration>
<crypto location="172.16.10.34" database="Crypto_Pan" username="PinbySms" password="ASDasd123"/>
</configuration>
then my code is :
Dictionary<string, string> d = conf.getCrypto();
SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();
bu.DataSource = d["location"];
bu.NetworkLibrary = "DBMSSOCN";
bu.InitialCatalog = d["database"];
bu.IntegratedSecurity = false;
bu.UserID = d["username"];
bu.Password = d["password"];
SqlConnection thisConnection = null;
try
{
thisConnection = new SqlConnection(bu.ConnectionString);
thisConnection.Open();
Console.WriteLine("success connected");
}
catch (Exception e)
{
Console.WriteLine("exeption: " + e.ToString());
thisConnection.Close();
}
I have an error when my app try to connect this database:
[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.
Does anybody have any suggestions?
there was not default port in sql configuration PROTOCOL MSQSQLSERVER TCP/IP , i changed it to 1433 and now it's ok.

VS C# connected to mySQL database

I am very new in C# and now try to find online resource to connect VS C# to mySQL database at server 'localhost', with userid 'root', and password '****', the databasename is 'dlht'.
1.I copied a line of code from youtube and it works:
this.stockTableAdapter.Fill(this.blhsDataSet.stock);
Can anyone explain to me what exactly this is doing? There is no place to put server, password, userid etc... How can it work?
I tried to use the online tutorials to connect to mySQL database
ZetCode C#Tutorial
string cs = #"server=localhost;userid=root;
password=****;database=dlht";
MySqlConnection conn = null;
try
{
conn = new MySqlConnection(cs);
conn.Open();
Console.WriteLine("MySQL version : {0}", conn.ServerVersion);
} catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
} finally
{
if (conn != null)
{
conn.Close();
}
}
I run this code at Form1.cs at VS C#. It is always stuck at :
conn = new MySqlConnection(cs);
Why? Thank you so much!
It seems that the proper way to connect to MySql database is this
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

How to create connection with online mysql database in C# windows form?

I am creating connection with my online mysql database but it gives me the error (unable to connect to any of the specified Mysql hosts).
this is my code:
private void button1_Click(object sender, EventArgs e)
{
try
{
MySqlConnection con1 = new MySqlConnection("Server=http://eu5org.freewebhostingarea.com; Port=3306; Database=477928; Uid=477928; Pwd=password123;");
con1.Open();
MessageBox.Show("Connection Established");
con1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
how could i correct this?
It seems that the hosting you have only allows a server address of localhost any other address will fail.

Categories