簡體   English   中英

錯誤從Visual Studio連接到SQL數據庫

[英]error connect to sql database from visual studio

我是使用C#編程Visual Studio的新手。 我想將數據從Visual Studio連接到mysql數據庫。 我使用XAMPP作為服務器,但是當我嘗試連接時,它會顯示錯誤消息:“連接必須有效且打開”。 我不知道該如何解決。 我嘗試了一些方法來解決此問題,但仍然沒有解決。

using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace scadaSql
{
    public partial class Form1 : Form
    {
        MySqlConnection connectionMySql;

        bool statusServer = false;
        string server = "localhost";
        string database = "topsus_iot";
        string uid = "root";
        string pass = "";
        string conString;
        int checkCounter = 0;
        int timeCounter = 0;
        int filesCnt = 0;
        int loopProg = 0;

        public void sqlInsert(int temp, int humidity)
        {
            try
            {
                MySqlCommand cmd = connectionMySql.CreateCommand();
                cmd.CommandText = "INSERT INTO data (device_id, temperature, humidity) VALUE(2, @temp, @humidity)";
                cmd.Parameters.AddWithValue("@temp", temp);
                cmd.Parameters.AddWithValue("@humidity", humidity);
                cmd.ExecuteNonQuery();
            }
            catch (Exception X)
            {
                MessageBox.Show(X.Message);
                throw;
            }
        }

        public bool sqlClose()
        {
            try
            {
                connectionMySql.Close();
                label4.Text = "disconnected";
                label4.ForeColor = System.Drawing.Color.Lime;
                return true;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public bool sqlOpen()
        {
            try
            {
                connectionMySql.Open();
                label4.Text = "Connected";
                label4.ForeColor = System.Drawing.Color.Lime;
                return true;
            }
            catch (MySqlException mx)
            {
                switch (mx.Number)
                {
                    case 0:
                        break;
                    case 1045:
                        break;
                }
                return false;
            }
        }
        public Form1()
        {
            InitializeComponent();
            conString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + pass + ";";
            connectionMySql = new MySqlConnection(conString);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            statusServer = false;
            sqlOpen();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            sqlInsert((int)temperature.Value, (int)humidity.Value);
        }
    }
}

創建sql命令之前,需要打開連接。 似乎您從未調用過SQLOpen方法,因此從未初始化和打開連接,因此您的insert語句失敗。

我建議為您的類創建一個構造函數,並在其上調用open命令。

另外,完成命令后,不要忘記調用sqlClose()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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