Sunday, November 28, 2010

Bulk Inser in Sql from CSV

/* CSV to SQL */
create table UserTable(
UserName varchar(50),
UserPassword varchar(50)
)


BULK
INSERT UserTable
FROM 'D:\Usertable.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
___________

[ Using Procedure ]
In Code behind
-------------

Dbconn.connection();

cmd = new SqlCommand("bulkinsert",Dbconn.con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Path",SqlDbType.VarChar,50));
cmd.Parameters["@Path"].Value = FileUpload2.PostedFile.FileName;
cmd.ExecuteNonQuery();
----

Procedure
---------
CREATE Procedure [dbo].[bulkinsert]
@Path Varchar(50)
As
Declare @Str Varchar(2000)
Begin
Set @Str=''BULK INSERT waybill_register FROM ''
Set @Str=@Str+''''''''
Set @Str=@Str+@Path
Set @Str=@Str+''''''''
Set @Str=@Str+'' WITH (FIELDTERMINATOR = '''','''',ROWTERMINATOR = ''''\n'''')''
Exec(@Str)
End

0 comments:

Post a Comment