Click here to Skip to main content
15,914,070 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
//string filepath = @"C:\Documents and Settings\HPFS.HPFS\Desktop\rajmal.csv";
        string file = FileUpload1.PostedFile.FileName;
        file = Path.GetFileName(file);
        string filepath = Server.MapPath("~/csv/")+file;
        FileUpload1.SaveAs(filepath);
        StreamReader sr = new StreamReader(filepath);
        string line = sr.ReadLine();
        string[] value = line.Split(';');
        DataTable dt = new DataTable();
        DataRow row;
        foreach (string dc in value)
        {
            dt.Columns.Add(new DataColumn(dc));
        }
        while ( !sr.EndOfStream )
        {
            value = sr.ReadLine().Split(';');
            if(value.Length == dt.Columns.Count)
            {
                row = dt.NewRow();
                row.ItemArray = value;
                dt.Rows.Add(row);
            }
        }
        
        SqlBulkCopy bc = new SqlBulkCopy(sc.ConnectionString, SqlBulkCopyOptions.TableLock);

        bc.DestinationTableName = "company";

        bc.BatchSize = dt.Rows.Count;

        sc.Open();

        bc.WriteToServer(dt);

        Response.Write("Ok");

        bc.Close();

        sc.Close(); 

This code will work in SQL SERVER(IF SQL SERVER CONNECTIONS MADE) but not in MYSQL DATABASE. :(

Please Provide me a sample code.

Help me out guys please :)

I need to upload csv file into MYSQL DATABASE using ASP.NET :)
Posted
Updated 4-Jul-12 3:10am
v3
Comments
[no name] 4-Jul-12 8:44am    
"This code will work in SQL SERVER(IF SQL SERVER CONNECTIONS MADE) but not in MYSQL"
Great description of a problem... it don't work. Nothing about what does not work. Nothing about errors.

"I need to upload csv file into MYSQL"
Go ahead, you hav emy permission to do so.
Harsha Dev 4-Jul-12 9:10am    
can u give me a sample code if u have?

1 solution

SQL
create table tbl_BulkUpload([id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    [EmpName] [nvarchar](max) NULL,
    [LastName] [nvarchar](max) NULL,
    [Email] [nvarchar](max) NULL
) ON [PRIMARY]

create view tbl_View_BulkUpload
as
select EmpName,LastName from tbl_BulkUpload

BULK INSERT tbl_View_BulkUpload
FROM 'D:\BulkUpload.txt'
WITH
(
FIELDTERMINATOR =',',
ROWTERMINATOR = '\n'
)
 
Share this answer
 
Comments
Harsha Dev 5-Jul-12 0:18am    
thank u michael :) but i need C# code :(
[no name] 5-Jul-12 2:19am    
execute the same line of codes in C#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900