Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.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#不工作_C#_Mysql_Odbc - Fatal编程技术网

数据库查询C#不工作

数据库查询C#不工作,c#,mysql,odbc,C#,Mysql,Odbc,我已经在网上阅读了很多关于C#的ODBC教程,而这段代码是唯一没有告诉我错误的代码。但问题是,它没有任何作用-如何修复 using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Data.Odbc; using System.Data.Sql; namespace WindowsFormsApplication1 { static

我已经在网上阅读了很多关于C#的ODBC教程,而这段代码是唯一没有告诉我错误的代码。但问题是,它没有任何作用-如何修复

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;User       ID=root;Password=****;Database=testing;Port=3306;Pooling=false";
        MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);

        connection.Open();

        string insertQuery = "ALTER TABLE `user` ADD lol INT (15)";
        MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(insertQuery);
        connection.Close();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
使用System.Data.Odbc;
使用System.Data.Sql;
命名空间Windows窗体应用程序1
{
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
string connectionString=“服务器=本地主机;用户ID=根;密码=***;数据库=测试;端口=3306;池=假”;
MySql.Data.MySqlClient.MySqlConnection connection=new MySql.Data.MySqlClient.MySqlConnection(connectionString);
connection.Open();
string insertQuery=“ALTER TABLE`user`ADD lol INT(15)”;
MySql.Data.MySqlClient.MySqlCommand myCommand=new MySql.Data.MySqlClient.MySqlCommand(insertQuery);
connection.Close();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
}

您需要执行以下命令:

myCommand.ExecuteNonQuery();

请允许我整理一下;要点:

  • 使用
    确保正确处置
  • 设置命令的
    连接
  • 使用
    ExecuteNonQuery()
代码:


我建议在使用数据库连接之类的东西时,查看
使用语句
try/catch/finally
。@Frederic-这不是很有帮助。他想说的是你可以使用VARCHAR(15)或INT,而不是INT(15)。我不认为这是你的问题,因为那会引发异常。问题一定在那之前。您是否尝试过在调试模式下单步执行代码。@Ash,您可能是对的。我猜今天有太多奇怪的问题。另外,使用
root
进行DB事务也是一个很大的禁忌。这里问了同样的问题:为什么您希望它运行多次?“连接必须有效且打开”它说现在您需要将连接传递到命令对象。通过构造函数或属性。
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string connectionString = "Server=localhost;User ID=root;Password=****;Database=testing;Port=3306;Pooling=false";
string insertQuery = "ALTER TABLE `user` ADD lol INT (15)";
using(MySql.Data.MySqlClient.MySqlConnection connection =
    new MySql.Data.MySqlClient.MySqlConnection(connectionString))
using(MySql.Data.MySqlClient.MySqlCommand myCommand =
    new MySql.Data.MySqlClient.MySqlCommand(insertQuery))
{
    myCommand.Connection = connection;
    connection.Open();
    myCommand.ExecuteNonQuery();
    connection.Close();
}
using(Form1 form1 = new Form1()) {
    Application.Run(form1);
}