c# - ExecuteReader: Connection property has not been initailized -
the error executereader: connection property has not been initialized giving me fit. think have set right. show how create mssql connection , requesting, , class created read/write , open/close connection. (three different parts of app lumped them see actual logic flow hope.)
what missing, or supposed put connection? example? appreciate help!
here using statement start with:
using (mssql mssqldb = new mssql(constants.mssqlserver, constants.mssqldb, constants.mssqluid, constants.mssqlpswd))
here "hey, me data using mssql
class"
using (var results = mssqldb.read("select top 1 * alertlog order alarmid desc")) { results.read(); legacyalert = int32.parse(results["alarmid"].tostring().trim()); }
here mssql class
class mssql : idisposable { public mssql(string server, string database, string uid, string pswd) { string mssqlconnectionstring = @"data source=" + server + ";initial catalog=" + database + ";user id=" + uid + ";password=" + pswd; sqlconnection msqlconnection = new sqlconnection(mssqlconnectionstring); msqlconnection.open(); console.writeline("ms sql open!"); } public void write(string sql) { using (sqlcommand mycommand = new sqlcommand(sql, msqlconnection)) mycommand.executenonquery(); } public sqldatareader read(string sql) { using (sqlcommand mycommand = new sqlcommand(sql, msqlconnection)) return mycommand.executereader(); } public void dispose() { try { msqlconnection.close(); } catch (sqlexception ex) { console.error.writeline("ms sql error - closing database"); console.error.writeline(ex); } msqlconnection.dispose(); } private sqlconnection msqlconnection; }
msqlconnection
null.
your constructor creates local variable named msqlconnection
, not assign field.
Comments
Post a Comment