-5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1254053
  • 755
  • 3
  • 19
  • 55
  • Sorry I did't understand your question . See it http://stackoverflow.com/faq – Ramesh Rajendran Apr 20 '13 at 07:42
  • Thanks, i have two clr procedures and both of them have same name of function let's say "LogException". Both the clr have this function though they work differently. I am not able to build the solution as it says the above error. – user1254053 Apr 20 '13 at 07:47
  • Similarly i have a some global variable defined in these files, like path, etc. This is alos creating the problems. I have to either rename them or will have to create seperate CLR projects. – user1254053 Apr 20 '13 at 07:49
  • See my answer : http://stackoverflow.com/a/16118110/2218635 – Ramesh Rajendran Apr 20 '13 at 08:02

2 Answers2

2

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).

Community
  • 1
  • 1
  • Thank you hvd for the response. I tried your suggestion and still i am not able to deploy the assembly. The error: Incorrect syntax: 'Create Procedure' must be the only statement in the batch. Any idea what am i doing wrong? – user1254053 Apr 20 '13 at 08:02
  • Yes, exactly what the error message is telling you: you have other statements than `CREATE PROCEDURE` in the same batch. Or have you perhaps put two `CREATE PROCEDURE` statements in the same batch? Put them in different batches by separating them with `GO`. –  Apr 20 '13 at 08:07
  • Now the error is : Could not find Type 'Namespace.StoredProcedures' in assembly 'Test'. – user1254053 Apr 20 '13 at 08:07
  • I have no idea how you named your namespace, so I picked `Namespace` as the namespace name. You need to change it to whatever it is you used. –  Apr 20 '13 at 08:08
  • This is how i have added it in c# code: namespace abc { public partial class StoredProcedures { private const string ERROR_FILE_PATH = "Logs.txt"; } public static void test1(string Path) { } } Test.[abc.StoredProcedures].test1 – user1254053 Apr 20 '13 at 08:11
  • Thanks a lot hvd! It worked. Much appreciate your suggestion. – user1254053 Apr 20 '13 at 08:18
  • This is working perfectly, thank you hvd for understanding my question. Love you for this! – user1254053 Apr 20 '13 at 08:23
  • @PeterMortensen I appreciate your attempt to clean up my answer, but the top bit was an exact quote and was marked as a quote, so I don't think changing it is the right thing to do, and the comma you added in my text causes a subtle change in meaning that I do not really agree with. I hope you don't take offence to me reverting it. –  Sep 16 '13 at 19:58
0

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#.

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234