Advertisement
demoraesr89

C# and SQL practice

Aug 2nd, 2016
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace EmployeesData
  13. {
  14. public partial class Form2 : Form
  15. {
  16. private int empid1;
  17. private string name1;
  18. public int getempid { set { empid1 = value; } }
  19. public string getname { set { name1 = value; } }
  20.  
  21.  
  22. public Form2()
  23. {
  24. InitializeComponent();
  25. }
  26.  
  27. private void Form2_Load(object sender, EventArgs e)
  28. {
  29. label2.Text = empid1.ToString();
  30. label3.Text = name1.ToString();
  31. }
  32. }
  33. }
  34.  
  35.  
  36. ----------------------------------------- new page ------------------------------------------------------
  37.  
  38.  
  39. using System;
  40. using System.Collections.Generic;
  41. using System.ComponentModel;
  42. using System.Data;
  43. using System.Data.SqlClient;
  44. using System.Drawing;
  45. using System.Linq;
  46. using System.Text;
  47. using System.Threading.Tasks;
  48. using System.Windows.Forms;
  49.  
  50. namespace EmployeesData
  51. {
  52. public partial class Form1 : Form
  53. {
  54. SqlConnection con = new SqlConnection(@"Data Source=PP001544;Initial Catalog=EmpProfile;Integrated Security=True");
  55.  
  56. public Form1()
  57. {
  58. InitializeComponent();
  59.  
  60. }
  61.  
  62. private void button2_Click(object sender, EventArgs e)
  63. {
  64. SqlConnection con = new SqlConnection(@"Data Source=PP001544;Initial Catalog=EmpProfile;Integrated Security=True");
  65. try
  66. {
  67. con.Open();
  68. toolStripStatusLabel1.Text = ":Connected Successfully!";
  69. SqlCommand cmd = con.CreateCommand();
  70. cmd.CommandText = "Insert into Employee (name,age,gender,email,password1,empid) values ('" + textBox2.Text + "',"+textBox3.Text+",'" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "',"+textBox1.Text+")";
  71.  
  72. try
  73. {
  74. cmd.ExecuteNonQuery();
  75. toolStripStatusLabel1.Text = "Record Inserted Successfully";
  76. }
  77. catch
  78. {
  79. toolStripStatusLabel1.Text = "Querry Execution Error";
  80. }
  81. }
  82. catch
  83. {
  84. toolStripStatusLabel1.Text = "Connection NOT successful";
  85. }
  86.  
  87. }
  88.  
  89. private void button1_Click(object sender, EventArgs e)
  90. {
  91. SqlConnection con = new SqlConnection(@"Data Source=PP001544;Initial Catalog=EmpProfile;Integrated Security=True");
  92. SqlDataAdapter sda = new SqlDataAdapter("Select * from Employee where empid="+textBox1.Text+"",con);
  93.  
  94. DataTable dt = new DataTable();
  95. sda.Fill(dt);
  96. textBox2.Text = dt.Rows[0][0].ToString();
  97. textBox3.Text = dt.Rows[0][1].ToString();
  98. textBox4.Text = dt.Rows[0][2].ToString();
  99. textBox5.Text = dt.Rows[0][3].ToString();
  100. textBox6.Text = dt.Rows[0][4].ToString();
  101. }
  102.  
  103. private void button3_Click(object sender, EventArgs e)
  104. {
  105. SqlConnection con = new SqlConnection(@"Data Source=PP001544;Initial Catalog=EmpProfile;Integrated Security=True");
  106.  
  107. SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from Employee where name='" + textBox2.Text + "' and empid=" + textBox1.Text + "", con);
  108.  
  109. DataTable dt = new DataTable();
  110. sda.Fill(dt);
  111. if (dt.Rows[0][0].ToString() == "1")
  112. {
  113. Form2 g = new Form2();
  114. g.getname = textBox2.Text;
  115. g.getempid = Int32.Parse(textBox1.Text);
  116.  
  117. g.Show();
  118. }
  119.  
  120.  
  121. }
  122.  
  123. private void button4_Click(object sender, EventArgs e)
  124. {
  125.  
  126. string sql = "Select * From Employee where empid='"+textBox1.Text+"'";
  127. SqlConnection con = new SqlConnection(@"Data Source=PP001544;Initial Catalog=EmpProfile;Integrated Security=True");
  128. SqlDataAdapter dataadapter = new SqlDataAdapter(sql, con);
  129.  
  130. DataTable ds = new DataTable();
  131. con.Open();
  132. dataadapter.Fill(ds);
  133. con.Close();
  134. dataGridView1.DataSource = ds;
  135.  
  136. }
  137.  
  138. private void button5_Click(object sender, EventArgs e)
  139. {
  140. string sql = "Select * From Employee";
  141. SqlConnection con = new SqlConnection(@"Data Source=PP001544;Initial Catalog=EmpProfile;Integrated Security=True");
  142. SqlDataAdapter dataadapter = new SqlDataAdapter(sql, con);
  143.  
  144. DataTable ds = new DataTable();
  145. con.Open();
  146. dataadapter.Fill(ds);
  147. con.Close();
  148. dataGridView1.DataSource = ds;
  149. }
  150.  
  151. private void button6_Click(object sender, EventArgs e)
  152. {
  153. SqlCommand cmd = new SqlCommand();
  154. cmd.CommandText = string.Format("Update Employee SET name='{1}', age='{2}', gender='{3}', email='{4}' WHERE empid='{0}'", textBox1.Text.Trim(), textBox2.Text.Trim(), textBox3.Text.Trim(), textBox4.Text.Trim(), textBox5.Text.Trim());
  155.  
  156. cmd.CommandType = CommandType.Text;
  157. cmd.Connection = con;
  158.  
  159. con.Open();
  160. cmd.ExecuteNonQuery();
  161. {
  162. MessageBox.Show("Profile Updated Successfully!");
  163. con.Close();
  164. }
  165. }
  166.  
  167. private void button7_Click(object sender, EventArgs e)
  168. {
  169. SqlCommand cmd = new SqlCommand();
  170. cmd.CommandText = string.Format("Delete from Employee where empid='{0}'", textBox1.Text.Trim());
  171.  
  172. cmd.CommandType = CommandType.Text;
  173. cmd.Connection = con;
  174.  
  175. con.Open();
  176. cmd.ExecuteNonQuery();
  177. {
  178. MessageBox.Show("Profile Updated Successfully!");
  179. con.Close();
  180. }
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement