Aim:

To perform the database connectivity  in c# windows forms application.

Algorithm:

Step 1: Open the visual studio 2010 and select the file menunewprojectwindows c# windows form applicationsfile nameok.

Step 2: Design the form as per the columns in the database.

Step 3: Add (usingSystem.Data.SqlClient;) in the program.

Step 4: Use SqlConnection, SqlDataAdapter , SqlCommand , DataSet  to connect a database.

Step 5 : Create a object for sqlcommand and get the values from the user.

Step 6 : Type a code for save  button.
cmd = newSqlCommand("insert into tbl_studatabaseconnectivitytest values('" + txtregno.Text + "','" + txtsname.Text + "','" + txtprogramme.Text + "','" + txtsyear.Text + "','" + txtsem.Text + "')", con);
cmd.ExecuteNonQuery();

Step 7:Compile and run the program without error.


PROGRAM


using System;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Data.SqlClient;

namespace WindowsFormsApplication1

{

publicpartialclassForm1 : Form

    {

SqlConnection con = newSqlConnection(@"Data Source=DESKTOP-6A6484E\SQLEXPRESS;InitialCatalog=c#;Integrated Security=True");

SqlDataAdapteradap;

SqlCommandcmd;

DataSet ds = newDataSet();

public Form1()

        {

InitializeComponent();

        }

privatevoid Form1_Load(object sender, EventArgs e)

        {

con.Open();

ds.Clear();

adap = newSqlDataAdapter("select * from tbl_studatabaseconnectivitytest", con);

adap.Fill(ds);

        }

privatevoidsave_Click(object sender, EventArgs e)

        {

cmd = newSqlCommand("insert into tbl_studatabaseconnectivitytest values('" + txtregno.Text 
+ "','" + txtsname.Text + "','" + txtprogramme.Text + "','" + txtsyear.Text + "','" + txtsem.Text + "')", con);

cmd.ExecuteNonQuery();

        }

    }

}


OUTPUT:



BY
      REGU RAM SV

Post a Comment

أحدث أقدم