c# - retrieving a datalist created in one void and displaying it in another -
i have question calling data 1 method another.
basically have tasked myself condensing , simplifying code. in scenario connecting .mdb file , reading 1 bit of data it. had connection string in spots needed, after learning more , more classes , stuff informed copying , pasting bad. wanted take connection string , make it...it's own void call variables needed spots. problem how return datalist retrieved db? here code have works fine.
string directory = @"c:\"; string[] folders = directory.getdirectories(directory, "rameses-*"); list<string> datalist = new list<string>(); foreach (string foldername in folders) { using (var conection = new system.data.oledb.oledbconnection("provider=microsoft.jet.oledb.4.0;" + "data source=" + foldername + "\\program\\random.mdb;jet oledb:database password=******")) { conection.open(); var query = "select u_company t_user"; var command = new system.data.oledb.oledbcommand(query, conection); var reader = command.executereader(); while (reader.read()) { datalist.add(reader[0].tostring()); } reader.close(); conection.close(); } } error_handling(" alternate profiles loaded selector"); profselect.datasource = datalist; profselect.selectedtext = datalist.last();
but thought able take connection out of it's current spot , can't datalist inital connection drop down. suggestions?
//database connection base function public static void database_link(string query, string foldername) { list<string> datalist = new list<string>(); using (var conection = new system.data.oledb.oledbconnection("provider=microsoft.jet.oledb.4.0;" + "data source=" + foldername + "\\program\\random.mdb;jet oledb:database password=****")) { conection.open(); var command = new system.data.oledb.oledbcommand(query, conection); var reader = command.executereader(); while (reader.read()) { datalist.add(reader[0].tostring()); } reader.close(); conection.close(); } } // ramdata check , load files , database private void ramdata_check(object sender, eventargs e) { try { string directory = @"c:\"; string[] folders = directory.getdirectories(directory, "myapp-*"); list<string> datalist = new list<string>(); foreach (string foldername in folders) { database_link("select u_company t_user", foldername); } error_handling(" alternate profiles loaded selector"); profselect.datasource = datalist; profselect.selectedtext = datalist.last(); } catch {}
don't make shared connection call void: make create connection, open , return it. using block close connection when completes don't need manually close it:
public oledbconnection openconnection() { var result = new oledbconnection("my special connection string here")) result.open() return result; } public list<string> void dosomethingwithconnection() { using (var connection = openconnection()); { list<string> datalist = new list<string>(); oledbcommand command = new oledbcommand("dosomethinghere", connection); var reader = command.executereader(); while (reader.read()) { datalist.add(reader[0].tostring()); } return datalist; // connection automatically closed when // code exits using block. } }
Comments
Post a Comment