Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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# 将commandtext转换为已回答问题中的字符串问题_C#_Asp.net_Mysql - Fatal编程技术网

C# 将commandtext转换为已回答问题中的字符串问题

C# 将commandtext转换为已回答问题中的字符串问题,c#,asp.net,mysql,C#,Asp.net,Mysql,关于这篇文章的问题,我想他是离线的,所以我无法得到任何反馈: 我不确定我是否完全失明,但在以下代码中: // run the insert using a non query call command.CommandText = cmd.ToString(); command.ExecuteNonQuery(); /* now we want to make a second call to MYS

关于这篇文章的问题,我想他是离线的,所以我无法得到任何反馈: 我不确定我是否完全失明,但在以下代码中:

            // run the insert using a non query call
            command.CommandText = cmd.ToString();
            command.ExecuteNonQuery();

            /* now we want to make a second call to MYSQL to get the new index 
               value it created for the primary key.  This is called using scalar so it will
                return the value of the SQL  statement.  We convert that to an int for later use.*/
            command.CommandText = "select last_insert_id();";
            id = Convert.ToInt32(command.ExecuteScalar());

            //------- the name id does not exist in the current context

            // Commit the transaction.
            transaction.Commit();
        }
        catch (Exception ex)
        {
            Label10.Text = ": " + ex.Message;

            try
            {
                // Attempt to roll back the transaction.
                transaction.Rollback();
            }
            catch
            {
                // Do nothing here; transaction is not active.
            }
        }
    }
id没有设置为任何值,我不确定他是如何尝试将其设置为最后插入的id的

编辑:

int id = Convert.ToInt32(cmd.ExecuteScalar());
Label10.Text = Convert.ToString(id);

您应该能够使用
select@@IDENTITY无需使用第二个查询:

 OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email) VALUES ('rusty@msn.com'); select @@IDENTITY;"
 int id = Convert.ToInt32(cmd.ExecuteScalar());
在回顾您的旧问题后,这应该是一样的:

 OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email) VALUES ('rusty@msn.com'); SELECT LAST_INSERT_ID();"
 int id = Convert.ToInt32(cmd.ExecuteScalar());

天哪,我试过了,但是使用了String.Format duhh,但问题是现在返回的是什么,我将它设置为一个标签,然后返回0?这不是它存储的形式,mysql说它将返回AI primarykey,它是我的用户表中的用户ID?啊,等等,不,我忘记了最后的select语句,我尝试了最后插入的select语句,再次得到相同的错误,所以看起来Justin可能是对的