c# - Repeat read specific number of line into a text file, show them into a textboxes and save them -
i read text file (8 lines), show them textbox , save them db. it. need continue read text files (8 lines everytime).
this code:
var textboxes = new list<textbox> { textbox1, textbox2, textbox3, textbox4, textbox5, textbox6, textbox7, textbox8 }; using (streamreader sr = new streamreader(@"saverisbex.txt")) { int incnumber = 0; string nynumber = incnumber.tostring("00"); incnumber++; textbox9.text = incnumber.tostring(); int linenumber = 0; int lastgroup = 0; string line; while ((line = sr.readline()) != null) { int currentgroup = linenumber / 8; if (lastgroup != currentgroup) { conn.open(); sqlcommand comando = new sqlcommand("", conn); comando.commandtext = "insert finabex (id,home,away,homescoresft,awayscoresft,oddhome,oddx,oddaway,date) values ('" + textbox9.text + "', '" + textbox1.text + "', '" + textbox2.text + "', '" + textbox3.text + "', '" + textbox4.text + "', '" + textbox5.text + "', '" + textbox6.text + "', '" + textbox7.text + "', '" + textbox8.text + "')"; comando.executenonquery(); messagebox.show("saved!"); conn.close(); } int textboxnumber = linenumber % 8; textboxes[textboxnumber].text = line; lastgroup = currentgroup; linenumber++; } }
so, read first 8 lines (0-7), need continue (8-15), (16-23) , on.
i hope can me. everybody!!! :d
you use integer division , %
-operators. put textboxes collection:
var textboxes = new list<textbox> { textbox1, textbox2, textbox3, textbox4, textbox5, textbox6, textbox7, textbox8 }; using(var conn = new sqlconnection("connectionstring")) using (streamreader sr = new streamreader(@"saverisbex")) { int linenumber = 0; int lastgroup = 0; string line; conn.open(); // before loop not in loop while((line = sr.readline()) != null) { int currentgroup = linenumber / 8; if (lastgroup != currentgroup) { // presuming id primary key , identity column auto generates value on insert string insertsql = @"insert finabex (home,away,homescoresft,awayscoresft, oddhome,oddx,oddaway,date) values (@home,@away,@homescoresft,@awayscoresft,@oddhome,@oddx,@oddaway,@date)"; using (var comando = new sqlcommand(insertsql, conn)) { comando.parameters.add("@home", sqldbtype.varchar).value = textbox9.text; comando.parameters.add("@away", sqldbtype.varchar).value = textbox1.text; comando.parameters.add("@homescoresft", sqldbtype.varchar).value = textbox2.text; comando.parameters.add("@awayscoresft", sqldbtype.varchar).value = textbox3.text; comando.parameters.add("@oddhome", sqldbtype.varchar).value = textbox4.text; comando.parameters.add("@oddx", sqldbtype.varchar).value = textbox5.text; comando.parameters.add("@oddaway", sqldbtype.varchar).value = textbox7.text; datetime date; if (!datetime.tryparse(textbox8.text, out date)) { messagebox.show("not valid date: " + textbox8.text); continue; } comando.parameters.add("@date", sqldbtype.datetime).value = date; int insertedcount = comando.executenonquery(); } // ... } int textboxnumber = linenumber % 8; textboxes[textboxnumber].text = line; // ... lastgroup = currentgroup; linenumber++; } }
so linenumber=0
in groupof8=0
until linenumber=7
, groupof8=1
, on.
note have used using
-statement on objects implement idisposable
ensure unmanaged resources disposed property(even in case of error).
also use sql parameters prevent sql-injection, always.
Comments
Post a Comment