Hello
i need help plz , How can i create Data layer Class that make me work with object from this class as aDataBase Table .
for Example: ClassDB MyClass = new ClassDB("dbo.Tbl1");
and from this class i can handle any task with the Data Base (Insert -Update-Delete-Select -Etc......)
hope that i can explain it well
waiting for the answer
Best Regards
Sam HobbsPosted Jul 11, 2011, 1:30 PM
Mahesh ChandPosted Jul 11, 2011, 1:06 PM
No means, it's not a simple task. Every table has different name, column names, types and so on. So you can't just create a class that deals with all the tables.
YES! means, you can create a generic class called DBHelper that will execute SQL queries etc. So this class can have a method called ExecuteSQL that takes a SQL statement. In your code, you can create a SELECT statement, DELETE or UPDATE or any other SQL statements and call that statement using this class.
I have also seen some code where all SQL statements are generated from XML files. You just have to update the XML but that's just too much!
Dorababu MekaPosted Jul 11, 2011, 12:05 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DAO
{
public class Login
{
private string m_strUserName;
private string m_strPassword;
private SqlCommand m_oCmd;
private SqlDataReader m_oDR;
private SqlConnection m_oConn;
private DataSet m_oDataSet;
private bool m_bFlag = false;
public string UserName
{
get
{
return m_strUserName;
}
set
{
m_strUserName = value;
}
}
public string Password
{
get
{
return m_strPassword;
}
set
{
m_strPassword = value;
}
}
public Login()
{
this.m_oCmd = new SqlCommand();
this.m_oConn = new SqlConnection();
this.m_oDataSet = new DataSet();
}
public bool Authenticate()
{
m_bFlag = false;
m_oConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
try
{
if (m_oConn.State != ConnectionState.Open)
{
m_oConn.Open();
}
m_bFlag = false;
m_oCmd = new SqlCommand("uspLogin", m_oConn);
m_oCmd.Connection = m_oConn;
m_oCmd.CommandType = CommandType.StoredProcedure;
m_oCmd.Parameters.AddWithValue("@UserName", UserName);
m_oCmd.Parameters.AddWithValue("@Password", Password);
m_oDR = m_oCmd.ExecuteReader();
if (m_oDR.Read())
{
m_bFlag = true;
}
}
catch (SqlException oSqlEx)
{
}
finally
{
m_oConn.Close();
}
return m_bFlag;
}
}
}
In aspx page on button you should write like this
DAO.Login objLogin = new DAO.Login();
protected void btnLogin_Click(object sender, EventArgs e)
{
objLogin.UserName = txtUsername.Text;
objLogin.Password = txtPassword.Text;
if (objLogin.Authenticate())
{
Response.Redirect("User.aspx");
}
else
{
lblInvalid.Visible = true;
}
}
Ehab ElfahamPosted Jul 11, 2011, 10:56 AM
Sam HobbsPosted Jul 10, 2011, 9:38 PM
The "3 Tier Archtecture" is popular. Also using "Entites" is the new thing.