Hello all, this is another issue I faced when I was trying to write an insert function for one of my friend’s assignments. anyways, this may be a small task, but because of having a lack of knowledge in this tech, I had to do research on how we can insert some data into an SQL database in an easy way.
Let’s see the way you need to use this. (This can be buggy for some people because it was only for educational purposes.)
Let’s write the code now.
First you have to import the sql data client. (If you see an error, you may have to add it using another method. I remember I got that issue, but can’t go back to find the way, let’s google it.)
After adding above code lines, write down the following code lines for your insert button.
string connectionString = “Data Source=.\SQLEXPRESS;Initial Catalog=ComputerShop;Integrated Security=True”;
SqlConnection connection = new SqlConnection(@connectionString);
string query = “INSERT INTO Person (Name,Salary) VALUES(‘Max’,’$1200′)”;
SqlCommand command = new SqlCommand(query, connection);
Console.WriteLine(“Records Inserted Successfully”);
catch (SqlException e)
Console.WriteLine(“Error Generated. Details: ” + e.ToString());
Remember, these code lines won’t work, if you haven’t started your SQL server and if something is wrong with the system. Anyways, congrats on your project. Let me know if you got any issues in the comment section.