I'm writing my first Windows 10 Universal App that operates on MySql database. I used code from this guide (It's for Windows 8 store apps):
https://blogs.oracle.com/MySqlOnWindows/entry/how_to_using_connector_net
But when I try to open connection with my database I get error:
An exception of type 'System.NotImplementedException' occurred in >MySql.Data.RT.dll but was not handled in user code
Additional information: SSL not supported in this WinRT release.
public class DBconnector
{
static string server = "127.0.0.1";
static string database = "hurtownia";
static string user = "root";
static string pswd = "root";
public static bool login(string email, string password)
{
string connectionString = "Server = " + server + ";database = " + database + ";uid = " + user + ";password = " + pswd + ";";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
MySqlCommand checkLogin = new MySqlCommand("select password_hash, password_salt from users where email = \""+email+"\"",connection);
using (MySqlDataReader reader = checkLogin.ExecuteReader())
{
reader.Read();
string hash = reader.GetString("password_hash");
string salt = reader.GetString("password_salt");
bool result = passwordGenerator.compare(password, hash, salt);
if (result)
return true;
else
return false;
}
}
}
}
So, my question is how to fix that and correctly connect to MySql database in Windows 10 Universal App.
Add ";SslMode=None" to your connection string
I'm afarid the SSL connection is not supported by MySql WinRT connector. You have to disable the SSL connection from the MySql server.
Chapter 8 Connector/Net Support for Windows Store
Connector/Net RT does not support SSL connections or Windows
authentication. Also, SHA256 is not currectly supported.
6.3.6.4 SSL Command Options
BTW, another alternative way to retrieve the data from mysql is host a REST service:
App -> Rest Service -> MySQL
Related
I wanted to insert some data to the mysql database using that ID that I have retrieved from datagridview. i am new in programming. can someone please help me? thanks
First you need to download and intall
MySQL ADO.Net connector
it's the official ado.net connector for C# applications. once you intall that you can use ado.net standered data access methods to save data.
basic steps
First create a connection to the my sql data base.
Then create a command object contains the insert command.
then provide the object that contain the so called ID and finalize the command object
then Execute the command against the database.
Sample code
This is the class variables that will be used later
private MySqlConnection connection; // this MySqlConnection class comes with the connector
private string server;
private string database;
private string uid;
private string password;
This will Initialize method will configure the connection with the configuration data
private void Initialize()
{
server = "localhost";
database = "connectcsharptomysql";
uid = "username";
password = "password";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
this method will open a connection to the database . you should write a C Lose method as well .because it's best practice to always close the connection after you used it
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
This will insert a record to database . query will contains the T-SQL query that runs against the database
public void Insert()
{
string query = "INSERT INTO tableinfo (name, age) VALUES('John Smith', '33')";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
Hope this will help
I am trying to open connection with Azure SQL database. Tried creating usual connection = new MySqlConnection("Server=" + server + "Database=" + database + "Uid=" + uid + "Password=" + password); with every string variable ending with ; but yet it always fails to connect even if the data is correct. Tried to use given string for ADO.NET but then I am getting exception "keyword is not supported". I don't what else to actually.. Googled as much as possible but all solutions are quite the same and yet nothing works out for me :/
Firstly, azure databases don't use mysql. so using MySqlConnection() won't work.
instead use
SqlConnection connection = new SqlConnection(connectionstring);
Standard connection strings should be in the format
Server=tcp:[serverName].database.windows.net;Database=myDataBase;
User ID=[LoginForDb]#[serverName];Password=myPassword;Trusted_Connection=False;
Encrypt=True;
See https://www.connectionstrings.com/azure-sql-database/ for more options
var connectionString = #"Server=tcp:<dbname>.database.windows.net,1433;Initial Catalog=<databasename>;Persist Security Info=False;User ID=<userid>;Password=<password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
That's how I connect to my Azure SQL Database, works for me.
For MySQL In App you can use the fellowing code in c# to get the connection string:
static string getConnectionString()
{
#if DEBUG
string connectionString = "dbname=localdb;host=localhost:3306;user=root;pass=password;";
#else
string connectionString = Environment.GetEnvironmentVariable("MYSQLCONNSTR_localdb");
#endif
string[] options = connectionString.Split(";");
string database = options[0].Split("=")[1]; ;
string serverport = options[1].Split("=")[1];
string server = serverport.Split(":")[0];
string port = serverport.Split(":")[1];
string user = options[2].Split("=")[1];
string password = options[3].Split("=")[1]; ;
connectionString = $"server={server};port={port};database={database};user={user};password={password};";
return connectionString;
}
The standard .Net Framework provider format is:
Server=[serverName].database.windows.net;Database=myDataBase;
User ID=[LoginForDb]#[serverName];Password=myPassword;Trusted_Connection=False;
Encrypt=True;
Azure SQL Database is an SQL Server type database, not MySQL!
Have you followed Microsoft instructions?
https://learn.microsoft.com/en-us/azure/sql-database/sql-database-connect-query-dotnet-visual-studio
You have to create server-level firewall rule on azure too, to be able to connect.
I want to create a simple tool that connect to SQL Server then list all databases, and after user selects one of those databases, the tool will perform some SQL scripts on it.
Below is a method that should return a DataTable contains all my databases;
I send to it sqlServerIp as first parameter like 10.10.20.30 ip address not server name.
Also I didn't specify a SQL Server instance.
private DataTable GetSqlServerDatabases(String sqlServer, bool isWindowsAuth, String login, String password)
{
String connectionString;
DataTable databases;
if (isWindowsAuth)
{
connectionString = "Data Source=" + sqlServer + ",1433;Network Library=DBMSSOCN; Integrated Security=True;";
}
else
{
connectionString = "Data Source=" + sqlServer + ",1433;Network Library=DBMSSOCN;" + "User id=" + login + ";Password=" + password + ";";
}
try
{
using (var con = new SqlConnection(connectionString))
{
con.Open();
databases = con.GetSchema("Databases");
foreach (DataRow database in databases.Rows)
{
String databaseName = database.Field<String>("database_name");
short dbID = database.Field<short>("dbid");
DateTime creationDate = database.Field<DateTime>("create_date");
}
}
}
catch(Exception ex)
{
databases = null;
MessageBox.Show(ex.Message.ToString());
}
return databases;
}
When I try to call the method on more than one server I faced an exception:
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
or
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.
I used connection string with Network Library=DBMSSOCN; after searching here on SO and web so I don't know what is the wrong with this method.
Remove Integrated Security parameter. This parameter is for trusted connections.
More samples on http://www.connectionstrings.com/sql-server
I have a WPF application that accesses a SQL server using SqlConnection with a connection string.
private string connString =
"user id=*****;" +
"password=*****;" +
"server=1.1.1.1;" +
"Trusted_Connection = yes;" +
"database=****;" +
"connection timeout = 30";
private SqlConnection myConnection = new SqlConnection(connString);
The string contains the username and password for the account I want the program to use to access the database. This works as I would expect it to on my machine. Once I run it on another machine I get a an error:
"Login failed for user 'Domain\local user'"
I am sure that I am just missing some sort of setting or configuration.
Any help would be great
Trusted_Connection = yes tells SQL Server to use Integrated (Windows) Authentication. Your user name and password are ignored.
I Am Trying to create a program using c# that needs to connect to a database running on a Solaris server, I am not too familiar with the server, we normally use dbVisualizer to connect to it. the driver it uses to connect is mysql-connector-java-5.1.10, which is a jdbc driver. was wondering what drivers to use to connect to the database using C# and what is the syntax used to establish the connection. as far as I know I will be unable to install any drivers on the server side, and i will only be able to make changes/Install what is required on the client.
If I read your question correctly you are trying to connect to a MySql database from c#. This can be achieved by downloading the .net connector for MySql - Connector/Net. When you install this driver it will "integrate" with Visual Studio and you will be able to connect to the server directly from Visual Studio and your Program that will use the driver.
On the question on the syntax to connect you will either need to use MySqlConnection, with a tutorial here - http://bitdaddys.com/MySQL-ConnectorNet.html, or use something like the ADO.NET Entity Framework. But that depends on your Tastes.
I am assuming this Server can be access over the network.
Update User Confused about Connection String
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
You pass that string to the connection without any JDBC:// prefixes.
Please Note haven't done this in a while so the connection string could be wrong (So correct me If I'm wrong) and if you forget any connection string in the future you can always use a website like http://www.connectionstrings.com/ which shows them all for you. That is where I got the string above.
Hope that helps.
I believe this is what you want to connect (on the server):
http://dev.mysql.com/downloads/connector/net/1.0.html
You can try your connection like this:
string MyConString = "SERVER=yourserver;" +
"DATABASE=mydatabase;" +
"UID=testuser;" +
"PASSWORD=testpassword;";
MySqlConnection connection = new MySqlConnection(MyConString);
You would probably want to follow the normal guidelines for IDisposable classes (use using etc.).
using MySql.Data.MySqlClient;
using System.Windows;
class Connexion
{
public MySql.Data.MySqlClient.MySqlConnection connexion;
private string server;
private string database;
private string uid;
private string password;
public Connexion()
{
server = "localhost";
database = "GestionCommeriale";
uid = "root";
password = "";
String connexionString;
connexionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" +
"UID" + uid + ";" + "PASSSWORD =" + password + ";";
connexion = new MySqlConnection(connexionString);
}
public bool OpenConnexion()
{
try
{
connexion.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
public bool ColseConnexion()
{
try
{
connexion.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}
}