Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从visual studio连接到sql数据库时出错_C#_Mysql_Sql_Visual Studio - Fatal编程技术网

C# 从visual studio连接到sql数据库时出错

C# 从visual studio连接到sql数据库时出错,c#,mysql,sql,visual-studio,C#,Mysql,Sql,Visual Studio,我是用c#编程visual studio的新手。我想将我的数据从VisualStudio连接到mysql数据库。我使用XAMPP作为服务器,但当我尝试连接时,它会给出错误消息:“连接必须有效且打开”。我不知道怎么修理它。我尝试了一些方法来解决这个问题,但仍然没有解决 using System; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace scadaSql { public partial cla

我是用c#编程visual studio的新手。我想将我的数据从VisualStudio连接到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()。

稍微绕道……在方法sqlOpen()中打开连接时是否出现异常?还是将label4.Text设置为已连接?同时尝试以大写字母开始方法名。处理结束的最简单方法是实现IDisposable并使用块调用
内的代码。