我写这个代码用c#运行sql server脚本:
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);
并进入behzadBULK.sql我写这段代码:
BULK INSERT TEMPO
FROM 'd:\3.csv'
WITH(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
CODEPAGE = '1256'
);
进入批量插入,从参数设置文件名,该参数是静态的,但我想在c#应用程序浏览文件中用浏览器选择文件并设置文件名从批量插入参数.
我的计划是这样的:
最佳答案 这是批量插入的结构:
BULK INSERT
[ database_name. [ schema_name ] . | schema_name. ] [ table_name | view_name ]
FROM 'data_file'
[ WITH ...
因为文件名必须是静态的,所以您可以执行以下操作:
DECLARE @sql NVARCHAR(4000) = 'BULK INSERT TEMPO FROM ''' + @FileName + ''' WITH ( FIELDTERMINATOR ='','', ROWTERMINATOR =''\n'' )';
EXEC(@sql);