Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.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# 为什么我的更新sql命令不起作用?_C#_Asp.net_Mysql_Sql_Database - Fatal编程技术网

C# 为什么我的更新sql命令不起作用?

C# 为什么我的更新sql命令不起作用?,c#,asp.net,mysql,sql,database,C#,Asp.net,Mysql,Sql,Database,我在mysql中使用sql命令时遇到问题。我有一个asp.net应用程序,使用mysql数据库作为数据库。我想建立一个类,可以插入,更新,删除和选择数据库中的记录。更新命令有问题。在phpmyadmin中它有效,但在我的类中不起作用:/ 这是我的密码 public void UpdateRecord(string title, string firstname, string lastname, string company, str

我在mysql中使用sql命令时遇到问题。我有一个asp.net应用程序,使用mysql数据库作为数据库。我想建立一个类,可以插入,更新,删除和选择数据库中的记录。更新命令有问题。在phpmyadmin中它有效,但在我的类中不起作用:/

这是我的密码

public void UpdateRecord(string title, string firstname, string lastname, string company,
                               string timefrom, string timeto)
        {
            connection = new MySqlConnection();

            try
            {
                MySqlCommand command = connection.CreateCommand();

                //for testing
                string sql = "UPDATE 'tb_gast' SET FIRSTNAME='test' WHERE ID=270";

                command.CommandText = sql;

                connection.Open();

                command.ExecuteNonQuery();

            }
            catch (Exception)
            {

            }
            finally
            {
                connection.Close();
            }
        }
此命令在phpmyadmin中工作

UPDATE `tb_gast` 
SET 
FIRSTNAME='uu',
LASTNAME='uu',
COMPANY='uu'
WHERE ID=270

从表名“tb_gast”中删除引号。而不是:

UPDATE 'tb_gast' SET FIRSTNAME='test' WHERE ID=270
试试这个:

UPDATE tb_gast SET FIRSTNAME = 'test' WHERE ID=270

您在表名周围使用单引号,而不是反勾号。 可以使用ALT+096组合键键入反勾号

编辑:更深入地审视你的问题。 注意,您声明了一个MySqlConnection,并在没有任何连接字符串的情况下初始化它。 如果这是你真正的代码,难怪它不起作用

这就是你的代码应该是什么样子

        using(connection = new MySqlConnection(GetConnectionString())
        {
            connection.Open();
            using(MySqlCommand command = connection.CreateCommand();
            {
                //for testing
                string sql = "UPDATE `tb_gast` SET FIRSTNAME='test' WHERE ID=270";
                command.CommandText = sql;
                command.ExecuteNonQuery();
            }
        }
其中GetConnectionString应该是一种方法,它可以从一些永久存储(如配置文件)返回连接字符串

void UpdateRecordstring title、string firstname、string lastname、string company、, 字符串timefrom,字符串timeto { 连接=新的MySqlConnection

        try
        {
           string sql = "UPDATE tb_gast SET FIRSTNAME='test' WHERE ID=270";
           MySqlCommand command = new MySqlCommand(sql, connection);
           connection.Open();

            command.ExecuteNonQuery();

        }
        catch (Exception)
        {

        }
        finally
        {
            connection.Close();
        }
    }

我不确定MySQL,但尝试删除表名周围的单引号,例如更新tb_gast SET…如果删除空的CatchException{},所有It't not work注释的解决速度会快得多阻止并让错误真正得到报告。然后,当你解决了问题后……不要再添加它。因为,下次出现问题时,你还需要知道实际错误是什么,而不是让此函数以静默方式失败。在catch block Exception ex->ex中添加MessageBox。Message//打印此消息并告诉你是什么error@Sunny-这种方法的问题是,对于未知异常,您可能仍然没有提供足够的信息,例如,您正在丢弃内部异常和堆栈跟踪。如果您不知道实际错误是什么,最好让异常逃逸并导致一个充满丰富信息的大而丑陋的崩溃。对于o:…我不明白问题出在哪里语法必须正确:/I没有收到错误…但我在数据库中看到不起作用:/
        try
        {
           string sql = "UPDATE tb_gast SET FIRSTNAME='test' WHERE ID=270";
           MySqlCommand command = new MySqlCommand(sql, connection);
           connection.Open();

            command.ExecuteNonQuery();

        }
        catch (Exception)
        {

        }
        finally
        {
            connection.Close();
        }
    }