Tài liệu ôn tập, ôn thi. Tài liệu luyện thi

Trang DBClass.cs

No comments

    
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;

using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Data.Sql;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;

/// <summary>
/// Summary description for DBClass
/// </summary>
public static class DBClass
{

    
public static SqlConnection taoketnoi()
    {
        
string chuoi "Data Source=.\\SQLEXPRESS;Initial Catalog=tendatabase;Integrated Security=True;Max Pool Size=300;Min Pool Size=5";
        
SqlConnection con = new SqlConnection(chuoi);
        
con.Open();
        return 
con;
    }

    
public static DataTable TruyVan_TraVe_DataTable(string strSQL)
    {
        
SqlConnection con taoketnoi();
        try
        {
            
SqlDataAdapter da = new SqlDataAdapter(strSQLcon);
            
DataTable dt = new DataTable();
            
da.Fill(dt);
            return 
dt;
        }
        
finally
        
{
            
//'finally' block is ALWAYS get called.
            
con.Close();
            
con.Dispose();
        }
        
    }
    
public static int TruyVan_XuLy(string strSQL)
    {
        
SqlConnection con taoketnoi();
        try
        {
            
            
SqlCommand cmd = new SqlCommand(strSQLcon);
            
int sodong cmd.ExecuteNonQuery();
            return 
sodong;
        }
        catch (
Exception e)
        {
            return -
1;
        }
        
finally
        
{
        
//'finally' block is ALWAYS get called.
        
con.Close();
        
con.Dispose();
        }
        
    }
    
public static bool SendMail_By_Gmail(string tostring strSubjectstring strContent)
    {
        
MailMessage mail = new MailMessage();

        
mail.To.Add(to);
        
mail.Subject strSubject;
        
mail.IsBodyHtml true;
        
mail.Body strContent;
        
mail.From = new MailAddress("minhtn1982@gmail.com");
        try
        {
            
SmtpClient client = new SmtpClient();
            
client.DeliveryMethod SmtpDeliveryMethod.Network;
            
client.EnableSsl true;
            
client.Host "smtp.gmail.com";
            
client.Port 587;
            
// khai báo tài khoản và mat khẩu gừi mail
            
NetworkCredential credentials = new NetworkCredential("123456789@gmail.com""123456");
            
client.UseDefaultCredentials false;
            
client.Credentials credentials;
            
client.Send(mail); // thuc hien gui mail
            
return true;
        }
        catch (
Exception ex)
        {
            
Console.WriteLine("Loi " ex.ToString());
            return 
false;
        }

    }
    
public static int Truyvan_Xuly_co_Thamso(string strSQLSqlParameter[] thamso)
    {
        
SqlConnection con taoketnoi();
        try
        {
            
            
SqlCommand cmd = new SqlCommand(strSQLcon);
            for (
int i 0thamso.Lengthi++)
            {
                
cmd.Parameters.Add(thamso[i]);
            }
            
int sodong cmd.ExecuteNonQuery();
            return 
sodong;
           

        }
        catch (
Exception e)
        {
            return -
1;
        }
        
finally
        
{
            
//'finally' block is ALWAYS get called.
            
con.Close();
            
con.Dispose();
        }
    }
    
public static DataTable Truyvan_TraVe_Table_co_Thamso(string strSQLSqlParameter[] thamso)
    {
        try
        {
            
SqlConnection con taoketnoi();
            
SqlCommand cmd = new SqlCommand(strSQLcon);
            for (
int i 0thamso.Lengthi++)
            {
                
cmd.Parameters.Add(thamso[i]);
            }
            
SqlDataAdapter da= new SqlDataAdapter(cmd);
            
DataTable dt = new DataTable();
            
da.Fill(dt);
            return 
dt;
            
con.Close();
            
con.Dispose();
        }
        catch (
Exception e)
        {
            return 
null;
        }

    }
    
public static string tao_chuoi_so_ngaunhien(int chieudai)
    {
        
string randomStr "";
        
int[] myIntArray = new int[chieudai];
        
int x;
        
//That is to create the random # and add it to our string
        
Random autoRand = new Random();
        for (
0chieudaix++)
        {
            
myIntArray[x] = System.Convert.ToInt32(autoRand.Next(09));
            
randomStr += (myIntArray[x].ToString());
        }
        return 
randomStr;
    }



    
public static string Truyvan_TraVe_Table_co_Thamso(string p)
    {
        throw new 
NotImplementedException();
    }


    

    
//Hàm này trả về chuỗi Password ngẫu nhiên [a-z], [0-9]
    
public static string CreateRandomPassword(int PasswordLength)
    {
        
string _allowedChars "abcdefghijkmnopqrstuvwxyz0123456789";
        
Random randNum = new Random();
        
char[] chars = new char[PasswordLength];
        
int allowedCharCount _allowedChars.Length;

        for (
int i 0PasswordLengthi++)
        {
            
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new 
string(chars);
    }

    
public static string taochuoi(int PasswordLength)
    {
        
string _allowedChars "abcdefghijkmnopqrstuvwxyz0123456789";
        
Random randNum = new Random();
        
char[] chars = new char[PasswordLength];
        
int allowedCharCount _allowedChars.Length;

        for (
int i 0PasswordLengthi++)
        {
            
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new 
string(chars);
    }

    
public static string chay_video_Yotube(string sIDint iWithint iHeight)
    {
        
string strHTML "";
        
strHTML += "<iframe title='YouTube video player'";
        
strHTML += " width='" iWith.ToString() + "' height='" iHeight.ToString() + "'";
        
strHTML += " src='http://www.youtube.com/embed/" sID "' ";
        
strHTML += " frameborder='0' allowfullscreen></iframe>";
        return 
strHTML;
    }

    
public static string returnUrl(string temp1)
    {
        
string[] word temp1.Split('/');
        
int i word.Length;
        return 
word[1].ToString();
    }
Ø Tạo một class KiemtraUserPass.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.Sql;
using System.Data.SqlClient;

/// <summary>
/// Summary description for KiemtraUserPass
/// </summary>
public class KiemtraUserPass
{
    
    
public int Kiemtra(string idstring pass)
    {
        
SqlConnection Conn DBClass.taoketnoi();
        if (
Conn.State == ConnectionState.Closed)
        {
            
Conn.Open();
        }
        try
        {
            
SqlCommand Cmd = new SqlCommand("select * from users where username=@id and password=@pass"Conn);
            
Cmd.Parameters.Add(new SqlParameter("@id"SqlDbType.NVarChar20));
            
Cmd.Parameters.Add(new SqlParameter("@pass"SqlDbType.NVarChar250));
            
Cmd.Parameters["@id"].Value id;
            
Cmd.Parameters["@pass"].Value pass;
            if (
Cmd.ExecuteReader().HasRows)
                return 
1;
            else
                return 
0;
        }
        
finally
        
{
            
//'finally' block is ALWAYS get called.
            
Conn.Close();
            
Conn.Dispose();
        }

    }

    
public int KiemtraUser(string id)
    {
        
SqlConnection Conn DBClass.taoketnoi();
        if (
Conn.State == ConnectionState.Closed)
        {
            
Conn.Open();
        }
        try
        {
            
SqlCommand Cmd = new SqlCommand("select * from users where username=@id"Conn);
            
Cmd.Parameters.Add(new SqlParameter("@id"SqlDbType.NVarChar20));
            
Cmd.Parameters["@id"].Value id;
            if (
Cmd.ExecuteReader().HasRows)
                return 
1;
            else
                return 
0;
        }
        
finally
        
{
            
//'finally' block is ALWAYS get called.
            
Conn.Close();
            
Conn.Dispose();
        }
    }

    
public int KiemtraEmail(string id)
    {
        
SqlConnection Conn DBClass.taoketnoi();
        if (
Conn.State == ConnectionState.Closed)
        {
            
Conn.Open();
        }
        try
        {
            
SqlCommand Cmd = new SqlCommand("select * from users where Email=@Email"Conn);
            
Cmd.Parameters.Add(new SqlParameter("@Email"SqlDbType.NVarChar250));
            
Cmd.Parameters["@Email"].Value id;
            if (
Cmd.ExecuteReader().HasRows)
                return 
1;
            else
                return 
0;
        }
        
finally
        
{
            
//'finally' block is ALWAYS get called.
            
Conn.Close();
            
Conn.Dispose();
        }
    }

    
public int Laypasswordmoi(int idstring chuoi)
    {
        
SqlConnection Conn DBClass.taoketnoi();
        if (
Conn.State == ConnectionState.Closed)
            {
                
Conn.Open();
            }
        try
        {
            
SqlCommand Cmd = new SqlCommand("select * from users where idUser=@idUser and codeLosPass=@codeLosPass"Conn);
            
Cmd.Parameters.Add(new SqlParameter("@idUser"SqlDbType.Int20));
            
Cmd.Parameters.Add(new SqlParameter("@codeLosPass"SqlDbType.NVarChar250));
            
Cmd.Parameters["@idUser"].Value id;
            
Cmd.Parameters["@codeLosPass"].Value chuoi;
            if (
Cmd.ExecuteReader().HasRows)
                return 
1;
            else
                return 
0;
        }
        
finally
        
{
            
//'finally' block is ALWAYS get called.
            
Conn.Close();
            
Conn.Dispose();
        }
    }
}

No comments :

Post a Comment