簡體   English   中英

SqlConnection.Open 拋出異常 C#

[英]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();
        }
    }
}

這是錯誤的屏幕截圖,

錯誤 1

錯誤 2

對於 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM