Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7792957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:20:05+00:00 2026-06-01T22:20:05+00:00

Here is the query: string query = @INSERT INTO session (PK_Id, user_id, login_time, machine_ip,

  • 0

Here is the query:

string query = @"INSERT INTO session (PK_Id, user_id, login_time, machine_ip, machine_fingerprint) 
                             VALUES (UUID(), @UId, @LogInTime, @MIp, @MFingerPrint);
                ";

Now I need this last inserted id back, which is a UUID generated by MySQL. As far as I read there is no select_last_insert_id() function for UUIDs!! And I read for php you could assign UUID() function first to a variable and then return that value. But how to go about that in C#?

Something like this, but not exactly:

string query = @"INSERT INTO session (PK_Id, user_id, login_time, machine_ip, machine_fingerprint) 
                             VALUES (@UUID = SELECT UUID(), @UId, @LogInTime, @MIp, @MFingerPrint);
                ";                  //how to do this here?

Here is more of my code:

string query = @"INSERT INTO session (PK_Id, user_id, login_time, machine_ip, machine_fingerprint) 
                 VALUES (@UUID = SELECT UUID(), @UId, @LogInTime, @MIp, @MFingerPrint);
                ";

try
{
    if (_conn.State != ConnectionState.Open)
        _conn.Open();
    MySqlCommand cmd = new MySqlCommand(query, _conn);
    cmd.Parameters.AddWithValue("@UId", Utility.usr.Id);
    cmd.Parameters.AddWithValue("@LogInTime", DateTime.Now);
    cmd.Parameters.AddWithValue("@MIp", GetMachineIP());
    cmd.Parameters.AddWithValue("@MFingerPrint", GetHardwareFingerPrint());

    var s= Convert.ToString(cmd.ExecuteScalar()); //this returns an empty string :(
    //I need to get it to any .NET data type, string, or Guid or byte[] or anything. 

But I need this datatype of s to be used in another WHERE clause in a query like this:

string query = @"UPDATE session SET logout_time = @LogOutTime 
                 WHERE user_id = @UId AND PK_Id = @SessionId";

try
{
    if (_conn.State != ConnectionState.Open)
        _conn.Open();
    MySqlCommand cmd = new MySqlCommand(query, _conn);
    cmd.Parameters.AddWithValue("@UId", Utility.usr.Id);
    cmd.Parameters.AddWithValue("@SessionId", s);
    cmd.Parameters.AddWithValue("@LogOutTime", DateTime.Now);
    cmd.ExecuteScalar();

Here @"SessionId" is the UUID field in the same table. So basically, how can I get the MySQL varbinary field in C# so that I could use that type to update by specifying WHERE in another query?

In MySQL table the UUID field is varbinary (I hope to see some solution that is not another php link or that is not asking me to switch to char datatype in the database 🙂 ).

Edit: The problem here is we have already added plenty of UUIDs generated by MySQL into the table, so I’m a bit apprehensive about changing MySQL UUID to .NET Guid. If that’s the only workaround, I’ll consider that. Just that this is the first time we needed the inserted UUID value back so that I can update in another query another point of time.

A sub question: Is .NET Guid exactly the same thing as MySQL UUID?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:20:08+00:00Added an answer on June 1, 2026 at 10:20 pm

    I think you can go ahead with your earlier implementation without having to rely on MS Guid, but I fear I am too late 🙂

    string query = @"INSERT INTO session (PK_Id, user_id, login_time, machine_ip, machine_fingerprint) 
                                 VALUES (UUID(), @UId, @LogInTime, @MIp, @MFingerPrint); 
                     SELECT PK_Id FROM session WHERE login_time=@LogInTime AND machine_fingerprint=@MFingerPrint; //or something similar which gives you the exact same id - UUID
                    ";
    
    try
    {
        if (_conn.State != ConnectionState.Open)
            _conn.Open();
        MySqlCommand cmd = new MySqlCommand(query, _conn);
        cmd.Parameters.AddWithValue("@UId", Utility.usr.Id);
        cmd.Parameters.AddWithValue("@LogInTime", DateTime.Now);
        cmd.Parameters.AddWithValue("@MIp", GetMachineIP());
        cmd.Parameters.AddWithValue("@MFingerPrint", GetHardwareFingerPrint());
    
        MySqlDataReader r = cmd.ExecuteReader();
    
        if (r.Read()) //ensure if it is read only once, else modify your `WHERE` clause accordingly
        {
            var s = (Guid)r[0];
        }
        //or even (Guid)cmd.ExecuteScalar() would work
    

    Now you can query in update like this:

    string query = @"UPDATE session SET logout_time = @LogOutTime 
                            WHERE user_id = @UId AND PK_Id = @SessionId";
    
    try
    {
        if (_conn.State != ConnectionState.Open)
            _conn.Open();
        MySqlCommand cmd = new MySqlCommand(query, _conn);
        cmd.Parameters.AddWithValue("@UId", Utility.usr.Id);
        cmd.Parameters.AddWithValue("@SessionId", s.ToByteArray());
        cmd.Parameters.AddWithValue("@LogOutTime", DateTime.Now);
        cmd.ExecuteNonQuery();
    

    Note: Here I have converted the Guid variable s to byte array before querying. This is important, in WHERE clause, be it UPDATE or SELECT statements in query. I would ask you to move to binary field in MySQL table from varbinary.

    Edit: If your table would grow dramatically large then inserting and selecting is a bad idea since SELECT query is an additional query being run. In that case @PinnyM’s choice is better. I really do not think MySQL or any other database would have a default way to give back “custom” inserted ids which are not something database generated. So in short I advice you to not go for this..

    Edit2: See this answer for getting binary value to .NET datatype. Sometimes casting do not work depending on MySQL .NET connector version..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code I input... mysql_query(INSERT INTO test (string) VALUES ('tes><ssst')); And this
here my code- $things = mysql_real_escape_string(implode(',', $_POST['things']),$link); $q = INSERT INTO tblslider(src) VALUES ('.$things.');
I'm using this query to insert into a table a date: DateTime dt =
I'm stuck with another coordinates related query here. I have a number of CSV
Here's the query: SELECT COUNT(*) AS c, MAX(`followers_count`) AS max_fc, MIN(`followers_count`) AS min_fc, MAX(`following_count`)
Here is my query Select Gender from Table The result pane shows 1 1
Here is the query that I am working on: SELECT `unitid`, `name` FROM apartmentunits
Here's my query: DECLARE @StartRow INT DECLARE @PageSize INT SET @StartRow = 1 SET
Here is my Query : select EmployeeName, EmployeeSalary from Employee2 for xml path('EmployeeDetails') returns
Here is my query select t1.*, t2.name as customer, t2.name as vendor from order

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.