Trang DBClass.cs
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(strSQL, con);
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(strSQL, con);
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 to, string strSubject, string 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 strSQL, SqlParameter[] thamso)
{
SqlConnection con = taoketnoi();
try
{
SqlCommand cmd = new SqlCommand(strSQL, con);
for (int i = 0; i < thamso.Length; i++)
{
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 strSQL, SqlParameter[] thamso)
{
try
{
SqlConnection con = taoketnoi();
SqlCommand cmd = new SqlCommand(strSQL, con);
for (int i = 0; i < thamso.Length; i++)
{
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 (x = 0; x < chieudai; x++)
{
myIntArray[x] = System.Convert.ToInt32(autoRand.Next(0, 9));
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 = 0; i < PasswordLength; i++)
{
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 = 0; i < PasswordLength; i++)
{
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
}
return new string(chars);
}
public static string chay_video_Yotube(string sID, int iWith, int 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[i - 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 id, string 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.NVarChar, 20));
Cmd.Parameters.Add(new SqlParameter("@pass", SqlDbType.NVarChar, 250));
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.NVarChar, 20));
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.NVarChar, 250));
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 id, string 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.Int, 20));
Cmd.Parameters.Add(new SqlParameter("@codeLosPass", SqlDbType.NVarChar, 250));
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