CODE

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
static string conString = "Data Source=(LocalDB)\\
MSSQLLocalDB;AttachDbFilename=c:\\users\\student.desktop-05rcfoa.000\\documents\\
visual studio 2015\\Projects\\WindowsFormsApplication6\\WindowsFormsApplication6\\
Database1.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(conString);

public Form1()
{
InitializeComponent();
}

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)


{

private void button1_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("select * from table1",con);
SqlDataReader reader = cmd.ExecuteReader();

listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();

while (reader.Read())
{
listBox1.Items.Add(reader[0]);
listBox2.Items.Add(reader[1]);
listBox3.Items.Add(reader[2]);
}
con.Close();
}

private void button2_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO table1 VALUES(@RN,@N,@M)",
con);
cmd.Parameters.AddWithValue("@RN", Int32.Parse(textBox1.Text));
cmd.Parameters.AddWithValue("@N", textBox2.Text);
cmd.Parameters.AddWithValue("@M", Int32.Parse(textBox3.Text));
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record inserted");
}

private void button3_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE table1 SET RNO=@RN, STNAME=@N,
MARKS=@M WHERE RNO=@RN", con);
cmd.Parameters.AddWithValue("@RN", Int32.Parse(textBox1.Text));
cmd.Parameters.AddWithValue("@N", textBox2.Text);
cmd.Parameters.AddWithValue("@M", Int32.Parse(textBox3.Text));
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record uPDATED");
}

private void button4_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("DELETE FROM table1 WHERE RNO=@RN",
con);
cmd.Parameters.AddWithValue("@RN", Int32.Parse(textBox1.Text));
//cmd.Parameters.AddWithValue("@N", textBox2.Text);
//cmd.Parameters.AddWithValue("@M", Int32.Parse(textBox3.Text));
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record dELETED");
}
}
}

You might also like