Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将内联sql语句更改为存储过程?_C#_Asp.net_Sql - Fatal编程技术网

C# 如何将内联sql语句更改为存储过程?

C# 如何将内联sql语句更改为存储过程?,c#,asp.net,sql,C#,Asp.net,Sql,我有一些带有数组和if循环的内联sql语句。我想在SQLServer2005中将它们全部更改为一个存储过程。 在学习SQL时,我不知道应该如何使用数组和if循环来实现SQL。是否可以将数组发送到存储进程中,或者是否有其他解决方案 请参考我将要更改为SP的代码。提前感谢 public bool TempUpdateMerchantCategories(long LocationID, ArrayList CategoryList, ArrayList ImageData) { try

我有一些带有数组和if循环的内联sql语句。我想在SQLServer2005中将它们全部更改为一个存储过程。 在学习SQL时,我不知道应该如何使用数组和if循环来实现SQL。是否可以将数组发送到存储进程中,或者是否有其他解决方案

请参考我将要更改为SP的代码。提前感谢

public bool TempUpdateMerchantCategories(long LocationID, ArrayList CategoryList, ArrayList ImageData)
{
    try
    {
        int j = 0;
        bool hasImage = false;
        string previousPID = string.Empty;
        bool isFirstPID = true;
        int numberOfSubCategories = 3;
        int pIDCount = 0;
        foreach (string data in CategoryList)
        {
            string pID = data.Split(',')[0];
            if (isFirstPID)
            {
                isFirstPID = false;
                previousPID = pID;
                pIDCount++;
            }
            else
            {
                if (pID != previousPID)
                {
                    previousPID = pID;
                    pIDCount++;
                }
            }
        }
        ArrayList stepsThresholdList = new ArrayList();
        if (pIDCount > 1)
        for (int k = 1; k < pIDCount; k++)
        {
            stepsThresholdList.Add(k * numberOfSubCategories);
        }


        if (CategoryList == null || CategoryList.Count == 0) return true;
        SqlParameter[] parameters = new SqlParameter[]
        {
            new SqlParameter("@LocationID", LocationID)
        };

        //SqlHelper.ExecuteNonQuery(DbConnString, System.Data.CommandType.StoredProcedure, "TempMerchant_Location_Category", parameters);

        string commandText = String.Format("DELETE FROM [TempMerchant_Location_Category] WHERE locationid = @LocationID");
        Debug.Write(commandText);
        SqlHelper.ExecuteNonQuery(DbConnString, CommandType.Text, commandText, parameters);

        for (int i = 0; i < CategoryList.Count; i++)
        {
            if (ImageData.Count > 0 && j < ImageData.Count)
            {
                string imageID = ImageData[j].ToString().Split(',')[0];
                string primaryID = ImageData[j].ToString().Split(',')[1];

                if (primaryID == CategoryList[i].ToString().Split(',')[0])
                if (imageID != "IsDefault")
                hasImage = true;
            }

            if (!hasImage)
            {
                parameters = new SqlParameter[]
                {
                    new SqlParameter("@LocationID", LocationID),
                    new SqlParameter("@PrimaryID", Convert.ToInt32(CategoryList[i].ToString().Split(',')[0])),
                    new SqlParameter("@SecondaryID", Convert.ToInt32(CategoryList[i].ToString().Split(',')[1]))
                };

                //SqlHelper.ExecuteNonQuery(DbConnString, System.Data.CommandType.StoredProcedure, "TempMerchant_Location_Category", parameters);
                commandText = String.Format("INSERT INTO [dbo].[TempMerchant_Location_Category] ([LocationID],[PrimaryID],[SecondaryID])  VALUES (@LocationID,@PrimaryID,@SecondaryID)");
            }
            else
            {
                parameters = new SqlParameter[]
                {
                    new SqlParameter("@LocationID", LocationID),
                    new SqlParameter("@PrimaryID", Convert.ToInt32(CategoryList[i].ToString().Split(',')[0])),
                    new SqlParameter("@SecondaryID", Convert.ToInt32(CategoryList[i].ToString().Split(',')[1])),
                    new SqlParameter("@ImageID", Convert.ToInt64(ImageData[j].ToString().Split(',')[0]))
                };
                // SqlHelper.ExecuteNonQuery(DbConnString, System.Data.CommandType.StoredProcedure, "TempMerchant_Location_Category", parameters);
                commandText = String.Format("INSERT INTO [dbo].[TempMerchant_Location_Category] ([LocationID],[PrimaryID],[SecondaryID],[ImageID])  VALUES (@LocationID,@PrimaryID,@SecondaryID,@ImageID)");
            }
            if (stepsThresholdList.Count > 0 && j < stepsThresholdList.Count)
            if (i == (Convert.ToInt32(stepsThresholdList[j]) - 1))
            j++;
            Debug.Write(commandText);
            SqlHelper.ExecuteNonQuery(DbConnString, CommandType.Text, commandText, parameters);
        }
        return true;
    }


    catch (Exception ex)
    {
        LogError("Error Occurred When Updating TempMerchant Ctegories: LocationID:" + LocationID.ToString(), ex);
        return false;
    }
}

考虑使用#Temp表或表变量作为“数组”。存储过程可以执行循环,但您可能希望集中思考以避免RBAR(逐行处理)处理。

如果您有SQL Server 2008,您可以使用表变量作为存储过程的输入变量,这就是处理数组的方式。在网上的书籍中查找如何做到这一点

至于循环和IF,我建议您尝试使用基于集合的处理,因为它明显更快。有关基于集合的逻辑的想法,请参见:


您可能会特别注意使用CASE语句的示例

将其移动到存储过程的动机是什么?嗯,我的雇主要求我这么做!!:(@Ram-请不要在标题中添加标签(例如,C#,SQL)。我已经为您删除了它们。@Robaticus:对不起,我会记住的!!:)我没有要求为我做这件事。我要求知道如何才能做到这一点。