How to connect to oracle using Oracle.ManagedDataAccess - c#

I am trying to connect to oracle data base using below code.
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string connectionString =
"Data Source=a;User Id=id;Password=pwd;";
OracleConnection con = new OracleConnection(connectionString);
con.Open();
Console.WriteLine("Connected to Oracle Database {0}", con.ServerVersion);
con.Dispose();
Console.WriteLine("Press RETURN to exit.");
Console.ReadLine();
}
}
}
Its throwing exception saying:
An unhandled exception of type
'Oracle.ManagedDataAccess.Client.OracleException' occurred in
Oracle.ManagedDataAccess.dll
Additional information: ORA-12514: TNS:listener does not currently
know of service requested in connect descriptor.
Can any body correct me what was going wrong with the above code?

the issues maybe is the connection string, if you haven't TNS name "a" registered
you can try this
string connectionString =
"Data Source=localhost:1521/xe;User Id=USERDB;Password=pwd";
by default XE is name for oracleservice
where
Data Source -> localhost:1521/xe (ip:port/servicename)
User Id -> you Oracle user
Password -> you Oracle password

Related

Connection to Mariadb in a C# application

I am new to c#. I am trying to make a connection to my On-Premise MariaDb Database using a simple windows form application. Where my connection is getting established successfully but later it was getting closed by my remote host forcibly. I am attaching my code.Can you guys check this code and help me what is the missing piece. Thanks in advance
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connectionString;
SqlConnection cnn;
connectionString = #"Data Source=xxx.xxx.x.xx,xxxx;Initial Catalog=dataBaseName;User ID=UID;Password=Password";
cnn = new SqlConnection(connectionString);
cnn.Open();
MessageBox.Show("Connection Open !");
cnn.Close();
}
}
}
This is the error I am getting
System.Data.SqlClient.SqlException: 'A connection was successfully
established with the server, but then an error occurred during the pre-login > handshake. (provider: TCP Provider, error: 0 - An existing connection was
forcibly closed by the remote host.)'

Is it an error in my class? MySql connection isn't working properly [Visual C#]

Okay, I'm trying to make a simple project for school, in which the teacher is letting us use the root user so we connect to databases everything. But while compiling, there seems to be an error in my class I use to connect to MySql:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
namespace BuscarDatos
{
class ClaseConectar
{
public static MySqlConnection obtener_conexion()
{
MySqlConnectionStringBuilder c = new MySqlConnectionStringBuilder();
c.Server = "localhost";
c.UserID = "root";
c.Password = "*******"; //password
c.Database = "proyecto_almacen"; //my database name already created
MySqlConnection con = new MySqlConnection();
con.Open();
return con;
}
}
}
I keep getting this error:
MySql.Data.MySqlClient.MySqlException: 'Authentication to host '' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''#'xxxxxxxxxx' (using password: NO)'
Also, I changed it to MySqlConnection since I got an error before in another class, about how I couldn't convert MySql into Sql only. Here's an example of the class I changed it for:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace BuscarDatos
{
class Agregar
{
public static int AddName (string pNombre,string pAp_paterno, string pAp_materno, string pMatricula,
string pDireccion, string pTelefono)
{
int resultado = 0;
MySqlConnection con = ClaseConectar.obtener_conexion();
MySqlCommand Comando = new MySqlCommand(string.Format("Insert into maestros (nombre, ap_paterno, ap_materno, matricula," +
"direccion, telefono) values('{0}','{1}','{2}','{3}','{4}','{5}')", pNombre, pAp_paterno, pAp_materno,
pMatricula, pDireccion, pTelefono), con);
resultado = Comando.ExecuteNonQuery();
con.Close();
return resultado;
}
}
}
You're not giving the connection any connection string. It doesn't know what to open.
When you create the MysqlConnectionStringBuilder it is completely separate from the MySqlConnection you create. You must use them together:
MySqlConnection con = new MySqlConnection(c.ConnectionString);
This way you're actually giving the generated connection string to the connection object.

Connection Strings and Dapper

