0% found this document useful (0 votes)
6 views

Connection

FJSDFSJDFSDJFSDFJSFSJD

Uploaded by

happylifehome924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Connection

FJSDFSJDFSDJFSDFJSFSJD

Uploaded by

happylifehome924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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 WindowsFormsApplication86
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection();
public Form1()
{
con.ConnectionString = "data source=.; initial catalog=jahan;
integrated security=true";
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void button4_Click(object sender, EventArgs e)


{
con.Open();
if(con.State == ConnectionState.Open )
{
MessageBox.Show("done");
}
}

private void button1_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into student(id,name,address)values(N'"
+ idtxt.Text
+ "',N'" + nametxt.Text + "',N'" + addresstxt.Text + "')";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("data saved");
con.Close();
}

private void button2_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from student where id=N'" + idtxt.Text +
"'";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("data deleted");
con.Close();
}

private void button3_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "update student set name='" + nametxt.Text
+ "', address='"
+ addresstxt.Text + "' where id='" + idtxt.Text + "'";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("data updated");
con.Close();
}

private void idtxt_TextChanged(object sender, EventArgs e)


{
SqlDataReader dr;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select *from student where id='" + idtxt.Text
+ "'";
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
while(dr.Read ())
{
nametxt.Text = dr[@"name"].ToString();
addresstxt.Text = dr[@"address"].ToString();
}
con.Close();
}
}
}

You might also like