Convert this code that can work with access
i working code import text file sql server data base . don't have problem .but till don't work access , don't write program access database can't convert code work access database . can me ? please convert code work access data base
string sqltable = "dbo.student"; string[] importfiles = directory.getfiles(@"f:\j1", "2.txt"); // try wrap ado.net stuff using() statements automatically // dispose of sqlconnection after you're done it using (sqlconnection con = new sqlconnection("server=server-pc;uid=sa;pwd=1234;database=alavi")) { // define sql insert statement , use parameters string sqlstatement = "insert student(pname,fname,lname,shsh,shs,tt,mahal,mfname,mlname,ptahsil,pjob,mtahsil) values(@pname,@fname,@lname,@shsh,@shs,@tt,@mahal,@mfname,@mlname,@ptahsil,@pjob,@mtahsil)"; // define sqlcommmand insert - use using() approach again using (sqlcommand cmd = new sqlcommand(sqlstatement, con)) { // define parameters sqlcommand cmd.parameters.add("@fname", sqldbtype.nvarchar, 50); cmd.parameters.add("@lname", sqldbtype.nvarchar, 50); cmd.parameters.add("@pname", sqldbtype.nvarchar, 50); cmd.parameters.add("@shsh", sqldbtype.nvarchar, 50); cmd.parameters.add("@shs", sqldbtype.nvarchar, 50); cmd.parameters.add("@tt", sqldbtype.nvarchar, 50); cmd.parameters.add("@mahal", sqldbtype.nvarchar, 50); cmd.parameters.add("@mfname", sqldbtype.nvarchar, 50); cmd.parameters.add("@mlname", sqldbtype.nvarchar, 50); cmd.parameters.add("@ptahsil", sqldbtype.nvarchar, 50); cmd.parameters.add("@pjob", sqldbtype.nvarchar, 50); cmd.parameters.add("@mtahsil", sqldbtype.nvarchar, 50); // loop through files found foreach (string importfile in importfiles) { // read lines text file string[] alllines = file.readalllines(importfile); con.open(); // start counting index = 1 --> skipping header (index=0) for (int index = 1; index < alllines.length; index++) { // split data line parts, using "|" separator // items[0] = date // items[1] = time // items[2] = text var items = alllines[index].split(new char[] { ',' }); cmd.parameters["@fname"].value = items[0]; cmd.parameters["@lname"].value = items[1]; cmd.parameters["@pname"].value = items[2]; cmd.parameters["@shsh"].value = items[3]; cmd.parameters["@shs"].value = items[4]; cmd.parameters["@tt"].value = items[5]; cmd.parameters["@mahal"].value = items[6]; cmd.parameters["@mfname"].value = items[7]; cmd.parameters["@mlname"].value = items[8]; cmd.parameters["@ptahsil"].value = items[9]; cmd.parameters["@pjob"].value = items[10]; cmd.parameters["@mtahsil"].value = items[11]; cmd.executenonquery(); } con.close(); } } }
hello pooriagoogooli,
i working code import text file sql server data base . don't have problem .but till don't work access , don't write program access database can't convert code work access database . can me ? please convert code work access data base
string sqltable = "dbo.student"; string[] importfiles = directory.getfiles(@"f:\j1", "2.txt"); // try wrap ado.net stuff using() statements automatically // dispose of sqlconnection after you're done it using (sqlconnection con = new sqlconnection("server=server-pc;uid=sa;pwd=1234;database=alavi")) { // define sql insert statement , use parameters string sqlstatement = "insert student(pname,fname,lname,shsh,shs,tt,mahal,mfname,mlname,ptahsil,pjob,mtahsil) values(@pname,@fname,@lname,@shsh,@shs,@tt,@mahal,@mfname,@mlname,@ptahsil,@pjob,@mtahsil)"; // define sqlcommmand insert - use using() approach again using (sqlcommand cmd = new sqlcommand(sqlstatement, con)) { // define parameters sqlcommand cmd.parameters.add("@fname", sqldbtype.nvarchar, 50); cmd.parameters.add("@lname", sqldbtype.nvarchar, 50); cmd.parameters.add("@pname", sqldbtype.nvarchar, 50); cmd.parameters.add("@shsh", sqldbtype.nvarchar, 50); cmd.parameters.add("@shs", sqldbtype.nvarchar, 50); cmd.parameters.add("@tt", sqldbtype.nvarchar, 50); cmd.parameters.add("@mahal", sqldbtype.nvarchar, 50); cmd.parameters.add("@mfname", sqldbtype.nvarchar, 50); cmd.parameters.add("@mlname", sqldbtype.nvarchar, 50); cmd.parameters.add("@ptahsil", sqldbtype.nvarchar, 50); cmd.parameters.add("@pjob", sqldbtype.nvarchar, 50); cmd.parameters.add("@mtahsil", sqldbtype.nvarchar, 50); // loop through files found foreach (string importfile in importfiles) { // read lines text file string[] alllines = file.readalllines(importfile); con.open(); // start counting index = 1 --> skipping header (index=0) for (int index = 1; index < alllines.length; index++) { // split data line parts, using "|" separator // items[0] = date // items[1] = time // items[2] = text var items = alllines[index].split(new char[] { ',' }); cmd.parameters["@fname"].value = items[0]; cmd.parameters["@lname"].value = items[1]; cmd.parameters["@pname"].value = items[2]; cmd.parameters["@shsh"].value = items[3]; cmd.parameters["@shs"].value = items[4]; cmd.parameters["@tt"].value = items[5]; cmd.parameters["@mahal"].value = items[6]; cmd.parameters["@mfname"].value = items[7]; cmd.parameters["@mlname"].value = items[8]; cmd.parameters["@ptahsil"].value = items[9]; cmd.parameters["@pjob"].value = items[10]; cmd.parameters["@mtahsil"].value = items[11]; cmd.executenonquery(); } con.close(); } } }
you access to just replace the terminesql with oledb, import system.data.oledb your project and it the fields declared in code devon match in table name and field type as.
using system.data.oledb;
string sqltable = "dbo.student"; string[] importfiles = directory.getfiles(@"f:\j1", "2.txt"); // try wrap ado.net stuff using() statements automatically // dispose of sqlconnection after you're done using (oledblconnection con = new oledbconnection("yourconnectionstring")) { // define sql insert statement , use parameters string sqlstatement = "insert student(pname,fname,lname,shsh,shs,tt,mahal,mfname,mlname,ptahsil,pjob,mtahsil) values(@pname,@fname,@lname,@shsh,@shs,@tt,@mahal,@mfname,@mlname,@ptahsil,@pjob,@mtahsil)"; // define oledbcommmand insert - use using() approach again using (oledbcommand cmd = new oledbcommand(sqlstatement, con)) { // define parameters sqlcommand cmd.parameters.add("@fname", sqldbtype.nvarchar, 50); cmd.parameters.add("@lname", sqldbtype.nvarchar, 50); cmd.parameters.add("@pname", sqldbtype.nvarchar, 50); cmd.parameters.add("@shsh", sqldbtype.nvarchar, 50); cmd.parameters.add("@shs", sqldbtype.nvarchar, 50); cmd.parameters.add("@tt", sqldbtype.nvarchar, 50); cmd.parameters.add("@mahal", sqldbtype.nvarchar, 50); cmd.parameters.add("@mfname", sqldbtype.nvarchar, 50); cmd.parameters.add("@mlname", sqldbtype.nvarchar, 50); cmd.parameters.add("@ptahsil", sqldbtype.nvarchar, 50); cmd.parameters.add("@pjob", sqldbtype.nvarchar, 50); cmd.parameters.add("@mtahsil", sqldbtype.nvarchar, 50); // loop through files found foreach (string importfile in importfiles) { // read lines text file string[] alllines = file.readalllines(importfile); con.open(); // start counting index = 1 --> skipping header (index=0) (int index = 1; index < alllines.length; index++) { // split data line parts, using "|" separator // items[0] = date // items[1] = time // items[2] = text var items = alllines[index].split(new char[] { ',' }); cmd.parameters["@fname"].value = items[0]; cmd.parameters["@lname"].value = items[1]; cmd.parameters["@pname"].value = items[2]; cmd.parameters["@shsh"].value = items[3]; cmd.parameters["@shs"].value = items[4]; cmd.parameters["@tt"].value = items[5]; cmd.parameters["@mahal"].value = items[6]; cmd.parameters["@mfname"].value = items[7]; cmd.parameters["@mlname"].value = items[8]; cmd.parameters["@ptahsil"].value = items[9]; cmd.parameters["@pjob"].value = items[10]; cmd.parameters["@mtahsil"].value = items[11]; cmd.executenonquery(); } con.close(); } } }
regards.
carmelo la monica
blog http://community.visual-basic.it/carmelolamonica/
wordpress http://carmelolamonica.wordpress.com/
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment