Insert button Code and Mysql data to be save and display in grdiview

Mohamed Rafi N 106 Reputation points
2022-03-17T11:49:08.293+00:00

I am very new and using visual studio community 2000 and mysql work bench 8.0,

I am having 3 textbox names like textbox1,2,3 and 1 insert button and 1 data gridview for saving data in mysql database, i need code for insert button..

This is mysql coding:

SELECT * FROM testdb.employee;
insert into testDB.employee(employee_id,employee_name,employee_salary) Values ('123','RAFI','10000')
insert into testDB.employee(employee_id,employee_name,employee_salary) Values ('456','RAJESH','20000')
insert into testDB.employee(employee_id,employee_name,employee_salary) Values ('789','RAMESH','30000')
select * from testDB.employee

I don't know how to connecting string and where to write for database connection for mysql workbench 8.0 and visual studio community 2022 pls send me step procedure

Azure Database for MySQL
Developer technologies | ASP.NET Core | Other
0 comments No comments

Answer accepted by question author
Oury Ba-MSFT 21,156 Reputation points Microsoft Employee Moderator
2022-03-21T17:10:05.157+00:00

Hi @Mohamed Rafi N Thank you for posting your Question on Microsoft Q&A and for using Azure services.

Sorry for the delay in response to your question.

If my understanding is correct, you are looking for a code for insert button and you would also like to write a connection string for your SQL workbench 8.0 using visual studio. Please let me know if my understanding is not correct.
I have tried to repo the issue successfully.

To create the form in visual studio please follow the step below

  1. Go to your visual studio
  2. click on create a new project
  3. once the page is open and type on the search as shown below
    185268-image.png

185260-image.png

Once the page is created click on search text box then start drag and drop
185341-image.png

185269-image.png

185332-image.png

How to write the connection string

int id = Convert.ToInt32(textBox1.Text);
string name = textBox2.Text;
double Salary = Convert.ToDouble(textBox3.Text);

string connectionString;
MySqlConnection cnn;
connectionString = "server=localhost;database=test;uid=root;pwd=password@123;";
cnn = new MySqlConnection(connectionString);
cnn.Open();
string insertQuery = "insert into test.Employee(employee_id,employee_name,employee_salary) values(" + id + ",'" + name + "'," + Salary + ")";
MySqlCommand cmd = new MySqlCommand(insertQuery,cnn);
MySqlDataReader rdr = cmd.ExecuteReader();
cnn.Close();

Like below

185306-image.png

Please let me know if you are still having problems.

Regards,
Oury

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.