I'm trying to connect from my C# windows application to MySQL database which is on my host of my website.How can i make connection? I downloaded a simple connection project from "Codeproject" but it's not inserting anything.I also installed MySQL connector 6.1 as required.
server = "" //local host in php
database = "database";
uid = "username";
password = "pass";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
//Insert statement
public void Insert()
{
string query = "INSERT INTO Orders (name, age) VALUES('John Smith', '33')";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
can any one of you please tell me what to type in "server" string i tried server ip but don't work
can any one of you please tell me what to type in "server" string i tried server ip but don't work
sudipta sanyalPosted May 13, 2014, 5:28 AM
go throw
http://sudiptasanyal.blogspot.in/2011/03/how-you-connect-mysql-databse-with.html
Anupam SinghPosted May 12, 2014, 6:58 AM
To access MySql DB from Internet there are two important thing you have to configure
1. Open the port 3306(which is default port for MySQL) for incoming requests on the server.
2. Access DB using TCP\IP (IP address).
Sanjay KumarPosted Apr 30, 2014, 4:34 AM
try this
https://www.connectionstrings.com/mysql/
http://stackoverflow.com/questions/10065259/connection-string-for-mysql-database-on-webserver-having-shared-ip-address-c-s
http://www.c-sharpcorner.com/UploadFile/47548d/install-mysql-connector-in-visual-studio-2012/
http://www.c-sharpcorner.com/UploadFile/47548d/how-to-retrieve-data-from-mysql-database-in-C-Sharp/
http://www.c-sharpcorner.com/UploadFile/47548d/simple-insert-select-update-and-delete-in-Asp-Net-using-mysq/
VulpesPosted Apr 29, 2014, 8:10 PM
NickPosted Apr 29, 2014, 12:33 PM
VulpesPosted Apr 29, 2014, 11:30 AM
Have you tried reading data from the database using the same set up rather than writing to it?
NickPosted Apr 29, 2014, 11:25 AM
VulpesPosted Apr 29, 2014, 11:14 AM
Do you know whether the id column is generated automatically? If it isn't then you'll need to pass an INT value for that if it can't be NULL.
NickPosted Apr 29, 2014, 11:00 AM
VulpesPosted Apr 29, 2014, 10:49 AM
NickPosted Apr 29, 2014, 10:36 AM
Kunal VaishyaPosted Apr 29, 2014, 8:34 AM
Use IP Address instead of Server Name and try.