c# - Importing .DAT file to Database? -
how go import/inserting .dat file database calling procedure?
here's file , has go database in format.
50 4411902304 1 3 441192304 01/02/2013
would process same .dat file xml file?
here's have xml
sqlconnection myconnection = new sqlconnection("user id=name;" + "password=password;server=servername;" + "trusted_connection=yes;" + "database=database; " + "connection timeout=30"); var conn = new sqlconnection(); conn.connectionstring = "user id=idname;" + "password=password;" + "server=servername;" + "trusted_connection=yes;" + "database=databasename; " + "connection timeout=30"; string filepath = "c:/testdata2.xml"; string xml = file.readalltext(filepath); using (sqlconnection con = new sqlconnection(conn.connectionstring)) { using (sqlcommand cmd = new sqlcommand("procedurename")) { cmd.connection = con; cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@x", xml); con.open(); cmd.executenonquery(); con.close(); messagebox.show("done"); } }
what happens xml new technology compared old flat file (dat). xml markup format file , there functions implemented make easier importing tasks. flat file older, different approach needed.
you can use bcp (bulk copy program) import files sql server or ssis import options.
or, can use:
system.io.streamreader file = new system.io.streamreader(@"c:\data\testdata2.dat"); int counter = 0; while ((line = (file.readline())) != null){...}
and parsing each line using split command.
string[] fields= line.split(' ');
string = fields[0];
string b = fields[1];
string c = fields[2];
and execute command insert each line:
string sqlcommandtoinsert= "insert [table] (tablefield1, tablefield2, tablefield3) values (" + + ", " + b + ", '" + c + "');";
cmd.commandtext = sqlcommandtoinsert;
cmd.executenonquery();
inserting each record in table.
Comments
Post a Comment