MySqlCommandBuilder in C#

Mousmi Nishad 1 Reputation point
2021-10-22T09:19:11.68+00:00

I want to insert data to mysql database using commandbuilder at the same time i want to retrive the id of each recode that i inserted to the table.

id is autoincrement.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,819 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,337 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,190 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sreeju Nair 11,606 Reputation points
    2021-10-22T11:04:35.947+00:00

    MySqlCommand has a member called LastInsertedId which will give you the last inserted Id.

    MySqlCommand cmd= _conn.CreateCommand();
    cmd.CommandText = "your insert query here";
    cmd.ExecuteNonQuery();
    var insertedId= cmd.LastInsertedId;

    0 comments No comments

  2. Mousmi Nishad 1 Reputation point
    2021-10-29T09:30:49.437+00:00

    @Jack J Jun
    I use below code. the data is from local db and inserting to web db . Record may increase to 1000

      public ArrayList ExportItems(MySqlConnection connection, ArrayList arrListItem)  
            {  
                if (connection.State == ConnectionState.Open)  
                    connection.Close();  
                connection.Open();  
                ArrayList items = new ArrayList();  
                try  
                {  
                    int itemid;  
                    
                     cmd = new MySqlCommand();  
                    cmd.Connection = connection;  
                    cmd.CommandType = CommandType.Text;  
                    cmd.CommandText = "INSERT INTO items(name,detail,status,created_at,updated_at) VALUES(@NAME,@DETAIL,@STATUS,@CREATEDDATE,@MODIFIEDDATE);";  
                    for (int i = 0; i < arrListItem.Count; i++)  
                    {  
                        ItemDetails itemdetails = (ItemDetails)arrListItem[i];  
                        cmd.Parameters.Add("@NAME", MySqlDbType.VarChar).Value = itemdetails.itemName;  
                        cmd.Parameters.Add("@DETAIL", MySqlDbType.VarChar).Value = itemdetails.discription;  
                        cmd.Parameters.Add("@STATUS", MySqlDbType.VarChar).Value = itemdetails.status;  
                        cmd.Parameters.Add("@CREATEDDATE", MySqlDbType.DateTime).Value = DateTime.Now;  
                        cmd.Parameters.Add("@MODIFIEDDATE", MySqlDbType.DateTime).Value = DateTime.Now;  
                        cmd.ExecuteNonQuery();  
                        itemid = Convert.ToInt32(cmd.LastInsertedId);  
                        itemdetails.itemID = itemid;  
                        cmd.Parameters.Clear();  
                        items.Add(itemdetails);  
                    }  
      
                    return items;  
                }  
                catch (Exception ex)  
                {  
                    resultString = ex.Message;  
                    return null;  
                }  
            }