Share via


convert .txt to .mdf and use

Question

Thursday, December 12, 2013 4:38 PM

i want to know is there an efficient way to convert .txt into .mdf so that i can use it in my database related project..

All replies (12)

Thursday, December 19, 2013 11:44 AM ✅Answered | 1 vote

Try something like the code below.  For testing I used StringReader.  Use StreamReader instead to get data from a file.

       const string TABLE = "Table";        const string CONNSTRING = "Enter You r Conneciton String Here";        static void Main(string[] args)        {            string input =                "Country,City,AccentCity,Region,Population,Latitude,Longitude\n" +                "ad,aixas,Aixàs,06,,42.4833333,1.4666667\n" +                "ad,aixirivali,Aixirivali,06,,42.4666667,1.5\n";            string columnNames = "";            SqlConnection conn = new SqlConnection(CONNSTRING);            conn.Open();            SqlCommand cmd = new SqlCommand();            cmd.Connection = conn;            //for reading from a file use StreamReader instead of StringReader            //StreamReader reader = new StreamReader("filename");            StringReader reader = new StringReader(input);            string inputLine = "";            int rowCount = 0;            while ((inputLine = reader.ReadLine()) != null)            {                if (rowCount == 0)                {                    columnNames = inputLine;                }                else                {                    //add single quote around all values                    string values =  "'" + inputLine.Replace(",","','") + "'";                                        string SQL = string.Format("Insert into {0} ({1}) Values ({2})", TABLE, columnNames, values);                    cmd.CommandText = SQL;                    cmd.ExecuteNonQuery();                }                rowCount++;            }            cmd.Dispose();            conn.Close();            conn.Dispose();        }

jdweng


Friday, December 20, 2013 7:43 PM ✅Answered | 1 vote

The code should work with the following changes

1) Replace the StringReader with StreamReader.

2) Add a connection string

3) Enter the DataBase table name

4) Enter the CSV filename

jdweng


Thursday, December 12, 2013 4:50 PM

mdf is a database format and you need to use SQL client class with SQL INSERT statements to write to the database.

jdweng


Thursday, December 12, 2013 4:58 PM

Use

SQL Server Import and Export Wizard

from SQL Server Management Studio, or

Microsoft SQL Server Management Studio Express

to create a SQL Server database and import the text data into a new table in a SQL database.

David

David http://blogs.msdn.com/b/dbrowne/


Saturday, December 14, 2013 11:43 AM

how can i import that file in Visual Studio as i am coding in C# and want to access that .txt file using database ... i know I'm not explaining well


Wednesday, December 18, 2013 6:25 AM

Hi Sam,

You cannot easily import data from txt into SQL Server.

See the code example on the following link. http://social.msdn.microsoft.com/Forums/vstudio/en-US/3ce5bb2e-85e9-4116-af6e-3d9c37111282/c-import-data-from-text-file-into-sql-databse-line-by-line.

Hope useful to you.

Regards,

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Wednesday, December 18, 2013 6:48 AM

not so efficient but an easy way of doing it.

you can load the text into the excel and use concat excel function to build an insert query.


Wednesday, December 18, 2013 7:19 AM

Can you post some samples of the text file?  I can easily write an upload function.  It usually takes around 25 line of code to write depending on the type of data.  Is the code a one time application or does the code have to work with a different types of text files?

jdweng


Thursday, December 19, 2013 7:36 AM

the txt format is in csv format like this.....
Country,City,AccentCity,Region,Population,Latitude,Longitude
ad,aixas,Aixàs,06,,42.4833333,1.4666667
ad,aixirivali,Aixirivali,06,,42.4666667,1.5
ad,

samEE


Thursday, December 19, 2013 7:40 AM

According your description, You want import data from csv to sql server.

 Mark as answer if it helps to you
 Free .NET Barcode Generator & Scanner supporting over 40 symbologies


Friday, December 20, 2013 4:17 PM

@Joel Engineer the txt i provided  was just a sample the .txt file i want to convert into database is 144 MB i want to read it through code and add it in .mdf file .. i thnk now i'm explaining it rightly...

samEE


Saturday, December 21, 2013 8:36 AM

@Joel Engineer  thnx man table is created :)

samEE