Skip to main content

Example of code of c sharp with SQL Server connectivity


 using System;  
  using System.Data;  
  using System.Text;  
  using System.Windows.Forms;  
  using System.Data.SqlClient;  
  using System.Drawing;  
  namespace DatabaseExp  
  {  
  public partial class DatabaseExp : Form  
  {  
  SqlConnection conn;  
  SqlDataAdapter adaptr;  
  SqlCommand cmd;  
  DataTable tbl;  
  static int count = 0, current = 0;  
  static int check = 0;  
  public DatabaseExp()  
  {  
  InitializeComponent();  
 }  
  private void DatabaseExp_Load(object sender, EventArgs e)  
  {  
  string connectionString = null;  
  adaptr = new SqlDataAdapter();  
  tbl = new DataTable();  
  connectionString = "Data Source=127.0.0.1;Initial Catalog=Abdul;User ID=sa;Password=allah";  
  conn = new SqlConnection(connectionString);  
  try  
  {  
  conn.Open();  
  MessageBox.Show("Connection Open !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  //conn.Close();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show("Can not open connection ! ");  
  }  
  }  
 private void cmd_Add_Click(object sender, EventArgs e)  
  {  
  string sql;  
  sql = "Insert INTO Student VALUES('" + tRoll.Text + "','" + tName.Text + "','" + tAddress.Text + "')";  
  try  
  {  
  cmd = new SqlCommand(sql, conn);  
  cmd.ExecuteNonQuery();  
  MessageBox.Show("Record Inserted Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  cmd.Dispose();  
  Refresh();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  }  
 private void cmd_Refresh_Click(object sender, EventArgs e)  
  {  
  Refresh();  
  }  
 private void Refresh()  
  {  
  adaptr.Dispose();  
  tbl.Reset();  
  adaptr.SelectCommand = new SqlCommand("Select * from Student", conn);  
  adaptr.Fill(tbl);  
  count = tbl.Rows.Count;  
  current = 0;  
  if (count > 0)  
  Display(tbl);  
  else  
  MessageBox.Show("No Records In The Table", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
 }  
  private void Display(DataTable tbl)  
  {  
  tRoll.Text = tbl.Rows[current]["Roll Number"].ToString();  
  tName.Text = tbl.Rows[current]["Name"].ToString();  
  tAddress.Text = tbl.Rows[current]["Address"].ToString();  
  }  
 private void cmd_Clear_Click(object sender, EventArgs e)  
  {  
  tRoll.Text = "";  
  tName.Text = "";  
  tAddress.Text = "";  
  }  
 private void cmd_Delete_Click(object sender, EventArgs e)  
  {  
  if (MessageBox.Show("Do You Want to Delete This Record?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)  
  {  
  string sql;  
  string key = tRoll.Text.ToString();  
  sql = "Delete From Student Where [Roll Number]='" + key + "'";  
  try  
  {  
  cmd = new SqlCommand(sql, conn);  
  cmd.ExecuteNonQuery();  
  MessageBox.Show("Record Deleted Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  cmd.Dispose();  
  Refresh();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  }  
  }  
  private void cmd_Search_Click(object sender, EventArgs e)  
  {  
  string sql;  
  string nm = tName.Text.ToString();  
  sql = "Select * From Student Where [Name]='" + nm + "'";  
  adaptr.Dispose();  
  tbl.Reset();  
  adaptr.SelectCommand = new SqlCommand(sql, conn);  
  adaptr.Fill(tbl);  
  count = tbl.Rows.Count;  
  current = 0;  
  //MessageBox.Show(count + "");  
  if (count > 0)  
  Display(tbl);  
  else  
  MessageBox.Show("No Record In The Table", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  }  
 private void cmd_Previous_Click(object sender, EventArgs e)  
  {  
  if (current > 0)  
  {  
  current--;  
  Display(tbl);  
  }  
  else  
  {  
  current = count - 1;  
  Display(tbl);  
  }  
  }  
  private void cmd_Next_Click(object sender, EventArgs e)  
  {  
  if (current == count - 1)  
  {  
  current = 0;  
  Display(tbl);  
  }  
  else  
  {  
  current++;  
  Display(tbl);  
  }  
  }  
 private void cmd_Cancel_Click(object sender, EventArgs e)  
  {  
  if (check == 1)  
  {  
  Button_Status(true);  
  check = 0;  
  }  
  else  
  {  
  try  
  {  
  conn.Close();  
  this.Dispose();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  }  
  }  
 private void Button_Status(Boolean value)  
  {  
  cmd_Add.Enabled = value;  
  cmd_Refresh.Enabled = value;  
  cmd_Clear.Enabled = value;  
  cmd_Delete.Enabled = value;  
  cmd_Previous.Enabled = value;  
  cmd_Next.Enabled = value;  
 }  
  private void cmd_Edit_Click(object sender, EventArgs e)  
  {  
  check = 1;  
  Button_Status(false);  
  tRoll.Enabled = false;  
  }  
 private void cmd_Save_Click(object sender, EventArgs e)  
  {  
  string sql;  
  string key = tRoll.Text.ToString();  
  if (check == 1)  
  {  
  check = 0;  
  tRoll.Enabled = true;  
  sql = "Update Student Set [Name]='" + tName.Text + "',[Address]='" + tAddress.Text + "' Where [Roll Number]='" + key + "'";  
  try  
  {  
  cmd = new SqlCommand(sql, conn);  
  cmd.ExecuteNonQuery();  
  MessageBox.Show("Record Edited Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  cmd.Dispose();  
  Button_Status(true);  
  tbl.Reset();  
  Refresh();  
  }  
  catch (Exception ex)  
  {  
  MessageBox.Show(ex.Message);  
  }  
  }  
  }  
 }  
  }  

Popular posts from this blog

All songs notation and chords at one place

Song : O Saathi Re Film : Mukhathar Ka Sikkandhar Uses : C D D# E G A Note : The numbers at the end of the lines indicate line numbers. Pallavi: O saathi re, tere binaa bhi kya jina, tere binaa bhi kya jina A- C D D#....,D D C DD E...C..CA-...,D D C DD E...CC.......1 Play line 1 again phulon men khaliyon men sapnom ki galiyon men GGG...GAGE.. GGG G A G E.................................................2 tere bina kuchh kahin naa E A G E D C D D#.......................................................................3 tere binaa bhi kya jina, tere binaa bhi kya jina D D C DD E....C..CA-..., D D C DDE....CC.............................4 Charanam: har dhadkan men, pyaas hai teri, sanson men teri khushboo hai CCC C D C A-, CCC C D C A-, DDD DED CD EE.. CCCC......................5 is dharthi se, us ambar tak, meri nazar men tu hi tu hai CCC C D C A-, CCC C D C A-, DDD DED CD EE.. CCCC......................6 pyaar yeh tute naa GGG... GAG D#......E............................

Song Aankhen Khuli Ho lyrics notation

Song : Aankhen Khuli Ho Movie: Mohabbatein Notes used : W=>Western - C D E F G- A- B-/ H=>Hindustani - S R G M P- D- N- ( Here for western, G=G-, A=A-, & B=B- ) ( For hindustani, P=P-, D=D-, & N=N- ) Song I : Aankhen Khuli...Ho Ya.. Ho Bandh W=> A.... C... B..C.. E.. E...... A... A.... H=> D... S... N..S.. G G....... D... D.... Deedaar Un Ka Ho.o.taa Hai.. W=> A...B....A....D.BAG....ADB... H=> D...N...D.....R.NDP...DRN... Kaise Kahoon Main O..Yaaraa W=> B..D.. D....E.... D.....C..C..C... H=> N..R.. R....G... R.....S..S..S..... Ye Pyaar Kaise Hota Hai W=> E...B.....DB...AG...B..AA H=> G...N....RN...DP...N...DD (Tururu ru ru, ru ru rururu ru......) W=> AA...GA...BCE..., B...DB..GA H=> DD...PD...NSG..., N..RN.. PD Song II: Aa.aj He Kisi..par Yaa.ro.on..., Marke De..Khe..gein Hum W=> E....FEDCBABC.D.. D D......., G A B C.... E.......D...D..... H=> G....MGRSNDNS.R. R R......., P D N S.....G........R...R.... Pyaar Ho...

How to create View using Union query from one database to another database in SQL

To create View using Union query from one database to another database in SQL you can get hints from below example its very simple your column should be the same type and number of the column should be same to create union query view. USE [TestDB] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create VIEW [dbo].[Test_FinancialDimension] AS ( SELECT [key_] ,[NAME] ,[Value] FROM MicrosoftDynamicsAX.dbo.dimattributecompanyinfo union SELECT [key_] ,[NAME] ,[Value] FROM MicrosoftDynamicsAX.dbo.DimAttributeOMCostCenter --union -- SELECT [key_] ,[NAME],[Value] FROM MicrosoftDynamicsAX.dbo.DimAttributeHcmWorker union SELECT [key_] ,[NAME] ,[Value] FROM MicrosoftDynamicsAX.dbo.DIMATTRIBUTEOMDEPARTMENT union SELECT [key_] ,[NAME] ,[Value] FROM MicrosoftDynamicsAX.dbo.DimAttributeOMB...