通用功能的ASP.net类 [英] ASP.net class for common function

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

问题描述

请参见以下代码:
但是我想让参数通过我们的要求基础,如果我有其他要求的形式,例如名字,姓氏,年龄,日期,性别等,那么我将无法访问此功能.所以我想通过参数作为我们的要求依据.那么我该怎么做.请帮助我.........提前感谢.

See the bellow code:
But i want the parameters pass our requirement basis, if i have required in another forms, firstname,lastname,age,date,sex etc so i can''t access this function. so i want to pass parameters our requirement basis. So how can i do that. Please help me.........Thanks in advance.

public int Insert(string firstName, string lastName, int age)
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand dCmd = new SqlCommand("InsertData", conn);
            dCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dCmd.Parameters.AddWithValue("@firstName", firstName);
                dCmd.Parameters.AddWithValue("@lastName", lastName);
                dCmd.Parameters.AddWithValue("@age", age);
                return dCmd.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                dCmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }

推荐答案

如果您已在App_code文件夹中获取一个类文件,则在一个类文件中创建此功能,然后复制粘贴此函数并创建一个该实例您要在其中调用此函数的其他页面类中的类....
Create this fuction in one class file if you have taken one class file in App_code folder then copy paste this function and create one instance of this class in other page class at where you want to call this function....
public int Insert(string procedureName, SortedList<string, object> parameter = null)
{
    _cmd = new SqlCommand(procedureName, _con);
    _cmd.CommandType = CommandType.StoredProcedure;
    if (parameter != null)
    {
        for (int l = 0; l < parameter.Count; l++)
        {
            _cmd.Parameters.AddWithValue(parameter.Keys[l], parameter.Values[l]);
        }
    }
    int _rowAffected;
    try
    {
        if (_con.State == ConnectionState.Closed)
            _con.Open();
        _rowAffected = _cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
    finally
    {
        if (_con.State == ConnectionState.Open)
            _con.Close();
    }
    return _rowAffected;
}



现在,按以下方式调用此方法...



Now, Invoke this method as following...

//first, create an object of that class... here, assume we have one object named obj
var _sl = new SortedList<string, object>();
_sl.Add("@firstName", firstName);
_sl.Add("@lastName", lastName);
_sl.Add("@age", age);
int _affectedRow = obj.Insert("InsertData",_sl);


我对Sandeep Mewara和parmar_punit@yahoo.co.in投了5票. Parmar投票5的原因是它的简单性和正确的运作方式.我对Sandeep Mewara的5票是因为他的概念也很出色.以下是说明.

字典是一个简单而不错的主意,但是如果某些用户添加_sl.Add("@MyHeart", lovepulse);. If this should not happen then the caller should know the table structure prior 怎么办.什么是紧耦合?

但是对象模型给出了defined contract.有人将对象模型误解为单个对象,并否定了Sandeep的答案.让我们看看在这种情况下,paramar和Sandeep的回答如何共同起作用.

对象具有新颖的概念Encapsulation

让我们有一个对象模型

I vote 5 for Sandeep Mewara and parmar_punit@yahoo.co.in. The reason for vote 5 for parmar is because of its simplicity and works correctly. My vote of 5 for Sandeep Mewara is because his concept works great too. Following are the explanations.

Dictionary is a simple and nice idea, but what if some users add _sl.Add("@MyHeart", lovepulse);. If this should not happen then the caller should know the table structure prior . What a tight coupling?

But a object model gives a defined contract. Somebody misunderstood object model as single object and downvoted Sandeep''s answer. Let us have a look at how paramar''s and Sandeep''s answer together helps in this situation.

Objects have a novel concept Encapsulation

Let us have an object model

 public Interface IInsertableObject
{
    public  SortedList<string,object>  getInsertData()
}
public class CommonObject:IInsertableObject
{
    public string FirstName{get;set;}
    public string LastName{get;set;}
    public int Age{get;set;} 
    public virtual SortedList<string,object> getInsertData()
    {
        // Here you will be having parmar's sorted list created based on the                 
        //object properties. Now this logic is encapsulated and no rubish                 
        //other than object properties are allowed
    }
}
public class ExtendedObjectBasedOnTable:CommonObject
{
    public DateTime birthDate{get;set;}
    public SexEnum Sex{get;set;}
        public overrides SortedList<string,object> getInsertData()
    {
        // Here you will be having parmar's sorted list created based on the                 
        //object properties. Now this logic is encapsulated and no rubish                 
        //other than object properties are allowed
    }
}



所以你的功能就像



So your function will be like

public int Insert(IInsertableObject insertableObject)
{
    SortedList<string,object> insertData=insertableObject.getInsertData();
    //use the sorted list as shown by Parmar.
}
insert(new CommonObject{ set the properties here.....})
insert(new ExtendedObjectBasedOnTable{ set the properties here.....})



因此,Sandeep的模态(封装和委派)+ Parmar的逻辑提供了更好的解决方案.调用者不必担心持久性逻辑,而只需要知道它可以选择哪个对象来匹配其具有的参数.另一层工厂在这里.但是,让我们不要让OP变得更加复杂.

祝您好运



So Sandeep''s modal (Encapsulation and delegation) + Parmar''s logic gives a better solution. The caller doesn''t worry about persistance logic, but only need to know which object it can choose to match the parameters it is having. An another layer of factory goes here. But let us not make things further complex for OP.

Good luck


这听起来像您想要将1到几个参数传递给在数据库中插入数据的方法.

您需要一个对象模型.创建一个类(让我们说个人信息).向其中添加属性,例如名字,姓氏,年龄,性别等.随处使用此对象.在其中填充数据,并将其传递给方法.根据是否存在来处理方法中的数据.

试试吧!
What it sounds like you want to pass 1 to few parameters to a method for inserting data in database.

You need an object model. Create a class (Lets say personal Info). Add the proeprties like firstname, lastname, age, sex, etc to it. Use this object everywhere. Fill the data in it and pass it to the methods. handle the data in the method based on if present or not.

Try!


这篇关于通用功能的ASP.net类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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