c# - conversion failed when converting date and/or time from character string when reading from file -
i know question has been posted on stackoverflow in different forms didn't manage solve problem. trying insert sql server database data read text file. have inserted data database in format '2014-02-02' don't think it's here. text file looks :
1213 3 2013-01-03 2013-03-03 1263 2 2014-01-01 2014-01-10
and code is:
private void importcomandătoolstripmenuitem_click(object sender, eventargs e) { string cale = application.startuppath; openfiledialog ofd = new openfiledialog(); if (ofd.showdialog() == dialogresult.ok) { cale = ofd.filename; system.io.streamreader sr = new system.io.streamreader(cale); string linie = null; while ((linie = sr.readline()) != null) { string comanda = sr.readline(); string[] vcmd = comanda.split(' '); foreach (string cmd in vcmd) { sqlconnection conn = new sqlconnection("server=localhost;" + "trusted_connection=yes;" + "database=erp; " + "connection timeout=30"); sqlcommand cmd1 = new sqlcommand(); cmd1.commandtext = "insert [erp].[dbo].[comenzi] values("+int32.parse(cmd[0].tostring())+","+int32.parse(cmd[1].tostring())+",'"+cmd[2].tostring()+"','"+cmd[3].tostring()+"');"; cmd1.commandtype = commandtype.text; cmd1.connection = conn; conn.open(); cmd1.executenonquery(); conn.close(); } } messagebox.show("comanda inserată"); } else messagebox.show("inserare eșuată"); } }
any suggestions?
the first problem 2 date in file. if expressed in format accepted locale settings conversion enough. code tries update database has many errors.
i have tried fix them.
- first. use using statement correctly close connection when have done
- second. use parameterized query pass data database. string concatenation prone error bad parsing , open sql injection hacks.
- third. open connection before entering loop , create command parameters dummy values. inside loop change value of parameters actual value , execute query
- fourth. splitting of string produces array , need use 4 elements of array not inexistant elements of
cmd
single string variable - fifth. read line 2 times each loop. unless have blank row every line of data should remove readline inside loop
.
private void importcomandătoolstripmenuitem_click(object sender, eventargs e) { string cale = application.startuppath; openfiledialog ofd = new openfiledialog(); if (ofd.showdialog() == dialogresult.ok) { cale = ofd.filename; system.io.streamreader sr = new system.io.streamreader(cale); using(sqlconnection conn = new sqlconnection("server=localhost;" + "trusted_connection=yes;" + "database=erp; " + "connection timeout=30")) using(sqlcommand cmd1 = new sqlcommand(@"insert [erp].[dbo].[comenzi] values @p1, @p2, @p3, @p4", conn) { conn.open(); cmd1.parameters.addwithvalue("@p1", 0); cmd1.parameters.addwithvalue("@p2", 0); cmd1.parameters.addwithvalue("@p3", datetime.minvalue); cmd1.parameters.addwithvalue("@p4", datetime.minvalue); string comanda = null; while ((comanda = sr.readline()) != null) { string[] vcmd = comanda.split(' '); cmd1.parameters["@p1"].value = convert.toint32(vcmd[0])); cmd1.parameters["@p2"].value = convert.toint32(vcmd[1])); cmd1.parameters["@p3"].value = convert.todatetime(vcmd[2])); cmd1.parameters["@p4"].value = convert.todatetime(vcmd[3])); cmd1.executenonquery(); } } } }
Comments
Post a Comment