连接必须有效且打开错误 [英] Connection must be valid and open error

查看:61
本文介绍了连接必须有效且打开错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误消息:启动此程序时,连接必须有效并打开.我在Google上搜索,仅发现2个人有相同的错误.有可能解决这个问题吗?谢谢

I'm getting error: Connection must be valid and open when I start this program. I searched on google and only found 2 people that have the same error. Is it possible to fix this ?? Thx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.Sql;
namespace WindowsFormsApplication1
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        string connectionString = "Server=localhost;Uid=root;Pwd=******;Database=testing;";
        MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);

        connection.Open();

        string insertQuery = "ALTER TABLE `user` ADD lol INT";
        MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(insertQuery);
        myCommand.ExecuteNonQuery();
        connection.Close();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
}

推荐答案

您需要告诉您的命令有关您的连接对象的信息. 将连接参数添加到此行:

You need to tell your command about your connection object. Add the connection parameter to this line:

MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(insertQuery, connection);

这篇关于连接必须有效且打开错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