[英]SqlConnection.Open throwing exception C#
尝试在C#
创建登录表单,连接到SQL server
。 但它不断在cn.Open();
抛出异常cn.Open();
. 我将XAMPP
用于我的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();
}
}
}
这是错误的屏幕截图,
对于 MySql 服务器,您需要 MySql.Data.MySqlClient.MySqlConnection 而不是 SqlConnection。 检查: https : //www.codeproject.com/Articles/43438/Connect-C-to-MySQL
很明显您正在使用MySQL
并且您正在使用支持MSSQL
SqlClient
将您的数据库数据提供程序更改为MySQL并使用MySql.Data.MySqlClient.MySqlConnection
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.