Where can I find MySQL Server name? - c#

I am new to MySQL, I am setting up a database connection to be used for a C# application. I am following a youtube tutorial on setting up this connection, but I am confused on what is a Server name? Is it just the IP Address or do I have to include the company domain name whos providing the server. Would the following be correct?
private void Initialize()
{
server = "xxx.1x4.xxx.15x";
database = "nameOfDatabase";
uid = "username123";
password = "pass123";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}

Using the ip adress along with the actual port for the mysql service will work unless security measures have been taken to prevent access.
example : 127.0.0.1:1840

In short, yes, you can use the IP address, although I've always used the hostname.
You might want to check out this question:
Accessing SQL Server using an IP Address and Port Number ... Help!

Related

ERROR: Unable to connect to any of the specified MySQL hosts - Synology NAS - MariaDB 10 - C#

I have a problem to connect my C# application in Visual Studio 2019 to Database inside Snyology NAS using MariaDB 10. If I connected the Database with HeidiSQL works good, but if I try to connect C# App with the same credentials I see an error like this:
"Unable to connect to any of the specified MySQL hosts"
I tried to check and I verified that:
TCP functionality is enable on Synology NAS
User and password are correct and I have all the privileges
Database name is correct
Port is also correct 3307 (setting default on Synology)
This is the code that I use to check the connection:
string connectionString = "";
string server = "ip_address_NAS:3307";
string database = "my_Database";
string username = "my_user";
string password = "my_password";
MySql.Data.MySqlClient.MySqlConnection cn;
connectionString = "SERVER=" + server + ";DATABASE=" + database + ";UID=" + username + ";PASSWORD=" + password;
try
{
cn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
cn.Open();
label1.Text = "Database ONLINE!";
label1.ForeColor = System.Drawing.Color.Green;
cn.Close();
}
catch(Exception ex)
{
label1.Text = "Database OFFLINE!\n" + ex.Message;
label1.ForeColor = System.Drawing.Color.Red;
}
Why this error?
Can you help me? please.
Thank you.
Remove the port number from the hostname. If you are connecting to the default port, there's no need to specify a port. If you are connecting to a custom port, it has to be defined in the following way
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword
Docs
Edit
3307 is not the default port for MySQL or MariaDB. So if your database is running on port 3307 (because for whatever reason Synology has chosen it to be the initial setting) you need to specify that port in the connection string. But not as part of the servername but with the Port=3307 property.

C# MySQL Change Credentials or Logout on the fly

