Unable to connect to MySQL database through Visual studio 2012 - mysql

We encountered an error while trying to connect. Here is my code :
public void BindData()
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=abc");
con.Open();
MySqlCommand cmd = new MySqlCommand("Select * from register", con);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.Dispose();
con.Close();
}
But it gives following error :

You should check if the reference Renci.SshNet is existing in your Projects's references. If none, find the .dll file then add it respectively.
Regards,
Hack Dawg

Related

system 'ArgumentException' occurred in system.data.dll

My Question is can I pass the string to MySqlConnection this way??
The Error is in this Line below in the code "MySqlConnection conn = new MySqlConnection(myconnstr);"
It worked for SQLConnection but not for MySqlConnection ... is it not possibl?
kindly help me With the right solution
thanks!
string myconnstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
//Selecting Data from databse
public DataTable Select()
{
MySqlConnection conn = new MySqlConnection(myconnstr);
DataTable dt = new DataTable();
try
{
string sql = "SELECT * FROM Member";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;
}

Internal connection fatal error in opening connection

In Asp.net mvc, I tried to open a sql connection, but it always gives me invalid connection fatal error.
Here is my following code:
In Web.config:
<connectionStrings>
<add name="test" connectionString="Data Source=test,3306;Initial catalog=test;User Id=xxxxx;password=xxxx;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />
In Controller class:
var sql = "select * from users where email = \"" + model.Email + "\" and password = \"" + model.Password +\"";
System.Diagnostics.Debug.WriteLine("return url = "+sql);
string connectionString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
using (var conn = new SqlConnection(connectionString))
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
System.Diagnostics.Debug.WriteLine("output = " + dr.HasRows);
conn.Close();
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine("exception " + e.ToString());
}
}
It gives me following exception on conn.open();
System.InvalidOperationException: Internal connection fatal error. Error state: 18
Can anyone suggest what is wrong in my code.
public MySqlConnection GetMySqlConnection(string dataBase, string server,uint port,string userID,string pass)
{
MySqlConnectionStringBuilder myBuilder = new MySqlConnectionStringBuilder();
myBuilder.Database = dataBase;
myBuilder.Server = server;
myBuilder.Port = port;
myBuilder.UserID = userID;
myBuilder.Password = pass;
MySqlConnection myconn = new MySqlConnection(myBuilder.ConnectionString);
myconn.Open();
return myconn;
}
Description:
You should use MySql instead of MS SQL.
Install MySql connector net and MySql for Visual Studio ("just google them")
After install:
you must to add MySql.Data in References
in your class: using MySql.Data.MySqlClient;

Xamarin SqlConnection throwing error Input string was not in correct format

Im trying to connect my mobile to PC database using this code:
string cString = #"Persist Security Info=False;Integrated Security=false;Initial Catalog=myDB;server=192.168.1.11,1433\SqlExpress";
SqlConnection connection = new SqlConnection(cString);
connection.Open();
using (SqlCommand cmd = new SqlCommand("SELECT name1 FROM Product", connection))
{
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
t.Text += rdr.GetString(rdr.GetOrdinal("name1")) + System.Environment.NewLine;
}
}
}
Unfortunately I'm getting error: "Input string was not in the correcr format", source: mscorlib.
I know that there's error in connectionString (throwin exception after connection.Open())
Any ideas? Thanks in advance!
your current Culture is not in correct format
protected override void OnResume()
{
base.OnResume();
//Here you would read it from where ever.
var userSelectedCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = userSelectedCulture;
}

Mysql .NET Connection String Error

My database is on online which is cheapitservice.com. The Mysql connector produces error like this:
Unable to connect to any of the specified MySQL hosts
And my code is:
dbConn.ConnectionString = "Server=......hostedresource.com;Database=sampledb;user=user;password=test;"; dbConn.Open();
Is there something wrong with my code? Please help me.
mySQLconnector is version 6.6.5
you can use this link to make works your code... ConnectionsString with this you can realize whats wrong...
Example:
static void Main(string[] args)
{
var ConnectionString = #"server=localhost; user=root; password=a54321; database=Prueba";
using (MySqlConnection con = new MySqlConnection(ConnectionString))
{
con.Open();
using (MySqlCommand Command = new MySqlCommand("SELECT IdEmpleado, Nombres, Apellidos, Correo, Telefono FROM Empleados", con))
using (MySqlDataReader Reader = Command.ExecuteReader())
{
while (Reader.Read())
{
Console.WriteLine("{0} - {1} - {2} - {3} - {4}",
Reader.GetInt32(0), Reader.GetString(1), Reader.GetString(2), Reader.GetString(3), Reader.GetString(4));
}
}
}
Console.ReadKey();
}
Regards

calling mysql storedprocedure from c#?

im trying to call a mysql stored procedure from my c# application , the procedure has 3 parameters which im passing dynamically, the problem is im not error nor the output, just im getting empty datatable
MySqlConnection con = new MySqlConnection(myConnectionString);
con.Open();
MySqlCommand cmd = new MySqlCommand("User_details", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("username", "aditya");
cmd.Parameters.AddWithValue("password", "123");
cmd.Parameters.AddWithValue("gender", "male");
MySqlDataAdapter da= new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView2.DataSource = dt;
I think this example will help you
protected DataTable RetrieveEmployeeSubInfo(string employeeNo)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
try
{
cmd = new SqlCommand("RETRIEVE_EMPLOYEE", pl.ConnOpen());
cmd.Parameters.Add(new SqlParameter("#EMPLOYEENO", employeeNo));
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cmd.Dispose();
pl.MySQLConn.Close();
}
return dt;
}
or visit this page with the same information http://www.java2s.com/Code/CSharp/Database-ADO.net/ModifyDataTableinsertdatatodatabasetable.htm
try this code
DataTable dt = new DataTable();
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlDataAdapter da =new MySqlDataAdapter(nom_fonction,connection);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("id", MySqlDbType.Int64).Value = id;
da.Fill(dt);
connection.Close();