views:

78

answers:

2

Using VB.Net, C#.Net and SQL Server.

Windows Application

I want to separate a code for 3 Tier Architecture(Presentation Layer, Data Access Layer, Business Logic Layer).

Code.

Form_Load

    Dim cmd As New SqlCommand
    Dim ada As New SqlDataAdapter
    Dim ds As New DataSet

    Con = New SqlConnection("Data Source='" & servername.Text & "';Initial  Catalog='"     & databasename.Text & "';Integrated Security=true")
                    Con.Open()

        cmd = New SqlCommand("Select * from tb1", Con)
        ada = New SqlDataAdapter(cmd)
        ada.Fill(ds, "tb1")
        datagrid2.DataSource = ds.Tables("tb1")

Above code is working, But i want to do a same process by using 3 Tier Architecture.

How to separate my code according to 3 tier Architecture.

Need VB.Net Code Help

A: 

the following codeshould be on your Presentation Layer

 Datagrid2.DataSource = ds.Tables("tb1")

the following layer should be on you Data Access Layer

 Dim cmd As New SqlCommand 
 Dim ada As New SqlDataAdapter 
 Dim ds As New DataSet 

 Con = New SqlConnection("Data Source='" & servername.Text & "';Initial  Catalog='"     & databasename.Text & "';Integrated Security=true") 
 Con.Open() 

and the following code should be onyour Business Logic Layer

cmd = New SqlCommand("Select * from tb1", Con)    
ada = New SqlDataAdapter(cmd)    
ada.Fill(ds, "tb1")

but I strongly recommand tonot use something like it, Use an ORM like NHibernate or EntityFramework or atleast from LinkToSql

Nasser Hadjloo
@Nasser - Thank You for your reply, But this code is look like a simple. How to change this code according to the standard.
Gopal
@Nasser - ORM like NHibernate, or EntityFramework or LinkToSql Means? Am not going to use this same query. I will used a store procedure instead of selecting a value from table. There is any other standard is there?
Gopal
Why would you put SQL objects into a BLL and not the DLL where it belongs?
HardCode
@HardCode, because Gopal wants to use his code in 3 tier and there was no code in bll with his code. unless we create our own method like what Masoud did.
Nasser Hadjloo
+2  A: 

you can make a class library project for every layer except UI layer. Then you must breakdown each process to this 3 layer. for example do every data accessing in Data Access Layer and put every logic in Logic Layer. at the UI you can't work with DAL directly. you should only call methods that defined in BLL.

Note: add these layers to UI by adding dll of per layer as reference to UI Project.

DAL

public DataSet GetData(string serverName,string dataBaseName)
{
    SqlCommand cmd;
    SqlDataAdapter ada;
    DataSet ds = new DataSet();
    SqlConnection Con;

    Con = New SqlConnection("Data Source='" & serverName & "';Initial  Catalog='"  & dataBaseName & "';Integrated Security=true");
     Con.Open();

        cmd = New SqlCommand("Select * from tb1", Con);
        ada = New SqlDataAdapter(cmd);
        ada.Fill(ds, "tb1");
        return ds;
}

BLL

public DataSet GetData(string serverName,string dataBaseName)
{
    DataSet ds = new DataSet();
    ds = DoAction(new DAL().GetData(serverName, dataBaseName)); //do something on data 
    return ds;
}

UI

datagrid2.DataSource = new BLL().GetData(servername.Text,databasename.Text);
datagrid2.DataMember = "tb1";
datagrid2.DataBind();
masoud ramezani
@Masoud. Can you modify my posted code according to that.
Gopal
@Masoud. Am New to 3 tier Architecture. I understand you code. ThankYou. Before starting a 3 tier Architecture. what are steps i have to do like adding class file etc? "add these layers to UI by adding dll of per layer as reference to UI Project" Means?
Gopal
each of your application become a dll file. also you can have this layers in your UI as a class per layer.if this answer is accepted by you ,please accept that for other users use. :)
masoud ramezani
@Masoud - When I try to run your code, It was showing error in "DoAction"
Gopal
DaAction is a sample method that if you need, you can implement that. you can delete that.
masoud ramezani
@Masoud - I pass the ServerName and Database like this datagrid2.DataSource = new BLL().GetData("Server1", "database1");I remove this line "datagrid2.DataBind();" then run nothing display in datagridview, when i add this line datagrid2.databind() It was showing error
Gopal
what is the error?
masoud ramezani
@Masoud - Error is "Databind is not a member of datagridview()"
Gopal
do you have multiple table in your dataset?
masoud ramezani
use this : http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx
masoud ramezani
No Only one table, I convert your code to vb.net, then i run it. There is no error in DAL, BLL only in UL. Only in ul as showing error in "Datagrid2.databind(). Instead of this i used datagrid2.databinding() again it was showing error. How to solve this.
Gopal
your problem is DataGridView control. please see this link.http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx
masoud ramezani
@Masoud. Now data is displaying in datagridview. Thanks
Gopal
I'm glad that can help you.
masoud ramezani