0

I am working in Visual Studio C# 2010 Express and having some problems when working with mySQL database.

I have implemented the Connect button, so that after you filling all the account information (Server, User, Port and Password), you click Connect button then list of Databases will be displayed, as well as table in each Database (when you select certain database), as well as data in each table (when you select certain table).

Now I am trying to implement Insert, Update and Delete button but don't know where to start and I mostly confuse about the Insert button (I mean different table have different number of column, so on...) and I can't imagine how it work when click the Insert button. Someone can give me some advise or sample piece of code so that I can following for the rest.

Thank you!

2 Answers 2

1

If you can find a way to enter new records into the last row of your grid, you could craft a query like so

cmd.CommandText = "insert into " + listTables.Items[listTables.SelectedIndex].ToString() + "(";

foreach( var col in dataGridTableView.DataSource.DataTable[0].Columns)
    cmd.CommandText += col.ColumnName +  "," ;

// trim last comma off

cmd.CommandText += ") Values( ";

// access newly created row
// iterate over the cells of the row, and add values to the command text we've been building
// execute command text.
1
  • Yes, I can type words in the last row of the grid, can you provide a details codes with comment so I can understand what you are doing? Really appreciated it. Feb 15, 2013 at 6:04
0

You could perhaps have a look at the following. http://www.daniweb.com/software-development/csharp/threads/235966/mysql-insert-from-c-app

public void CreateMySqlCommand() 
{
  MySqlConnection myConnection = new MySqlConnection("Persist Security Info=False;database=test;server=myServer");
  myConnection.Open();
  MySqlTransaction myTrans = myConnection.BeginTransaction();
  string mySelectQuery = "SELECT * FROM myTable";
  MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection,myTrans);
  myCommand.CommandTimeout = 20;
}

I would make use of commands with some SQL to do my insert / update / delete etc.

See the following for the mySQL SQL syntax http://dev.mysql.com/doc/refman/5.0/en/sql-syntax.html

Go to: 13.2. Data Manipulation Statements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.