1

I tried to create a CLR stored procedure in VS2017 but encountering error "NOT Connected." while executing that stored procedure.

I need to connect to other database server to grab some data. Therefore I cannot use context=true in SqlConnection.

  1. Stored procedure will be created in serverA
  2. This stored procedure will query data from serverB
  3. Data will be stored back to serverA.

Is there anything I need to do in order to have regular connection in CLR stored procedure?

Please advise. Thanks!

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void udp_CLR_GetData()
    {
        string ConnStr = "server=MyServer; database=MyDB; user id=accabc; password=abc123";
        string sql = " select top 1 ID from [dbo].Table1 ";
        SqlDataReader dr = null;
        DataTable dt = new DataTable();

        try
        {
            using (SqlConnection fcon = new SqlConnection(ConnStr))
            {
                if (fcon.State == ConnectionState.Open)
                {
                    SqlContext.Pipe.Send("Connected.");

                    using (SqlCommand fcmd = new SqlCommand(sql, fcon))
                    {
                        SqlContext.Pipe.Send("Before executing reader...");
                        dr = fcmd.ExecuteReader();
                        SqlContext.Pipe.Send("After executing reader...");

                        SqlContext.Pipe.Send("Before send...");
                        SqlContext.Pipe.Send(dr);
                        SqlContext.Pipe.Send("After send...");
                    }
                }
                else
                {
                    SqlContext.Pipe.Send("NOT Connected.");
                }
            }
        }
        catch(Exception ex)
        {
            SqlContext.Pipe.Send("Exception error (udp_CLR_GetData): " + ex.Message);
        }
        finally
        {
            if(dr != null && !dr.IsClosed)
            {
                dr.Close();
            }
        }
    }
}

2 Answers 2

1

Creating a new instance of a SqlConnection in:

using (SqlConnection fcon = new SqlConnection(ConnStr))

does not create it in an "open" state. You need to actually open it for it to be "open". So, I would remove the if (fcon.State == ConnectionState.Open) and the associated else part of it. I would also remove the SqlContext.Pipe.Send("Connected."); line.

Then, just before the dr = fcmd.ExecuteReader(); line, add a line for:

fcon.Open();

This way you open the connection and immediately execute the command. No need to open the connection only to do other work getting the command ready.

For more info on working with SQLCLR in general, please visit: SQLCLR Info

Sign up to request clarification or add additional context in comments.

1 Comment

OMG!!!!!!!!! I've missed out to open the connection!!!!!! Thanks for pointing out this!!!
0

Try defining the data source in the connection string instead of server

 string ConnStr = "DataSource=MyServer;Initial Catalog=MyDB;User Id=accabc;Password=abc123";

other than that, make sure clr is enabled on the server: https://learn.microsoft.com/en-us/sql/relational-databases/clr-integration/clr-integration-enabling?view=sql-server-ver15

3 Comments

The CLR integration option is definitely enabled since this code got far enough to run the SqlContext.Pipe.Send("NOT Connected."); line. If CLR was not enabled, there would be an error stating as much when the O.P. attempted to execute the stored procedure. Also, the connection string format is likely OK. So far, it hasn't been used since the connection was never opened ;-).
Ahh yes good catch. I see your suggested answer and it makes sense
ya, as @SolomonRutzky mentioned, CLR is enabled. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.