This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Friday, February 3, 2023

Inserting Data into a Database in Windows Form using C#

 Inserting Data into a Database in Windows Form using C#


In this post, we'll go through how to use the C# programming language to insert data into a database using a Windows Form. A connection must be established with the database, an INSERT statement must be run, and then the connection must be closed.


Prerequisites

Make sure you have the following before starting the implementation:


1.installed was Visual Studio

2. Installed is SQL Server Management Studio (SSMS).

3. a working knowledge of C# programming

4. a SQL Server-created database


Create windows form:

Opening Visual Studio and starting a new Windows Form project are the first steps in creating the Windows Form. Create the form as needed, adding the appropriate controls, including text boxes and buttons.

Connecting to the Database

Next, we need to create a connection to the database. This can be done by using the SqlConnection class from the System.Data.SqlClient namespace.



using System.Data.SqlClient;


SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=DBName;Integrated Security=True");




Replace DBName with the name of your database.


Executing the INSERT Statement:

Once the connection is established, we can execute the INSERT statement to insert data into the database. This can be done using the ExecuteNonQuery method of the SqlCommand class.


using System.Data.SqlClient;


SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=DBName;Integrated Security=True");

con.Open();


string insertQuery = "INSERT INTO TableName (Column1, Column2) VALUES (@Value1, @Value2)";

SqlCommand cmd = new SqlCommand(insertQuery, con);

cmd.Parameters.AddWithValue("@Value1", textBox1.Text);

cmd.Parameters.AddWithValue("@Value2", textBox2.Text);

cmd.ExecuteNonQuery();



Replace TableName with the name of your table and Column1 and Column2 with the names of the columns in your table.


Closing the Connection:

Close the database connection immediately after running the INSERT command. Calling the SqlConnection object's Close function will accomplish this.

con.Close();




Complete Code:

Here is the complete code for inserting data into a database in Windows Form using C#:



using System;

using System.Data.SqlClient;

using System.Windows.Forms;


namespace WindowsFormsApp1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=DBName;Integrated Security=True");

            con.Open();


            string insertQuery = "INSERT INTO TableName (Column1, Column2) VALUES (@Value1, @Value2)";

            SqlCommand cmd = new SqlCommand(insertQuery, con);

            cmd.Parameters.AddWithValue("@Value1", text


try

{

    SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=DBName;Integrated Security=True");

    con.Open();


    string insertQuery = "INSERT INTO TableName (Column1, Column2) VALUES (@Value1, @Value2)";

    SqlCommand cmd = new SqlCommand(insertQuery, con);

    cmd.Parameters.AddWithValue("@Value1", textBox1.Text);

    cmd.Parameters.AddWithValue("@Value2", textBox2.Text);

    cmd.ExecuteNonQuery();


    con.Close();

    MessageBox.Show("Data inserted successfully");

}

catch (Exception ex)

{

    MessageBox.Show(ex.Message);

}



}



Error Handling:

It is important to handle any errors that may occur during the process of inserting data into the database. This can be done using a try-catch block.

Conclusion:

 using C# to insert data into a Windows Form database is an easy and straightforward operation. You may easily implement the process in your own Windows Form application by following the methods covered in this article. After performing the INSERT statement, be sure to handle any potential issues and properly end the database connection.

No comments:

Post a Comment