【问题标题】:SMO C# alter fail on EXISTING stored procedure parameterSMO C# alter fail on EXISTING 存储过程参数
【发布时间】:2015-03-13 14:56:37
【问题描述】:

我正在尝试在 C# 中使用 SMO 以编程方式更改存储过程。除了改变步骤之外,一切似乎都有效。连接和存储过程连接都很好。请帮忙,为什么它没有改变我的数据库?

using System;
using System.Data;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace DBChecker
{
    public class A
    {

        public static void Main(string[] args)
        {

            String sqlServerLogin = "*****";
            String password = "****";
            String instanceName = "";
            String remoteSvrName = "****";


            ServerConnection srvConn2 = new ServerConnection(remoteSvrName);
            srvConn2.LoginSecure = true;
            Server srv3 = new Server(srvConn2);
            Console.WriteLine(srv3.Information.Version);  
            Console.WriteLine("Connection Established");

            Database db;
            db = srv3.Databases["decision"];
            Console.WriteLine("DB Connection Established");

            StoredProcedure sp;
            sp = new StoredProcedure(db,"copy_alias_by_type");
            Console.WriteLine("SP Connection Established");

            sp.TextMode = false;
            sp.AnsiNullsStatus = false;
            sp.QuotedIdentifierStatus = false;

            StoredProcedureParameter param;
            param = new StoredProcedureParameter(sp, "@alstyp");

            param.DataType = DataType.Char(100);
            sp.Refresh();
            sp.Alter();

            Console.WriteLine("DataType Changed");
            Console.ReadKey();

        }

    }

}

【问题讨论】:

    标签: c# sql smo


    【解决方案1】:

    您的更改未得到应用的原因是,尽管您创建了 param StoredProcedureParameter 并引用了 sp StoredProcedure,但您实际上从未将其添加为存储过程的参数。 (别担心,当我看到/意识到它时,我也有一个“呼?”的时刻)。

    将您的代码 sn-p 更改为如下所示的 sn-p:

    StoredProcedureParameter param;
    param = new StoredProcedureParameter(sp, "@alstyp");
    sp.Parameters.Add(param);  // Add the parameter as a parameter to the StoredProcedure
    

    这将使当您调用sp.Alter() 时,它实际上会应用代码。

    参考:MSDN: Creating, Altering, and Removing Stored Procedures

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      相关资源
      最近更新 更多