I am trying to connect to an SQL db to extract data and I am using Dapper for the process. I assume I am having some problems with the connection string; I am trying to access a remote sql server 2012 db but my connection keeps failing:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
namespace Data_Connection
{
public static class Program
{
static void Main(string[] args)
{
string connetionString = null;
SqlConnection cnn ;
connetionString = "Server=datanet.local.net/datadb; Database=ddata;Trusted_Connection=True";
cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
Console.WriteLine("Connection Open !");
cnn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Can not open connection !");
}
}
}
}
I have never tried connecting to an sql db using c# and I am fairly new to the environment. Any help would be appreciated
Thanks!

Can't able to connect to Database

I have installed the MySQL server using XAMMP.Created a new database with some data using phpmyadmin.Then i tried to connect to database using this code.But it did not connect.It shows me an error.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing!");
SqlConnection myco = new SqlConnection("server=localhost;User Id=root;database=customer;Trusted_Connection=yes");
try
{
myco.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.ReadKey();
}
}
}
Error:
http://pastebin.com/MyEtk4w6
You have MySql then you need to use the classes for MySql not the ones used for SqlServer
MySqlConnection myco = new MySqlConnection("Server=localhost;Database=customer;" +
"Uid=username;Pwd=password;");
Also the connection string for MySql is different
Server=localhost;Database=customer;Uid=username;Pwd=password;
The connection strings for MySql could be very numerous, you should check the correct one for your requirement here at connectionstrings.com
It's because you're using a SqlConnection object which is for Microsoft SQL Server.
Use a MySQLConnection instead.

C# 2010 Express + SQL Server 2008 Express - Connection "Login failed"

I am actually developing a Windows Forms Application with Visual C# Express 2010 which would use (read/write) data from a SQL Server 2008 Express DB
I have created my DB with SQL Server Management Studio (2008 Express),
I understand the instance is named ATLELAG786576\SQLEXPRESS
My DB is called 'TEST'
Looking at my DB 'TEST' Properties in SQL Server Management Studio (2008 Express):
Under Files, I am (ATLE\bneveux) the owner of the DB
Looking under Security, Logins, Mylogin (ATLE\bneveux)
My default DB is 'TEST'
Server roles are 'public' + 'sysadmin'
User Mapping DB 'TEST' User 'dbo' Default Schema 'dbo'
In my C# application
app.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="connectionStringTestDb"
connectionString="Data Source=ATLELAG786576\SQLEXPRESS;Initial Catalog=D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf;Integrated Security=True;Connect Timeout=30;User Instance=False"
providerName="System.Data.SqlClient" />
</connectionStrings> </configuration>
dbConnection.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace SQLServerConnectionDemo
{
class dbConnection
{
public static SqlConnection newCon;
public static string connectionStringTestDb = ConfigurationManager.ConnectionStrings["connectionStringTestDb"].ConnectionString;
public static SqlConnection GetConnection()
{
newCon = new SqlConnection(connectionStringTestDb);
return newCon;
}
}
}
dbAccess.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SQLServerConnectionDemo
{
class dbAccess
{
SqlConnection conn;
public dbAccess()
{
conn = dbConnection.GetConnection();
}
//Method insert new in tblEmployees
public void addEmployee(string Id, string Name, string Email)
{
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "INSERT INTO tblEmployees VALUES ('"+ Id +"','"+ Name +"','"+ Email +"')";
newCmd.ExecuteNonQuery();
}
}
}
in a form formEmployeeAdd.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SQLServerConnectionDemo
{
public partial class formEmployeeAdd : Form
{
dbAccess access = new dbAccess();
public formEmployeeAdd()
{
InitializeComponent();
}
private void btnInsert_Click(object sender, EventArgs e)
{
access.addEmployee(txtId.Text, txtName.Text, txtEmail.Text);
MessageBox.Show("Data successfully added");
}
}
}
And here the error message I always get when trying to run this process:
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf" requested by the login. The login failed.
Login failed for user 'ATLE\bneveux'.
Note that I have never really been able to add my Data Source in Visual C# 2010 Express so I could manage the DB from VS, I always get the following error message:
Unable to open the physical file "D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf". Operating system error 32: "32(Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus.)".
An attempt to attach an auto-named database for file D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Try replacing
Initial Catalog=D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf
with simply
Initial Catalog=TEST

Categories

Resources