Belgrade-SqlClient icon indicating copy to clipboard operation
Belgrade-SqlClient copied to clipboard

Need to pass appname in connection string

Open SudhaaG opened this issue 5 years ago • 2 comments

Hi Jovan

Thank you for the nifty tool. In my REST api project, I am using the SqlPipe.Sql(cmd).Stream(ms, "[]") but for every cmd, I need to pass a new connection string with an included application name. Currently, the stream logic in the belgrade sqlclient is using the connectionstring given in the base command created during startup. I need an ability to pass the connection string. I tried to download and build locally with this additional method included but I am unable to proceed with the build due to the following error.

Severity Code Description Project File Line Suppression State Error MSB3325 Cannot import the following key file: BelgradeSqlClient.pfx. The key file may be password protected. To correct this, try to import the certificate again or manually install the certificate to the Strong Name CSP with the following key container name: VS_KEY_3C6256A74AF3876F Code C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets 3214

Please help resolve this urgent requirement.

thank you, Sudhaa

SudhaaG avatar Apr 29 '19 16:04 SudhaaG

Hi Sudhaa,

Dependency injection is used as a convenience method and something that is standard in ASP.NET. If you need to provide different connection string on every query, I would propose not to use dependency injection and command initiated in startup. Instead, create a query/command object as local variable in action method, initialize it with your custom connection string and then call pipe method. Technically you just need to copy initialization code from startup and put it into your controller action code.

If you want to edit the code just remove pfk file from solution .

Regards, Jovan

JocaPC avatar Apr 29 '19 17:04 JocaPC

Thanks for the response. I am creating the command object just before calling the sql.stream but still the stream method is using the base.command and its associated connection.

SqlCommand cmd = new SqlCommand(sql) { CommandType = CommandType.StoredProcedure, Connection = new SqlConnection(SqlDataAdaptor.GenerateConnectionString(task)) }; await cmd.Connection.OpenAsync(); Task t = SqlPipe.Sql(cmd).Stream(cmd,ms, "[]");

This is not using the "task" that I am sending instead its using the default which was created at startup.cs

services.AddTransient<IQueryPipe>(_ => new QueryPipe(new SqlConnection(Configuration.GetConnectionString("defaultDB")))); services.AddTransient<ICommand>(_ => new Command(new SqlConnection(Configuration.GetConnectionString("defaultDB"))));

SudhaaG avatar Apr 29 '19 17:04 SudhaaG