使用数组创建类 [英] Create class using array

查看:93
本文介绍了使用数组创建类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在asp.net c#中创建一个用于更新命令的类文件.

如何使用数组创建更新命令?
我不能静态地声明更新命令,因为它对于不同的表是不同的.

请帮助我如何创建它以及如何声明数组并在我们的asp.net页面中调用该函数.

在此先感谢.

I want to create a class file in asp.net c# for update command.

How can I create update command using array?
I can''t declare the update command statically because its different for different tables.

Please help me how to create this and how to declare array and call the function in our asp.net page.

Thanks in advance.

推荐答案

以下是执行此操作的通用例程:
Here is a generic routine to do it:
/// <summary>
/// Update a database entry with new field data
/// </summary>
/// <param name="table">Name of table to update</param>
/// <param name="fieldsList">Comma separated list of fields to be updated</param>
/// <param name="condition">If not null, condition of form "ID=@1".
/// The first parameter in the ''parameters'' list will be the condition ''@1''</param>
/// <param name="parameters">List of parameters to update with</param>
public static void Update(string table, string fieldsList, string condition, params object[] parameters)
    {
    StringBuilder sb = new StringBuilder(1000);
    sb.Append(string.Format("UPDATE {0} SET ", table));
    string[] fields = fieldsList.Split('','');
    int firstSetting = (string.IsNullOrEmpty(condition) ? 0 : 1);
    if ((fields.Length + firstSetting) > parameters.Length)
        {
        throw new ArgumentException("Not enough parameters for this field list");
        }
    string prefix = "";
    for (int i = 0; i < fields.Length; i++)
        {
        firstSetting++;
        sb.Append(string.Format("{0}{1}=@{2}", prefix, fields[i],firstSetting.ToString()));
        prefix = ",";
        }
    sb.Append(" " + (string.IsNullOrEmpty(condition) ? "" : "WHERE " + condition));
    Execute(sb.ToString(), parameters);
    }
/// <summary>
/// Execute an Sql command with parameters, and return the command
/// object for later use.
/// Parameters are added as "@1", "@2", etc.
/// </summary>
/// <param name="sql">SQL command</param>
/// <param name="parameters">List of parameters to add.</param>
/// <returns>Constructed SQL object</returns>
private static SqlCommand Execute(string sql, params object[] parameters)
    {
    SqlCommand cmd = new SqlCommand(sql, con);
    Execute(cmd, parameters);
    return cmd;
    }
/// <summary>
/// Execute an Sql command with parameters
/// Parameters are added as "@1", "@2", etc.
/// </summary>
/// <param name="cmd">SQL command object</param>
/// <param name="parameters">List of parameters to add.</param>
private static void Execute(SqlCommand cmd, params object[] parameters)
    {
    AddParameters(cmd, parameters);
    cmd.ExecuteNonQuery();
    }
/// <summary>
/// Add all the parameters to the SQL Command object
/// Parameters are added in order as ''@1'', ''@2'', etc.
/// Replaces null with DBNull.Value
/// </summary>
/// <param name="cmd">SQL Command to add to</param>
/// <param name="parameters">List of parameters to add</param>
private static void AddParameters(SqlCommand cmd, object[] parameters)
    {
    int i = 1;
    cmd.Parameters.Clear();
    foreach (object o in parameters)
        {
        if (o == null)
            {
            cmd.Parameters.AddWithValue("@" + i++, DBNull.Value);
            }
        else
            {
            cmd.Parameters.AddWithValue("@" + i++, o);
            }
        }
    }


如果我对您的理解正确,那么您只想使用一个函数来更新几个不同的表.如果您在数据库中使用具有以下功能的存储过程,则可以完成此操作:

If I understand you correctly, you want to update several different tables using only one function. This can be done if you are using Stored Procedures in your DataBase with a function like this:

public int ExecuteNonQuery(string connectionString, string spName, Dictionary<string,object> parameters)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmdExecute = new SqlCommand(spName, con);
                cmdExecute.CommandType = System.Data.CommandType.StoredProcedure;
                foreach (KeyValuePair<string, object> kvp in parameters)
                {
                    cmdExecute.Parameters.AddWithValue(kvp.Key, kvp.Value);
                }
                con.Open();
                return cmdExecute.ExecuteNonQuery();
            }
        }



其中spName是存储过程的名称,Dictionary< string,>包含您的参数,因此您可以这样称呼它:



where spName is the name of the Stored Procedure, and the Dictionary<string,> contains your parameters, so you would call it like this:

public void Update()
        {
            Dictionary<string,> parameters = new Dictionary<string,>();
            parameters.Add("@Param1", param1Value);
            parameters.Add("@Param2", param2Value);
            int rowsAffected = ExecuteNonQuery("yourConnectionString", "yourStoredProcName", parameters);
        }



希望对您有帮助



Hope this helps


这篇关于使用数组创建类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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