所以对于一个学校项目我正在研究一个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();
}
}
}
答案 0 :(得分:0)
我遇到了问题,你正在打开连接,但我不知道你在哪里关闭它们。打开与MySQL的连接后,应在使用后关闭它们。
在这种情况下,连接在远程服务器中打开而不是关闭。好像远程服务器是空闲的,允许的连接数量是有限的。您应该通过联系MySQL服务提供商来关闭打开的连接,然后只会删除错误,请在使用后关闭连接,否则会再次出现问题。