C# project on VMWare and the database is outside of VMWare - c#

I have a C# project on VMWare and my database is outside of VMWare. It's on my real PC and the VMWare is on the same PC. My question is how can I access my database on my PC ? I have this class but it does not work.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace EyeCareCenter
{
class DataBase
{
private string connectionstring = #"Data Source =127.0.0.1,1433 ; Network Library=DBMSSOCN ;Initial Catalog=EyeCareCenter ; Trusted_Connection=True ";
private SqlConnection connection = new SqlConnection();
public void OpenConnection()
{
try
{
connection.ConnectionString = connectionstring ;
connection.Open();
// MessageBox.Show (" Connection successfull ... ");
}
catch ( Exception a)
{
MessageBox.Show(a.Message + " ");
}
}
public void CloseConnection()
{
connection.Close();
}
public SqlConnection GetConnection()
{
return connection;
}
}
}

At a minimum, your connection string is trying to connect to database inside your VM, not on host machine:
private string connectionstring = #"Data Source =127.0.0.1,1433 ; Network Library=DBMSSOCN ;Initial Catalog=EyeCareCenter ; Trusted_Connection=True ";
The IP should be changed to that of your host machine to connect to it. And make sure that your VM is configured for networking, and that networking configuration allows for connecting to your host machine.

Related

Connection to mysql database does not work c #

I'm trying to connect to my mysql database (I'm hosted by infinityfree.net), entering these credentials:
connection = new MySqlConnection("Server=sql306.epizy.com; Port=3306; Database=epiz_27674946_db1; Uid=epiz_27674946; Pwd=**********; ");
But I get this exception:
Unable to connect to any of the specified mysql hosts
I don't know why this happens, the credentials should be right...
Can you help me?
Full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace sharetru
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MySqlConnection connection;
private void Form1_Load(object sender, EventArgs e)
{
try
{
connection = new MySqlConnection("Server=sql306.epizy.com; Port=3306; Database=epiz_27674946_db1; Uid=epiz_27674946; Pwd=*******; ");
connection.Open();
if (connection.State == ConnectionState.Open)
{
label1.Text = "Connected";
label1.ForeColor = Color.Green;
}
else
{
label1.Text = "Not Connected";
label1.ForeColor = Color.Red;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Regarding this service (infinityfree) it is not possible to access mysql from desktop applications (outside the infinityfree host)
https://support.infinityfree.net/mysql/how-to-connect-with-mysql/
The code looks correct. The issue probably is in the connection string details.
Are you able to connect using the following connection details via some GUI tool?
Workbench for example https://dev.mysql.com/downloads/workbench/

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!

Connecting to SQL Server database using default connection string from C#

I've been trying to make a connection with my SQL Server database (Mynewdatabase) using the default connection string that was generated (supposedly) when I added the data source for this project. However, I'm thus far unable to make a connection: I'm getting an error that "the server was not found or was not accessible".
This my code. Thanks so much for any help you can offer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace Parsevcf2sql
{
class Program
{
static void Main(string[] args)
{
// here is where I get what I believe is an appropriate connection string
string cs = Parsevcf2sql.Properties.Settings.Default.MynewdatabaseConnectionString;
SqlConnection myconnection = new SqlConnection(cs);
Console.WriteLine(cs);
try
{
myconnection.Open();
Console.WriteLine("This worked!");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
This might help you out, change localhost to your address and database name as well.. But Please check sql server must be in running mode
{
// connection string!
SqlConnection myConn = new SqlConnection("Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=Mynewdatabase;");
try
{
myConn.Open();
Console.WriteLine(myConn );
}
catch (System.Exception)
{
// some exception
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
myConn.Dispose();
}
}
or check out this tutorial

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