Attempting to make a login form in C#
, connecting to a SQL server
. But It keeps throwing exceptions at the cn.Open();
. I am using XAMPP
for my SQL Server
.
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;
using System.Data.Sql;
using System.Data.SqlClient;
namespace BlackLight_CSharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("server=localhost;user id=root;database=blacklight_login");
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM login WHERE Username= '"+txt_user.Text+"' AND Password = '"+txt_pass.Text+"'", cn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
int count = 0;
while (dr.Read())
{
count += 1;
}
if (count == 1)
{
MessageBox.Show("OK");
dashboard dash = new dashboard();
dash.Show();
}
else if (count > 0)
{
MessageBox.Show("Duplicate Username and Password");
}
else
{
MessageBox.Show("Incorrect Username or Password, Try again.");
}
txt_user.Clear();
txt_pass.Clear();
}
}
}
Here are screen shots of the errors,
For MySql server you need MySql.Data.MySqlClient.MySqlConnection instead of SqlConnection. Check: https://www.codeproject.com/Articles/43438/Connect-C-to-MySQL
很明显您正在使用MySQL
并且您正在使用支持MSSQL
SqlClient
将您的数据库数据提供程序更改为MySQL并使用MySql.Data.MySqlClient.MySqlConnection
代替。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.