我是C#编程的新手,但希望能够访问MySQL数据库(没有用于MS SQL的资金)
但是我有一个问题。我知道您必须具有“ MySQL连接器/ NET”和“ MySQL for Visual Studio”才能开发C#应用程序,但是安装该应用程序的人还需要它们吗?(是否可以随程序释放连接器DLL?)
谢谢
编辑:最终用户还是只是连接器都需要它们?他们还有什么需要的吗?
我是C#编程的新手,但希望能够访问MySQL数据库(没有用于MS SQL的资金)
但是我有一个问题。我知道您必须具有“ MySQL连接器/ NET”和“ MySQL for Visual Studio”才能开发C#应用程序,但是安装该应用程序的人还需要它们吗?(是否可以随程序释放连接器DLL?)
谢谢
编辑:最终用户还是只是连接器都需要它们?他们还有什么需要的吗?
Answers:
安装Oracle的MySql.Data
NuGet软件包。
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();
}
}
}
例:
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();
}
if (_instance == null)
行。bool result = false
默认情况下也要进行设置,因为它可能因Internet连接问题而可能没有连接,而实际上还有许多其他问题,不仅是因为不会设置数据库名称。在这种情况下,if (String.IsNullOrEmpty(databaseName)) return false;
因为它只会引发异常,甚至可能返回true,这是不正确的。但是,感谢您提供干净的代码。
new DBConnection();
and private DBConnection()
)
connection = null
的DBConnection.Close()
,因为它是独立的,并可能要再次使用它
public bool IsConnect()
。要重用连接,请在以下if
语句之后:else if (connection.State == System.Data.ConnectionState.Closed) { connection.Open(); }
在您的应用程序中dbCon.IsConnect()
,在上一个查询之后将其关闭后,您可以再次执行该操作。所以千万不要connection = null;
在DBConnection.Close()
为罗马的建议,否则再利用将无法正常工作。
您可以使用“程序包管理器”将其添加为程序包,这是最简单的方法。您不需要其他任何东西即可使用mysql数据库。
或者您可以在Package Manager控制台中运行以下命令
PM> Install-Package MySql.Data
您必须从此处下载MySQLConnection NET 。
然后,您需要MySql.Data.DLL
像这样添加到MSVisualStudio:
C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.12\Assemblies\v4.5.2
如果您想了解更多信息,请访问:在此处输入链接描述
要在代码中使用,必须导入库:
using MySql.Data.MySqlClient;
通过Click事件连接到Mysql数据库(无SSL模式)的示例:
using System;
using System.Windows;
using MySql.Data.MySqlClient;
namespace Deportes_WPF
{
public partial class Login : Window
{
private MySqlConnection connection;
private string server;
private string database;
private string user;
private string password;
private string port;
private string connectionString;
private string sslM;
public Login()
{
InitializeComponent();
server = "server_name";
database = "database_name";
user = "user_id";
password = "password";
port = "3306";
sslM = "none";
connectionString = String.Format("server={0};port={1};user id={2}; password={3}; database={4}; SslMode={5}", server, port, user, password, database, sslM);
connection = new MySqlConnection(connectionString);
}
private void conexion()
{
try
{
connection.Open();
MessageBox.Show("successful connection");
connection.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message + connectionString);
}
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
conexion();
}
}
}
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);
}