Convert this code that can work with access

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am working with this code to import text file to SQL server Data base . i dont have any problem with it .But till now i dont work with access and i dont write any program for access database so i cant convert this code to work with access database
. can you help me ? please convert this code to work with access data base

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; string sqltable = <span style="color:#A31515; "dbo.student";
<span style="color:Blue; string[] importfiles = Directory.GetFiles(<span style="color:#A31515; @"F:j1", <span style="color:#A31515; "2.txt");

<span style="color:Green; // try to wrap your ADO.NET stuff into using() statements to automatically
<span style="color:Green; // dispose of the SqlConnection after youre done with it
<span style="color:Blue; using (SqlConnection con = <span style="color:Blue; new SqlConnection(<span style="color:#A31515; "server=Server-PC;uid=sa;pwd=1234;database=alavi"))
{
<span style="color:Green; // define the SQL insert statement and use parameters
<span style="color:Blue; string sqlStatement =
<span style="color:#A31515; "INSERT INTO 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)";

<span style="color:Green; // define the SqlCommmand to do the insert - use the using() approach again
<span style="color:Blue; using (SqlCommand cmd = <span style="color:Blue; new SqlCommand(sqlStatement, con))
{
<span style="color:Green; // define the parameters for the SqlCommand
cmd.Parameters.Add(<span style="color:#A31515; "@fname", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@lname", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@pname", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@shsh", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@shs", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@tt", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@mahal", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@mfname", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@mlname", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@ptahsil", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@pjob", SqlDbType.NVarChar, 50);
cmd.Parameters.Add(<span style="color:#A31515; "@mtahsil", SqlDbType.NVarChar, 50);

<span style="color:Green; // loop through all files found
<span style="color:Blue; foreach (<span style="color:Blue; string importfile <span style="color:Blue; in importfiles)
{
<span style="color:Green; // read the lines from the text file
<span style="color:Blue; string[] allLines = File.ReadAllLines(importfile);

con.Open();

<span style="color:Green; // start counting from index = 1 --> skipping the header (index=0)
<span style="color:Blue; for (<span style="color:Blue; int index = 1; index < allLines.Length; index++)
{
<span style="color:Green; // split up the data line into its parts, using "|" as separator
<span style="color:Green; // items[0] = date
<span style="color:Green; // items[1] = time
<span style="color:Green; // items[2] = text

<span style="color:Blue; var items = allLines[index].Split(<span style="color:Blue; new <span style="color:Blue; char[] { <span style="color:#A31515; , });
cmd.Parameters[<span style="color:#A31515; "@fname"].Value = items[0];
cmd.Parameters[<span style="color:#A31515; "@lname"].Value = items[1];
cmd.Parameters[<span style="color:#A31515; "@pname"].Value = items[2];
cmd.Parameters[<span style="color:#A31515; "@shsh"].Value = items[3];
cmd.Parameters[<span style="color:#A31515; "@shs"].Value = items[4];
cmd.Parameters[<span style="color:#A31515; "@tt"].Value = items[5];
cmd.Parameters[<span style="color:#A31515; "@mahal"].Value = items[6];
cmd.Parameters[<span style="color:#A31515; "@mfname"].Value = items[7];
cmd.Parameters[<span style="color:#A31515; "@mlname"].Value = items[8];
cmd.Parameters[<span style="color:#A31515; "@ptahsil"].Value = items[9];
cmd.Parameters[<span style="color:#A31515; "@pjob"].Value = items[10];
cmd.Parameters[<span style="color:#A31515; "@mtahsil"].Value = items[11];


cmd.ExecuteNonQuery();
}

con.Close();
}
}
}
[/code]
<br/>
<br/>


View the full article
 
Back
Top