Type 'StoredProcedures' already defines a member called 'AddNumber' with the same parameter types

StackOverflow https://stackoverflow.com/questions/16117845

  •  11-04-2022
  •  | 
  •  

Question

I have created a Visual C# SQL CLR database project. I added two stored procedure: test1.cs and test2.cs. I have a function in test1.cs as AddNumber.

In test2.cs, I want to have a function with the same name which will do something else, but this is the error I get after I rebuild it:

Type 'StoredProcedures' already defines a member called 'AddNumber' with the same parameter types.

The code for test1 looks like this:

public partial class StoredProcedures {
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void test1(string Path) {
        // Put your code here
    }

    private static void AddNumbers(int a, int b) {

    }
};

test 2

public partial class StoredProcedures {
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void test2(string Path) {
        // Put your code here
    }

    private static void AddNumbers(int a, int b) {

    }
};

I do not want to change the names as I have multiple other functions with the same name. Also, there are many variables with the same name in both files, too.

Was it helpful?

Solution

I tried adding a namespace in one of the file but while deploying i get an error "Incorrect syntax" The code to deploy them in sql server is:

CREATE PROCEDURE ADD_NUMBER ( @Path nvarchar(400) ) AS EXTERNAL NAME Test.StoredProcedures.test1 go

I'm not sure why you removed this bit from your question. If class StoredProcedures is in the namespace Namespace and in the assembly Test, you can use EXTERNAL NAME Test.[Namespace.StoredProcedures].test1. The full type name needs to be a single identifier as far as SQL is concerned, and since the full type name includes a dot, it needs to be quoted.

Note that this will cause multiple different types to have the same name StoredProcedures. There is no problem with that, but it has the same effect as renaming StoredProcedures to separate classes StoredProcedure1 and StoredProcedure2, and renaming is one thing you wanted to avoid (although I do not see why).

OTHER TIPS

I think you want to interface threw call your class method. It's an easy and structured way.

By implementing the interface explicitly, like this:

public interface ITest {
    void Test();
}
public interface ITest2 {
    void Test();
}
public class Dual : ITest, ITest2
{
    void ITest.Test() {
        Console.WriteLine("ITest.Test");
    }
    void ITest2.Test() {
        Console.WriteLine("ITest2.Test");
    }
}

Note that the functions are private to the class, so you have to assign an object of that class to a variable defined as that particular interface:

var dual = new Dual();
ITest test = dual;
test.Test();
ITest2 test2 = dual;
test2.Test();

My answer's starting point is an answer to Stack Overflow question Inheritance from multiple interface with the same method name in C#.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top