Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I want to create class for every sql command in asp.net. I don't want to use stored procedure just create class file for every sql command. How can i do that.
Please help me...Thanks in advance
Posted

hello there
what exact thing i got from the question is that you just don't want to create stored procedures and want to pass the queries to the functions created.

but this will not be the exact way this stuff suppose to be done by the way you can do that by creating a class (like data Access layer) where you can put all the classes responsible for all the things like :

create a conClass.cs or you can add a class project to the app you are working on : create a class which will open the connection to the database. now if you want a function which will accept a Query as string and will execute it do it like:

VB
Public Sub ExecutQuery(ByVal qry As String)

        Try
            ConOpen()
            cmd = New SqlCommand(qry, con)
            cmd.ExecuteNonQuery()
            ConClose()

        Catch ex As Exception
            ConClose()
            ErrMsgBox(ex.Message)
        End Try

    End Sub


so now to execute a qury you have to write all the time the following code :
create a instance of this class on the form
dim con as new conClass

and where you want to execute the query write :
con.ExecutQuery("QueryHere")


this code will execute the passed querry what ever it will be (save, update, delete etc.). like this way you can create the other methods like to bind the dropdown list, Datagrid view or any other control.

and to return an array :
VB
Public Function Ret_Array(ByVal qry As String, ByRef values() As Integer)

        Dim i As Integer

        Try
            ConOpen()
            cmd = New SqlCommand(qry, con)

            Dim dr As SqlDataReader = cmd.ExecuteReader

            If dr.HasRows = True And IsDBNull(dr) = False Then
                While dr.Read()
                    If Not IsDBNull(dr.Item(0)) Then
                        ReDim Preserve values(i)
                        values(i) = dr.Item(0)
                        i = i + 1
                    End If

                End While
            Else

            End If
            dr.Close()
            ConClose()

        Catch ex As Exception
            ErrMsgBox(ex.Message)
        End Try

        Return Nothing

    End Function


if this is not what you are expecting as an answer just let me know but if yes then i will provide you the C# code.

Best Regards.
 
Share this answer
 
v3
Comments
rahul dev123 14-May-11 1:54am    
I want like this..
public static string MemberId(string tblName)
{

SqlConnection con = Database.GetConnection();
SqlCommand cmd = new SqlCommand("select max(member_id) from tblName", con);
int a = Convert.ToInt32(cmd.ExecuteScalar());
a = a + 1;
string m = a.ToString();
cmd.ExecuteNonQuery();
return m;
}
after creating this class i call the function like this:
Label1.Text = MemberClass.MemberId("members");

but table name not pass in the class.
can you help me for this
IncredibleRam 14-May-11 2:02am    
did you try following
SqlCommand cmd = new SqlCommand("select max(member_id) from" + tblName.Trim () + "", con);

instead of :

SqlCommand cmd = new SqlCommand("select max(member_id) from tblName", con);
rahul dev123 14-May-11 2:07am    
Thanks for Your help its working.
Please tell me any other best procedure to do that for every sql command.
rahul dev123 14-May-11 3:35am    
can you help me for this:
i use this function in a class file but i want to use this command in sqldatareader how can i call it in sqldatareader.
ex:
SqlDataReader dr=cmd.ExecuteReader();
but now this function how can i call???? Because now i have no cmd in this file. its written in only class file.
public static void SelectMembers(string username)
{

SqlConnection con = Database.GetConnection();
SqlCommand cmd = new SqlCommand("select * from members where username=@" + username + "", con);

cmd.ExecuteNonQuery();
}
using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for MyClass
/// </summary>
public class MyClass
{
    public SqlConnection con;
    public SqlCommand cmd;
    public SqlDataAdapter adp;
    public DataSet ds;
	public MyClass()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public void Connection()
    {
        con = new SqlConnection("Connection String");
        con.Open();
    }
    public DataSet FillDate(string MyString)
    {
        Connection();
        adp = new SqlDataAdapter(MyString,con);
        ds = new DataSet();
        adp.Fill(ds);
        return ds;
    }

}


Hope this can helop you. Here just pass the string from page. and write other function in the class.
 
Share this answer
 
Dear friend
BTW in the prevoius code (For the first comment) no need to write :
cmd.ExecuteNonQuery();

to know more about this concept click on :
http://www.nigelwhitworth.com/VB/whatisthedifferencebetweenexecutereaderexecutescalarexecutexmlreaderandexecutenonquerydafillobjdt.php[^]
now as in the previous Case you return memberId as a string but here it will select all the data for the Member so here you have to return the dataset or dataredaer instance : do it like
public DataSet SelectMembers(string username)
        {
            SqlConnection con = Database.GetConnection();
            SqlDataAdapter adp = new SqlDataAdapter("select * from members where username=@" + username + "", con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            return ds;
        }

and on the form or page you want the data
dataset ds = SelectMember("Name of the member");
but here you have to keep one thing in mind that as here you are trying to select the members on the basis of the Username so there may be a possibility that more than two member with the same name exists.

so if you want the data of the member the always use the ID or the field with the primary key for search
best regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900