Jump to content

[Answered] Remote Access To Database


rhonin

Recommended Posts

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

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...