SMO C#alter在EXISTING存储过程参数上失败

时间:2015-03-13 14:56:37

标签: 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
    {

        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();

        }

    }

}

1 个答案:

答案 0 :(得分:0)

您未应用更改的原因是虽然您创建了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()时实际应用代码。

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