0

Alright so using C# I am creating a DLL that I am using in SSMS and attaching it as an assembly to perform a few tasks, I can create/use the DLL fine but I am having trouble access the DB from the DLL to pull data in. If i was creating a form in C# to access the data i would do something like this

  public class Gaps
{

    public static void Find_Gaps1()
    {
        SqlConnection connection = new SqlConnection("Server = DIS; Database = dyn35;  Integrated Security = true");
        DataTable FinishDT = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter();
        String SQLstatement = "SELECT [new_cust] FROM [Dyn35].[dbo].[Account] WHERE [new_cust] IS NOT NULL AND ISNUMERIC([new_cust]) = 1 ORDER BY [new_cust]";
        connection.Open();
        SqlCommand command = new SqlCommand(SQLstatement, connection);
        command.CommandTimeout = 0;
        adapter.SelectCommand = command;
        adapter.Fill(FinishDT);
        connection.Close();
    }

}

Then i could easily manipulate the data that is in the Data Table. I cannot do this as a DLL because it throws an error stating i can't. Would anyone know how to send data from a query to a DLL using t-sql? Or point me in the right direction? The stored procedure I am creating is inside the same DB that I'm trying to access.

1
  • 2
    You most likely have a credential issue. Is dll being used on same machine and same user account where code was built? Either one of two things is wrong 1) The user is different and doesn't have access to the database 2) You installed code on a different machine with a different version of Net. When a Net application is installed on another machine either the same version of Net must be on deploy machine or you need to publish and install using the setup.exe. Net library uses Windows dll and the setup.exe updates windows dll in deploy machine so Net application will run. Commented Apr 6, 2021 at 6:29

1 Answer 1

0

Yeah I believe it was a credential issue jdweng, once I set the sql connection context connection = true it was able to flow great!

SqlConnection connection = new SqlConnection("context connection = true");
Sign up to request clarification or add additional context in comments.

2 Comments

This isn't a credential issue at all. "context connection = true" only works in SQLCLR assemblies and is used to connect to the server the assembly is deployed in. You didn't say you have a SQLCLR assembly. The code you posted shouldn't be used in a SQLCLR assembly either. It wastes a lot of memory to just load some data in SQL Server's memory. Given that the server already caches data, this ends up loading the data twice for no benefit.
What are are you trying to do, and why are you using a SQLCLR?

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.