C#连接在线数据库

C# connecting with online database

因此,对于一个学校项目,我正在开发一个 C# 应用程序,该应用程序可以将信息放入数据库中,以便我们可以将其输出到网站中.. 我想知道是否可以为此使用在线数据库,我只是使用了这个网站:http://www.freesqldatabase.com/

Error: "Reading from the stream has failed" on adapter.fill(table)

这是 MySqlConnection 字符串:

MySqlConnection connection = new MySqlConnection("datasource=sql11.freesqldatabase.com;port=3306;Initial Catalog='sql11174958';username=Username;password=Password");
            MySqlCommand command;

这是我使用的 C# 代码:

using MySql.Data.MySqlClient;
using MySql.Data;
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;

namespace Database
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        MySqlConnection connection = new MySqlConnection("datasource=sql11.freesqldatabase.com;port=3306;Initial Catalog='sql11174958';username=Username;password=Password");
        MySqlCommand command;

        private void Form1_Load(object sender, EventArgs e)
        {
            populateDGV();
        }

        public void populateDGV()
        {
            // populate the datagridview
            string selectQuery = "SELECT * FROM park";
            DataTable table = new DataTable();
            MySqlDataAdapter adapter = new MySqlDataAdapter(selectQuery, connection);
            adapter.Fill(table);
            dataGridView_USERS.DataSource = table;
        }

        private void dataGridView_USERS_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                txbParkID.Text = dataGridView_USERS.CurrentRow.Cells[0].Value.ToString();
                txbParkNaam.Text = dataGridView_USERS.CurrentRow.Cells[1].Value.ToString();
                txbLocatie.Text = dataGridView_USERS.CurrentRow.Cells[2].Value.ToString();
                txbOpeningsDagen.Text = dataGridView_USERS.CurrentRow.Cells[3].Value.ToString();
                txbPrijzen.Text = dataGridView_USERS.CurrentRow.Cells[4].Value.ToString();
                txbLeeftijden.Text = dataGridView_USERS.CurrentRow.Cells[5].Value.ToString();
                txbTags.Text = dataGridView_USERS.CurrentRow.Cells[6].Value.ToString();
                rtbBeschrijving.Text = dataGridView_USERS.CurrentRow.Cells[7].Value.ToString();
            }
            catch
            {
                MessageBox.Show("No cell selected.");
            }

        }

        public void openConnection()
        {
            if(connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }
        }

        public void closeConnection()
        {
            if(connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }

        public void executeMyQuery(string query)
        {
            try
            {
                openConnection();
                command = new MySqlCommand(query,connection);

                if(command.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Query Executed");
                }

                else
                {
                    MessageBox.Show("Query Not Executed");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }finally
            {
                closeConnection();
            }
        }

        private void BTN_INSERT_Click(object sender, EventArgs e)
        {
            string insertQuery = "INSERT INTO park(ParkNaam, ParkLocatie, ParkOpeningsDagen, ParkOpeningsTijden, ParkPrijzen, ParkLeeftijden, ParkTags, ParkBeschrijving) VALUES('" +txbParkNaam.Text+ "','" +txbLocatie.Text+ "','" +txbOpeningsDagen.Text + "','" +txbOpeningsTijden.Text+ "','" +txbPrijzen.Text+ "','" +txbLeeftijden.Text+ "','" +txbTags.Text+ "','" +rtbBeschrijving.Text+ "')";
            executeMyQuery(insertQuery);
            populateDGV();
        }

        private void BTN_UPDATE_Click(object sender, EventArgs e)
        {
            string updateQuery = "UPDATE park SET ParkNaam='" +txbParkNaam.Text+ "',ParkLocatie='" +txbLocatie.Text+ "',ParkOpeningsDagen='" +txbOpeningsDagen.Text+ "',ParkOpeningsTijden='" +txbOpeningsTijden.Text+ "',ParkPrijzen='" +txbPrijzen.Text+ "',Parkleeftijden='" +txbLeeftijden.Text+ "',ParkTags='" +txbTags.Text+ "',ParkBeschrijving='" +rtbBeschrijving.Text+ "' WHERE ParkID =" + int.Parse(txbParkID.Text);
            executeMyQuery(updateQuery);
            populateDGV();
        }

        private void BTN_DELETE_Click(object sender, EventArgs e)
        {
            string deleteQuery = "DELETE FROM park WHERE ParkID = " + int.Parse(txbParkID.Text);
            executeMyQuery(deleteQuery);
            populateDGV();
        } 
    }
}

我遇到了问题,您正在打开连接,但我看不到您在哪里关闭它们。打开与 MySQL 的连接后,您应该在使用后关闭它们。

在这种情况下,远程服务器中的连接是打开的而不是关闭的。就像远程服务器是免费的一样,允许的连接数是有限的。您应该联系 MySQL 服务提供商关闭打开的连接,这样只会消除错误,请在使用后关闭连接,否则问题会再次出现。