用户定义的函数,将执行sql storeprocedure ...? [英] user defined function that will execute a sql storeprocedure...?

查看:74
本文介绍了用户定义的函数,将执行sql storeprocedure ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个看起来像这样的函数:


I want to develop a function which looks like this:


function name("store procedure name",parameter array)
{
}


此功能的主要功能是执行存储过程,并将sql参数从参数数组传递给该过程.

意味着如果存储过程将采用4个参数,则参数数组将为4个元素,如果其等于或大于5个元素,则它将为该元素.


它将适用于所有INSERT,UPDATE,DELETE


实际上我想要这样....

用户将不会传递参数名称


如果我的Store过程将使用一个名为@ ID,@ NAME
的参数
然后功能会像这样


This functions main functionality is to execute a store procedure, and pass sql parameter to the procedure from parameter array.

Means if store procedure will take 4 parameter then the parameter array will be 4 element and if it will of 5 or more then it will be of that.


And it will be work for all INSERT,UPDATE,DELETE


what actually i wanted like this....

the parameter name will not passed by the user


IF my Store procedure will take a parameter called @ID,@NAME

then the function will be like this

...
object [] obj = new object[2];
obj[0] = txtID.text;
obj[1] = txtName.text;

intertFunction("SP_INSERT",obj);



在此先感谢... !!!

Tejas Vaishnav



Thanks in advance...!!!

Tejas Vaishnav

推荐答案

这并不困难:只需将参数设置为KeyValuePairs,然后执行即可!
This isn''t difficult: just set up the parameters as KeyValuePairs, and do it!
private void myfunc(string s, params KeyValuePair<string, object>[] pars)
    {
    foreach (KeyValuePair<string, object> kvp in pars)
        {
        ...
        }
    }


我找到了想要的东西...

I found what i Wanted...

public void Registration(string storeProcedureNmae, params object[] parameterValue)
        {
            try
            {
                string st = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString.ToString();
                sqlCon = new SqlConnection(st);
                if (sqlCon != null || sqlCon.State != ConnectionState.Open)
                    sqlCon.Open();
                SqlCommand cmd = sqlCon.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = storeProcedureNmae;

                SqlCommandBuilder.DeriveParameters(cmd);
                int startIndex = 0;
                foreach (SqlParameter parameter in cmd.Parameters)
                {
                    if (parameter.Direction == ParameterDirection.Input || parameter.Direction == ParameterDirection.InputOutput)
                    {
                        parameter.Value = parameterValue[startIndex];
                        startIndex++;
                    }
                }
                cmd.ExecuteNonQuery();
            }
            catch (Exception exe)
            {
                throw exe;
            }
        }






这将从我们的aspx表单中调用,就像这样...






this will be called from our aspx form like this...

object[] myObj = new object[3];
myObj[0] = .....
.....
classObj.Registration("SP_REGISTRATION_PROCESS", myObj);


这篇关于用户定义的函数,将执行sql storeprocedure ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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