How can I set the value for Bulk Insert?

I am writing this code to run a sql server script using C #:

string sqlConnectionString = "Data Source=.;Initial Catalog=SERVERRAREPORT;Integrated Security=True";
            //string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
            FileInfo file = new FileInfo("d:\\behzadBULK.sql");
            string script = file.OpenText().ReadToEnd();
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            Microsoft.SqlServer.Server server = new Microsoft.SqlServer.Server(new ServerConnection(conn));
            server.ConnectionContext.ExecuteNonQuery(script);


and in behzadBULK.sqlI will write this code:

BULK INSERT TEMPO
FROM 'd:\3.csv'
WITH(
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '\n',
    CODEPAGE = '1256'
  );


in "Bulk Insert", "From the parameter set with the file name, this parameter is static, but I want the application to look at the browser file in C # and select the file and set the file name for the mass paste from the parameter.
my plan is this:
enter image description here

+4
source share
1 answer

This is the volume insert structure:

BULK INSERT 
   [ database_name. [ schema_name ] . | schema_name. ] [ table_name | view_name ] 
  FROM 'data_file' 
 [ WITH ...

Since the file name must be static, you can do the following:

DECLARE @sql NVARCHAR(4000) = 'BULK INSERT TEMPO FROM ''' + @FileName + ''' WITH ( FIELDTERMINATOR ='','', ROWTERMINATOR =''\n'' )';
EXEC(@sql);
+2
source

All Articles