How to Create Database component -Enterprise Application Using C# Lab
1. Studb.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace student
{
public class studb
{
public string getId(string sqlquery, SqlConnection connectionstring, ComboBox combox1)
{
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter(sqlquery, connectionstring);
adap.Fill(ds, "student");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
combox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
}
return null;
}
public string add(string id, string name, string dept, string phone, SqlConnection
connectionstring)
{
string sqlquery = "insert into student(id,name,dept,phone) values ('" + id + "','" + name + "','" +
dept + "','" + phone + "')";
SqlCommand cmd = new SqlCommand(sqlquery,connectionstring );
//connectionstring.Open();
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Inserted");
}
catch (Exception e)
{
MessageBox.Show("not inserted");
}
return null;
2. }
public string showgrid(string sqlquery, SqlConnection connectionstring, DataGridView
grid1)
{
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter(sqlquery, connectionstring);
adap.Fill(ds, "student");
grid1.DataSource = ds;
grid1.DataMember = "student";
return null;
}
}
}
************************
Form1.cs
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 student;
using System.Data.SqlClient;
namespace college
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
con.ConnectionString = @"Data
Source=(LocalDB)v11.0;AttachDbFilename=E:c#studentstudentCollege.mdf;Integrated
Security=True";
3. con.Open();
string sqlquery = "select id from student";
studb st = new studb();
st.getId(sqlquery, con, comboBox1);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string sqlquery = "Select * from student where id='"+comboBox1.Text +"'";
studb st = new studb();
st.showgrid(sqlquery, con, dataGridView1);
}
private void button1_Click(object sender, EventArgs e)
{
studb st = new studb();
st.add(textBox1.Text, textBox2.Text, textBox3.Text,textBox4.Text, con);
}
private void Exit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}