SHARE
TWEET

My first C# code

Ortund Jul 6th, 2012 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using MySql.Data.MySqlClient;
  4.  
  5. /// <summary>
  6. /// Global database class contains general database functions
  7. /// </summary>
  8. public class Database
  9. {
  10.     private string connstr = "Server=localhost;database=msg;user=root;pass=Ly@12157114";
  11.    
  12.     public MySqlDataReader GetReader(String sql, List<MySqlParameter> args)
  13.     {
  14.         MySqlConnection conn = new MySqlConnection(connstr);
  15.         MySqlCommand cmd = new MySqlCommand(sql, conn);
  16.  
  17.         if (args.Count != 0)
  18.         {
  19.             cmd.Parameters.AddRange(args.ToArray());
  20.         }
  21.  
  22.         MySqlDataReader dr = cmd.ExecuteReader();
  23.         return dr;
  24.     }
  25.  
  26.     public void DoCommand(string sql, List<MySqlParameter> args)
  27.     {
  28.         MySqlConnection conn = new MySqlConnection(connstr);
  29.         MySqlCommand cmd = new MySqlCommand(sql, conn);
  30.  
  31.         if (args.Count != 0)
  32.         {
  33.             cmd.Parameters.AddRange(args.ToArray());
  34.         }
  35.  
  36.         conn.Open();
  37.         cmd.ExecuteNonQuery();
  38.         conn.Close();
  39.     }
  40. }
  41.  
  42. using System;
  43. using System.Collections.Generic;
  44. using MySql.Data.MySqlClient;
  45.  
  46. public partial class Login : System.Web.UI.Page
  47. {
  48.     protected void Page_Load(object sender, EventArgs e)
  49.     {
  50.        
  51.     }
  52.  
  53.     protected void btnLogin_Click(object sender, EventArgs e)
  54.     {
  55.         Database db = new Database();
  56.  
  57.         string sql = "select * from users where email = @email and passcode = @pass";
  58.  
  59.         List<MySqlParameter> args = new List<MySqlParameter>();
  60.         args.Add(new MySqlParameter("@email", txtEmail.Text));
  61.         args.Add(new MySqlParameter("@pass", txtPass.Text));
  62.  
  63.         MySqlDataReader dr = db.GetReader(sql, args);
  64.  
  65.         if (dr.HasRows)
  66.         {
  67.             while (dr.Read())
  68.             {
  69.                 Session["userid"] = "{0}";
  70.                 Response.Redirect("Inbox.aspx");
  71.             }
  72.         }
  73.         else
  74.         {
  75.             lblerr.Text = "Invalid username/password";
  76.         }
  77.     }
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top