Jump to content

[Answered] Remote Access To Database


Recommended Posts

Posted

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();
			  
		    }
	  
	    }
    }
   
   }

}

Posted

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.

Posted

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#.

  • Like 1
Posted
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);
		    }
			   
		  
	   
    }
   
   }

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...