C# 将lastinsertedid从mysql传递到aspx文件

C# 将lastinsertedid从mysql传递到aspx文件,c#,mysql,C#,Mysql,这是我的webapi文件,在变量'imageId'中,我可以得到我最后插入的id,所以我想将此id传递给同一项目中的aspx文件,可以吗 string connString = "Server=localhost;database=abcd;uid=abcd;password=abcd"; try { using (MySqlConnection mcon = new MySqlConnection(connString))

这是我的webapi文件,在变量'imageId'中,我可以得到我最后插入的id,所以我想将此id传递给同一项目中的aspx文件,可以吗

        string connString = "Server=localhost;database=abcd;uid=abcd;password=abcd";
        try
         {
          using (MySqlConnection mcon  = new MySqlConnection(connString))
            {
              using (MySqlCommand cmd = mcon.CreateCommand())
               {
                 mcon.Open();
                 cmd.CommandText = @"INSERT INTO `contentupload`.`content_document`
                   (
                   `content`)
                   VALUES (@content);";
                   cmd.Parameters.AddWithValue("@content", content);
                   cmd.ExecuteNonQuery();
                   long imageId = cmd.LastInsertedId;
                   mcon.Close();
                   return;
                   }
                  }
                 }

        catch (MySqlException e)
        {
            return ;
        }

    }

我希望这对你有用。。!!您不需要显式关闭连接,它将在离开作用域后立即关闭

   string connString = "Server=localhost;database=abcd;uid=abcd;password=abcd";
    try
     {
      using (MySqlConnection mcon  = new MySqlConnection(connString))
        {
          using (MySqlCommand cmd = mcon.CreateCommand())
           {
             mcon.Open();
             cmd.CommandText = @"INSERT INTO `contentupload`.`content_document`
               (
               `content`)
               VALUES (@content);";
               cmd.Parameters.AddWithValue("@content", content);
               cmd.ExecuteNonQuery();
               long imageId = cmd.LastInsertedId;
               Response.Redirect("YourPage.aspx?imageID="+imageId.ToString());
               }
              }
             }

    catch (MySqlException e)
    {
        return ;
    }

}
在YourPage.aspx加载事件中,只需检索它

var imageId = Request.QueryString["imageId"];
并将其转换并正确使用。。
希望能有所帮助。

是的,这可能是错误的,例如你在一些变量中得到了id,比如var id=你最后插入的id,接下来是Response.Redirect(“Page.aspx?id”+id);