Jump to content

[SOLVED] connecting to MySQL with C#


Recommended Posts

Ok, I have the MySQL Connector installed and referenced in my project.  I have "using MySql.Data.MySqlClient;" at the top, I am using this code to connect:

 

private void btnConnect_Click(object sender, EventArgs e)
        {
            string strConnection = "Server=[removed];Database=[removed];Uid=[removed];Pwd=[removed];";
            MySqlConnection conSQL = new MySqlConnection(strConnection);
            conSQL.Open();
        }

 

(I've removed the values for server, database, user, and password).  This should connect to the database when I click the button...well, it doesn't.  It returns an error:

 

Access denied for user '[username'@'99-68-44-40.lightspeed.hstntx.sbcglobal.net' (using password: YES)

 

now "'99-68-44-40.lightspeed.hstntx.sbcglobal.net'" is the first hop I get when I run a tracert, so that's my service provider.  Why can't I get past my service provider (AT&T) when trying to connect to a MySQL server on the internet through C#?

 

Please help, I can't complete this project (or even go forward) without being able to do this!

Link to comment
Share on other sites

Why can I connect to my database via shell if remote access is disabled?  I don't understand what exactly I have to do?  Adding privelages?  This is the command I use to connect to mysql via shell:

 

shell> mysql -h [hostname] -u [username] -p[password] database

 

Should be the same concept right?  I'm remotely connecting to the database, is it different because I'm connecting through shell?  What privelages would I have to add to be able to access the database from my computer, and would it affect the way I access my database with php on my site?

Link to comment
Share on other sites

Are you connecting from you local machine shell, or from remote machine shell? If the latter, you're connecting from 'inside' so to speak.

 

First of all just check what privileges are set already. phpMyAdmin has a nice interface for that.

Link to comment
Share on other sites

Ah, I figured it out.  I control most of the properties of my databases and accessing user accounts through the control panel at my webhost, I just had to set my username as able to access any database from % hosts (so any host, instead of just %.dreamhost.com, which is the webhost).  That made it work.  Now I just have to figure out how to reuse query veriables so I can do multiple queries over and over.  Right now I have a textarea, a textbox, and a button.  You click the menu bar and click connect then you can type a query in the textbox and click the execute button and it'll show the result in the textarea, but when you try to type in a new query and click the Execute button again it throws an error, know any way around that?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.