rhonin Posted December 29, 2012 Share Posted December 29, 2012 Hello,i am trying to connect to mysql database remotely using C# applications (code is below). However i will always get error on connection.Open() -> server could not be found. I tryed to connect to database localy from web and that worked fine. In remote access in Cpanel i wrote just % (i did not understand that very well). I tryed to change server from Stevie.heliohost.org to IP with port but did not work 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 System.Data.SqlClient; namespace Databaza { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // } private void button1_Click(object sender, EventArgs e) { Dataconnect bublatanka = new Dataconnect(); bublatanka.ConnectToDatabase(); } } class Dataconnect { public void ConnectToDatabase() { int id = 2; string meno = "Hallder"; int pocet = 2500; string db = "server = 65.19.143.2:3306; database = rhonin_da; uid = rhonin_da; password = **********"; ; using (SqlConnection connection = new SqlConnection(db)) { using (SqlCommand command = new SqlCommand("INSERT INTO Suroviny (ID, Meno, Suroviny) VALUES (@id, @meno, @pocet", connection)) { command.Parameters.Add(new SqlParameter("id", id)); command.Parameters.Add(new SqlParameter("meno", meno)); command.Parameters.Add(new SqlParameter("pocet", pocet)); connection.Open(); command.ExecuteNonQuery(); } } } } } Quote Link to comment Share on other sites More sharing options...
Shinryuu Posted December 29, 2012 Share Posted December 29, 2012 Use your domain name as the server name. In the remote mysql page limit the allowed IPs to the block that is used by your home/work router, just setting it to allow connections from any IP is dangerous. also try: using (SqlConnection connection = new SqlConnection(db)) { connection.Open(); using (SqlCommand command = new SqlCommand("INSERT INTO Suroviny (ID, Meno, Suroviny) VALUES (@id, @meno, @pocet", connection)) { command.Parameters.Add(new SqlParameter("id", id)); command.Parameters.Add(new SqlParameter("meno", meno)); command.Parameters.Add(new SqlParameter("pocet", pocet)); command.ExecuteNonQuery(); } } and you have an extra ';' after your connection string. Quote Link to comment Share on other sites More sharing options...
rhonin Posted December 30, 2012 Author Share Posted December 30, 2012 did not help, still not working :(/&--#62; i used domain name: darkangels.heliohost.org Quote Link to comment Share on other sites More sharing options...
Krydos Posted December 31, 2012 Share Posted December 31, 2012 I don't know if this will help, but I've always used MySqlConnection() instead of SqlConnection() to connect to MySql databases through VB/C#. 1 Quote Link to comment Share on other sites More sharing options...
rhonin Posted December 31, 2012 Author Share Posted December 31, 2012 I don't know if this will help, but I've always used MySqlConnection() instead of SqlConnection() to connect to MySql databases through VB/C#. thank you very much ! This has finally worked :)/&--#62; I am posting code for ppl with same issue in future. class Dataconnect { public void ConnectToDatabase() { int id = 6; string meno = "Rhonin"; int pocet = 150000; string db = "server = darkangels.heliohost.org; database = rhonin_da; uid = rhonin_da; password = *********"; MySqlConnection conn = new MySqlConnection(db); MySqlCommand command; conn.Open(); try { command = conn.CreateCommand(); command.CommandText = "INSERT INTO Suroviny (ID, Meno, Pocet) VALUES (@id, @meno, @pocet)"; command.Parameters.AddWithValue("id", id); command.Parameters.AddWithValue("meno", meno); command.Parameters.AddWithValue("pocet", pocet); command.ExecuteNonQuery(); conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.