sql server - Can't connect to SQL 2008 database using .NET Core 2.0 -


update

i never make work "windows authentication" (domain) user. "sql server authentication" user.

original question

my connectionstring: server=ip;database=dbname;user id=xxx\user;password=pass;

the connection string located in appsettings.json this:

{   "logging": {     "includescopes": false,     "loglevel": {       "default": "warning"     }   },   "connectionstrings": {     "connectionstring": "server=ip;database=dbname;user id=xxx\user;password=pass;"   } } 

then pass static class "startup.cs" file, this:

public void configureservices(iservicecollection services) {     // add framework services.     services.addmvc();      orm.databaseconnection.connectionstring = configuration["connectionstrings:connectionstring"]; } 

this initiate connection:

using system.data.sqlclient;  namespace myproject.orm {     public static class databaseconnection     {         public static string connectionstring { get; set; }          public static sqlconnection connectionfactory()         {             return new sqlconnection(connectionstring);         }     } } 

and controller:

public string get() {     using (var databaseconnection = orm.databaseconnection.connectionfactory())     {         var sections = databaseconnection.query("select * mytable").tolist();         return sections.tostring();     } } 

where line:

var databaseconnection = orm.databaseconnection.connectionfactory(); 

returns:

serverversion: "'databaseconnection.serverversion' threw exception of type 'system.invalidoperationexception'" message: "invalid operation. connection closed." source: "system.data.sqlclient" stacktrace: "at  system.data.sqlclient.sqlconnection.getopentdsconnection()\n    @  system.data.sqlclient.sqlconnection.get_serverversion()" 

and error on new sqlconnection: "error cs0119: 'sqlconnection' type, not valid in given context".

but program execution doesn't stop because of these errors.

the application hangs on following line:

var sections = databaseconnection.query("select * mytable").tolist(); 

i'm using dapper orm (not entityframework). in "mytable" sql table 17 rows , 5 columns should load fast.

i tried kinds of different connectionstrings fails. if try same .net framework 4.5, works fine. problem .net core 2.0.

any idea fixing welcome. because spent many hours on already.

try add databaseconnection.open().

public string get() {     using (var databaseconnection = new sqlconnection(@"server=ip;database=dbname;user id=xxx\user;password=pass;pooling=false;"))     {         databaseconnection.open();         var sections = databaseconnection.query("select * mytable").tolist();         return sections.tostring();     } } 

to avoid problems connection pool described in comments add pooling=false; connection string: server=ip;database=dbname;user id=xxx\user;password=pass;pooling=false;

edit: hardcoded connection string , removed factory make example smaller


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -