I'm very new to C# programming but would like to be able to access MySQL Databases (do not have the money for MS SQL)
我对c#编程非常陌生,但是我希望能够访问MySQL数据库(没有MS SQL的资金)
However I have one question; I know you are required to have "MySQL connector/NET" and "MySQL for Visual Studio" for developing the C# application, but does the person who installs the application also require them? (Is it possible I can just release the connector DLL with the program?)
但是我有一个问题;我知道开发c#应用程序需要“MySQL connector/NET”和“MySQL for Visual Studio”,但是安装应用程序的人也需要它们吗?(我是否可以在程序中释放连接器DLL ?)
Thanks
谢谢
EDIT: Are both of them required for the end-user or just the connector? Is there anything else they would need?
编辑:终端用户和连接器都需要吗?他们还需要什么吗?
4 个解决方案
#1
45
using MySql.Data;
using MySql.Data.MySqlClient;
namespace Data
{
public class DBConnection
{
private DBConnection()
{
}
private string databaseName = string.Empty;
public string DatabaseName
{
get { return databaseName; }
set { databaseName = value; }
}
public string Password { get; set; }
private MySqlConnection connection = null;
public MySqlConnection Connection
{
get { return connection; }
}
private static DBConnection _instance = null;
public static DBConnection Instance()
{
if (_instance == null)
_instance = new DBConnection();
return _instance;
}
public bool IsConnect()
{
if (Connection == null)
{
if (String.IsNullOrEmpty(databaseName))
return false;
string connstring = string.Format("Server=localhost; database={0}; UID=UserName; password=your password", databaseName);
connection = new MySqlConnection(connstring);
connection.Open();
}
return true;
}
public void Close()
{
connection.Close();
}
}
}
Example:
例子:
var dbCon = DBConnection.Instance();
dbCon.DatabaseName = "YourDatabase";
if (dbCon.IsConnect())
{
//suppose col0 and col1 are defined as VARCHAR in the DB
string query = "SELECT col0,col1 FROM YourTable";
var cmd = new MySqlCommand(query, dbCon.Connection);
var reader = cmd.ExecuteReader();
while(reader.Read())
{
string someStringFromColumnZero = reader.GetString(0);
string someStringFromColumnOne = reader.GetString(1);
Console.WriteLine(someStringFromColumnZero + "," + someStringFromColumnOne);
}
dbCon.Close();
}
#2
36
you can use Package Manager to add it as package and it is the easiest way to do. You don't need anything else to work with mysql database.
您可以使用包管理器将其添加为包,这是最简单的方法。使用mysql数据库不需要任何其他东西。
Or you can run below command in Package Manager Console
或者可以在包管理器控制台的命令下面运行
PM> Install-Package MySql.Data
NUGET Mysql.Data
#3
2
Looking at the code below, I tried it and found: Instead of writing DBCon = DBConnection.Instance();
you should put DBConnection DBCon - new DBConnection();
(That worked for me)
查看下面的代码,我尝试了一下,发现:不是编写DBCon = dbconnector . instance ();您应该放置DBConnection DBCon - new DBConnection();(为我工作)
and instead of MySqlComman cmd = new MySqlComman(query, DBCon.GetConnection());
you should put MySqlCommand cmd = new MySqlCommand(query, DBCon.GetConnection());
(it's missing the d)
而不是MySqlComman cmd = new MySqlComman(query, DBCon.GetConnection());应该将MySqlCommand cmd = new MySqlCommand(query, db . getconnection ());(这是错过了d)
#4
0
private void Initialize()
{
server = "localhost";
database = "connectcsharptomysql";
uid = "username";
password = "password";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "U`enter code here`ID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
#1
45
using MySql.Data;
using MySql.Data.MySqlClient;
namespace Data
{
public class DBConnection
{
private DBConnection()
{
}
private string databaseName = string.Empty;
public string DatabaseName
{
get { return databaseName; }
set { databaseName = value; }
}
public string Password { get; set; }
private MySqlConnection connection = null;
public MySqlConnection Connection
{
get { return connection; }
}
private static DBConnection _instance = null;
public static DBConnection Instance()
{
if (_instance == null)
_instance = new DBConnection();
return _instance;
}
public bool IsConnect()
{
if (Connection == null)
{
if (String.IsNullOrEmpty(databaseName))
return false;
string connstring = string.Format("Server=localhost; database={0}; UID=UserName; password=your password", databaseName);
connection = new MySqlConnection(connstring);
connection.Open();
}
return true;
}
public void Close()
{
connection.Close();
}
}
}
Example:
例子:
var dbCon = DBConnection.Instance();
dbCon.DatabaseName = "YourDatabase";
if (dbCon.IsConnect())
{
//suppose col0 and col1 are defined as VARCHAR in the DB
string query = "SELECT col0,col1 FROM YourTable";
var cmd = new MySqlCommand(query, dbCon.Connection);
var reader = cmd.ExecuteReader();
while(reader.Read())
{
string someStringFromColumnZero = reader.GetString(0);
string someStringFromColumnOne = reader.GetString(1);
Console.WriteLine(someStringFromColumnZero + "," + someStringFromColumnOne);
}
dbCon.Close();
}
#2
36
you can use Package Manager to add it as package and it is the easiest way to do. You don't need anything else to work with mysql database.
您可以使用包管理器将其添加为包,这是最简单的方法。使用mysql数据库不需要任何其他东西。
Or you can run below command in Package Manager Console
或者可以在包管理器控制台的命令下面运行
PM> Install-Package MySql.Data
NUGET Mysql.Data
#3
2
Looking at the code below, I tried it and found: Instead of writing DBCon = DBConnection.Instance();
you should put DBConnection DBCon - new DBConnection();
(That worked for me)
查看下面的代码,我尝试了一下,发现:不是编写DBCon = dbconnector . instance ();您应该放置DBConnection DBCon - new DBConnection();(为我工作)
and instead of MySqlComman cmd = new MySqlComman(query, DBCon.GetConnection());
you should put MySqlCommand cmd = new MySqlCommand(query, DBCon.GetConnection());
(it's missing the d)
而不是MySqlComman cmd = new MySqlComman(query, DBCon.GetConnection());应该将MySqlCommand cmd = new MySqlCommand(query, db . getconnection ());(这是错过了d)
#4
0
private void Initialize()
{
server = "localhost";
database = "connectcsharptomysql";
uid = "username";
password = "password";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "U`enter code here`ID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}