I am currently developing a C# WPF application. It is only used by a small amount of people/devices.
To make things easier I decided to talk directly to the MySQL db.
Now I wanted to be able to switch the current User / db Credentials with the click of a button, or be able to implement a logout feature.
I just currently tried this:
public DBConnect()
{
Initialize(null, null);
}
private void Initialize(string uid, string password)
{
string connectionstring;
connectionstring = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionstring);
}
public void setCredentials(string uid, string password)
{
Initialize(uid, password);
}
public void destroyCredentialsAndConnection()
{
connection = null;
}
But apparently while Debugging I found out, that the old connection string is still cached statically by the MySQLConnection Class in the background.
Currently it does look like my approach is working, but I'm actually worried about the security of that implementation to list a few concerns:
memory dumps (usage of strings for passwords that can not be encrypted and may not be removed for quite some time by the garbage collector)
memory dumps (the fact that the connection string is being cached even after a "logout")
network traffic sniffing (is the connection between the database and my C# application encrypted)?
physical access to the server (is the MySQL database stored encrypted on the harddrive)?
Is there any better (more secure) way to switch credentials or to completely log the user out?
I did not really find any similar attempts here or anywhere else while doing research.
And if I would try to develop a php backend would that be safer without much experience? And could I still use my audit tables that I have created based on MySQL Triggers?
If I am understanding what you are asking correctly, you could try making a method to build the connection string based on user input, and inject the user's credentials into the connection string each time the method is called.
static SqlConnection Connection()
{
string UserName = UserNameField.Text;
string Password = PasswordField.Text;
SqlConnection Connection = new SqlConection("Server=ServerName,Port;Initial Catalog=Catalog;User Id=" + UserName + ";Password=" + Password + ";");
return Connection;
}

Access denied for user 'root'#'%' (using password: YES) but logged in as root

I'm aware there are similar questions all over the web, but I can't find anything for this particular issue. I have C# experience, but am pretty new to MySQL, so perhaps there's something I'm not understanding. I'm trying to make a simple select in C# from a MySQL table:
string server = "192.168.2.6";
string database = "productintegration";
string uid = "root";
string password = "Password1";
string connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection connection = new MySqlConnection(connectionString);
string query = "select * from tcdidataimport";
connection.Open();
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
DataTable dt = new DataTable();
if (dataReader.HasRows)
{
dt.Load(dataReader);
}
connection.Close();
And I get the following exception:
Authentication to host '192.168.2.6' for user 'root' using method 'mysql_native_password' failed with message: Access denied for user 'root'#'JUSTINSPERSONAL' (using password: YES)
Seems simple enough... Except that I'm already connecting to that server (via MySQL Workbench) using that exact login.
Here is show grants;
Which seems to me that I should be able to log in using root at whatever I want? When I tried to create another user (CDISelector#'%') and grant privileges I got a similar error:
But I'm logged in as root? Am I missing something here? Finally, here's the results of select user(), current_user();
JUSTINSPERSONAL is my PC, 192.168.2.6 is the MySQL machine's IP. Not sure what I'm missing here but this all seems a little strange.
And it turns out I had the password incorrect. Ha.

Connection String for MySQL (C# in asp.net)

I am using a the C# ODBC object in asp.net to connect up to a MySQL server hosted on another computer (on the same network).
<%# Import Namespace="System.Data.Odbc" %>
<html>
<body>
<script language="C#" runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
try
{
using(OdbcConnection connection = new OdbcConnection("DRIVER={MySQL ODBC 5.51.30 Driver};Database=test;Server=192.168.1.109;UID=Username;PWD=Password;"))
{
connection.Open();
using(OdbcCommand command = new OdbcCommand("SELECT * FROM tablename", connection))
using(OdbcDataReader dr = command.ExecuteReader())
{
while(dr.Read())
Response.Write(dr["name"].ToString() + "<br>");
dr.Close();
}
connection.Close();
}
}
catch(Exception ex)
{
Response.Write("An error occurred: " + ex.Message);
}
}
</script>
</body>
</html>
I am currently getting this error when I run the code:
An error occurred: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I am hosting this on a Microsoft IIS server, with ASP.net enabled. Would I be able to get any help with fixing this error?
Link to image of my MySQL server: http://i.stack.imgur.com/4NYCr.png
Would I be able to get any help with fixing this error?
Yes, you would. So, your only question was answered. And now? Please think a little more about WHAT you ask.
TO your problem.
Like always, http://www.connectionstrings.com/ is the primary resource for this ;) Anything you ever wanted to know about connectionstrings on one place.
For MySQL (asuming 5.2) you can find (for ODBC) the solution at:
http://www.connectionstrings.com/mysql-connector-odbc-5-2/
Now, your string looks good - so I start assuming you have a driver problem. I would assume that wherever you run it it just does not have - the ODBC ddriver installed.
THAT SAID: there is no real reason to use ODBC (which has native prerequisites) and not a (managed) MySQL Driver?
if you really miss the ODBC driver, you can ind the downloads at http://www.mysql.com/products/connector/. But I would really not use ODBC here - not with a native connector available.
I think you better use MySQlConnection as well as MyqlCommand for your needs:
eg:
server = "localhost";
database = "connectcsharptomysql";
uid = "username";
password = "password";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);

Can't connect C# application to Oracle 11g

I have 2 stumbling problem:
1) Which is the correct port number?
This is the link when I run my Oracle 11g:
http://127.0.0.1:8080/apex/f?p=4950:1:786646867589622
I'm confused as to my port number is 1521, 8080 or 4950?
2) ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
This is my connection string:
string oradb = "Data Source=(DESCRIPTION="
+ "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + server + ")(PORT=" + port + ")))"
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));"
+ "User Id=" + uid + ";Password=" + password + ";";
And my connection parameters are:
DB obj2 = new Oracle("localhost", "1521", "system", "dada050909");
Am unable to connect and was displayed the error 12514.
Please help - I'm not very familiar with C# and Oracle. Have been stuck for quite some time.

Categories

Resources