ASP.NET中SQL命令的类 [英] Class for sql command in asp.net

查看:65
本文介绍了ASP.NET中SQL命令的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为asp.net中的每个sql命令创建类.我不想使用存储过程,只是为每个sql命令创建类文件.我该怎么做.
请帮助我...提前感谢

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

推荐答案

你好,
我从这个问题中得到的确切信息是,您只是不想创建存储过程,而是想要将查询传递给所创建的函数.

但这并不是您想通过创建类(例如数据访问层)来完成此工作的确切方式,您可以在其中放置负责所有事情的所有类,如:

创建一个conClass.cs,或者您可以将一个类项目添加到正在使用的应用程序中:创建一个将打开与数据库的连接的类.现在,如果您想要一个将查询作为字符串并执行的函数,请执行以下操作:

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:

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")



此代码将执行传递的Querry,无论它是什么(保存,更新,删除等).像这样,您可以创建其他方法,如绑定下拉列表,Datagrid视图或任何其他控件.

并返回一个数组:



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 :

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



如果这不是您期望的答案,请告诉我,如果是,那么我将为您提供C#代码.

最好的问候.



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.


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.


亲爱的朋友
BTW在prevoius代码中(对于第一个注释),无需编写:
Dear friend
BTW in the prevoius code (For the first comment) no need to write :
cmd.ExecuteNonQuery();


要了解有关此概念的更多信息,请单击:
http://www.nigelwhitworth.com/VB/whatisthedifferencebetweenexecutereaderexecutescalarexecutexmlreaderandexecute /www.nigelwhitworth.com/VB/executereaderexecutescalarexecutexmlreaderandexecutenonquerydafillobjdt.php"target =" _ blank"title =" New Window> ^ ]
现在,与前面的案例一样,您将memberId作为字符串返回,但是在这里它将选择Member的所有数据,因此在这里您必须返回数据集或dataredaer实例:就像


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;
        }


并在表单或页面上需要数据
数据集ds = SelectMember(成员名称");
但是在这里您必须记住一件事,因为您在此处尝试根据用户名选择成员,因此可能存在两个以上同名成员的情况.

因此,如果要获取成员的数据,请始终使用ID或带有主键的字段进行搜索
最好的问候.


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.


这篇关于ASP.NET中SQL命令的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