使用 windows 表单将 C# 连接到 mySQL 数据库
Connect C# to mySQL database using windows form
我是 C# 的新手,正在尝试将 windows 表单连接到 mySQL 数据库。
问题是我收到以下错误,但找不到解决此问题的方法。
Error CS0120 An object reference is required for the non-static field, method, or property 'Login.con()
这是Form.cs
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Log
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void BtnConnect_Click(object sender, EventArgs e)
{
Login.con();
}
}
}
这是应该建立连接的 Login.cs 文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using MySql.Data;
using System.Data;
namespace Log
{
public class Login
{
public 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()
{
server = "localhost";
database = "test";
user = "root";
password = "root";
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);
}
public void con()
{
try
{
connection.Open();
MessageBox.Show("successful connection");
connection.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message + connectionString);
}
}
}
}
您需要实例登录class...
Login login = new Login();
login.con()
您必须创建一个新的 class 登录实例。试一试:
public void BtnConnect_Click(object sender, EventArgs e)
{
Login login = new Login();
login.con();
}
我是 C# 的新手,正在尝试将 windows 表单连接到 mySQL 数据库。 问题是我收到以下错误,但找不到解决此问题的方法。
Error CS0120 An object reference is required for the non-static field, method, or property 'Login.con()
这是Form.cs
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Log
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void BtnConnect_Click(object sender, EventArgs e)
{
Login.con();
}
}
}
这是应该建立连接的 Login.cs 文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using MySql.Data;
using System.Data;
namespace Log
{
public class Login
{
public 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()
{
server = "localhost";
database = "test";
user = "root";
password = "root";
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);
}
public void con()
{
try
{
connection.Open();
MessageBox.Show("successful connection");
connection.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message + connectionString);
}
}
}
}
您需要实例登录class...
Login login = new Login();
login.con()
您必须创建一个新的 class 登录实例。试一试:
public void BtnConnect_Click(object sender, EventArgs e)
{
Login login = new Login();
login.con();
}