SMO C#更改现有存储过程参数失败

SMO C#更改现有存储过程参数失败,c#,sql,smo,C#,Sql,Smo,我试图用C#中的SMO以编程方式更改存储过程。除了这一步之外,一切似乎都正常。连接和存储过程连接正常。请帮忙,为什么它不改变我的数据库 using System; using System.Data; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common; namespace DBChecker { public class A { publi

我试图用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();

        }

    }

}

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

将代码段更改为如下代码段:

StoredProcedureParameter param;
param = new StoredProcedureParameter(sp, "@alstyp");
sp.Parameters.Add(param);  // Add the parameter as a parameter to the StoredProcedure
这样,当您调用
sp.Alter()
时,它将实际应用代码

参考: