Advertisement
Guest User

Eto na ung connection string natin

a guest
Jan 11th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. using System.Data.Sql;
  11.  
  12. namespace Database
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         private SqlConnection connection = new SqlConnection();
  17.         byte counter = 4;
  18.         byte count = 0;
  19.         string user = null;
  20.         string cString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"c:\\users\\user\\documents\\visual studio 2010\\Projects\\Database\\Database\\nbiCCMdb.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True";
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.             connection.ConnectionString = cString;
  25.         }
  26.  
  27.         private void button2_Click(object sender, EventArgs e)
  28.         {
  29.             connection.Close();
  30.             Application.Exit();
  31.         }
  32.  
  33.         private void button1_Click(object sender, EventArgs e)
  34.         {
  35.             try
  36.             {
  37.                 connection.Open();
  38.                 SqlCommand command = new SqlCommand();
  39.                 command.Connection = connection;
  40.                 command.CommandText = "SELECT * FROM userTable WHERE userName = '" + textBox1.Text + "' AND userPass = '" + textBox2.Text + "'";
  41.                 SqlDataReader reader = command.ExecuteReader();
  42.                 textBox1.Select();
  43.                 while (reader.Read())
  44.                 {
  45.                     count++;
  46.                 }
  47.                 if (count == 1)
  48.                 {
  49.                     user = textBox1.Text;
  50.                     textBox1.Clear();
  51.                     textBox2.Clear();
  52.                     connection.Close();
  53.                     this.Hide();
  54.                 }
  55.                 else if (count != 1 && !(textBox1.Text == String.Empty || textBox2.Text == String.Empty))
  56.                 {
  57.                     counter--;
  58.                     if (counter != 0)
  59.                     {
  60.                         MessageBox.Show("Incorrect Username/Password please try again." + Environment.NewLine +
  61.                         "You have " + counter + " tries left.");
  62.                         textBox2.Clear();
  63.                         textBox2.Focus();
  64.                     }
  65.                 }
  66.                 else
  67.                 {
  68.                     MessageBox.Show("Please enter Username/Password.");
  69.                     if (textBox1.Text == String.Empty)
  70.                     {
  71.                         textBox1.Focus();
  72.                     }
  73.                     else
  74.                     {
  75.                         textBox2.Focus();
  76.                     }
  77.                 }
  78.                 if (counter == 0)
  79.                 {
  80.                     MessageBox.Show("You Have Exceeded The Acceptable Amount Of Trials" + Environment.NewLine +
  81.                     "Program Terminated.");
  82.                     connection.Close();
  83.                     Application.Exit();
  84.                 }
  85.                 connection.Close();
  86.             }
  87.             catch(Exception ex)
  88.             {
  89.                 MessageBox.Show("Error " + ex);
  90.                 connection.Close();
  91.             }
  92.         }
  93.  
  94.         private void Form1_Load(object sender, EventArgs e)
  95.         {
  96.             textBox1.Select();
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement