Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 必须更改SQL脚本以减少SQL服务器负载_C#_Sql Server 2008 - Fatal编程技术网

C# 必须更改SQL脚本以减少SQL服务器负载

C# 必须更改SQL脚本以减少SQL服务器负载,c#,sql-server-2008,C#,Sql Server 2008,几年前,它是一个脚本,用于将人员导入临时表。现在,加载此脚本需要10分钟,这有时会导致问题 所以我决定检查代码以进行优化,但我不知道如何更改它 当前代码如下所示 // Getting attributes from the configfile string filePath = getAppSetting("filepath"); string fileName = getAppSetting("filename"); string fileBP = filePath + fileName;

几年前,它是一个脚本,用于将人员导入临时表。现在,加载此脚本需要10分钟,这有时会导致问题

所以我决定检查代码以进行优化,但我不知道如何更改它

当前代码如下所示

// Getting attributes from the configfile
string filePath = getAppSetting("filepath");
string fileName = getAppSetting("filename");
string fileBP = filePath + fileName;

if (File.Exists(fileBP))
{
    // Truncate Temp-Table
    SqlCommand command = new SqlCommand("TRUNCATE TABLE [dbo].[temp_person];", connection);
    command.ExecuteNonQuery();
FileStream logFileStream = new FileStream(fileName, FileMode.Open,     FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream, System.Text.Encoding.Default);

while (!logFileReader.EndOfStream)
{
    string line = logFileReader.ReadLine();
    strActLine = line;
    string sql = "INSERT INTO temp_person(per_nummer, per_pid, per_name)"
                 + "VALUES(@per_nummer, @per_pid, @per_name)"

    SqlCommand cmd = new SqlCommand(sql, connection);

    cmd.Parameters.AddWithValue("@per_nummer", isNull(line.Substring(0, 7)));
    cmd.Parameters.AddWithValue("@per_pid", isNull(line.Substring(7, 7)));
    cmd.Parameters.AddWithValue("@per_name", isNull((line.Substring(14, 20))));
    cmd.ExecuteNonQuery();
}
// Clean up
connection.Close();
logFileReader.Close();
    logFileStream.Close();
}
在这段代码中,我为每个人打开了一个新的连接,这样做毫无意义。是否可以将其更改为批量插入或类似的内容?该文件没有任何类型的分隔符,如“;”

我正在使用 MSSQL 2008 R2,
.Net 4.0(此服务器上目前不可能有更高版本)

假定您的logFileStream文件(“文件名”)拥有您正在加载的所有用户,您并没有像您所想的那样打开新连接。它当前使用一个连接截断表,然后从文件名加载所有条目

使此运行更快的唯一方法是使用Bulk Insert SQL Server语句,您可以在此处找到该语句的详细信息:

我希望这能起作用

BULK INSERT Reports FROM  @ReportFile WITH (FIELDTERMINATOR = '|',ROWTERMINATOR = '\n') 
如果文件数据像

M1009|20130502|E400969|ARACIL ALONDRA A|2013050220131202201404022014040220140408
M1009|20130502|N1000533|BARRY PATRICIA| 2013050220131202201404022014040220140408
M1009|20130502|N1001263|GRAYSON JOSEPH| 2013050220131202201404022014040220140408
M1009|20130502|N1026710|GANZI LOUIS R.| 2013050220131202201404022014040220140408
这应该适用于t-sql

您调查过这个类吗?它提供了几种不同的方法将数据从.NET代码大容量复制到SQL Server中。下面是一个使用
DataTable
缓冲记录的示例:

if (File.Exists(fileName))
{
    TruncateTempTable(connection);
    DataTable newRecs = new DataTable();
    newRecs.Columns.Add("per_nummer", typeof (string));
    newRecs.Columns.Add("per_pid", typeof(string));
    newRecs.Columns.Add("per_name", typeof(string));
    using (TextReader tr = File.OpenText(fileName))
    {
        while (tr.Peek() > 0)
        {
            string theLine = tr.ReadLine();
            DataRow newRow = newRecs.NewRow();
            newRow["per_nummer"] = theLine.Substring(0, 7);
            newRow["per_pid"] = theLine.Substring(7, 7);
            newRow["per_name"] = theLine.Substring(14, 20);
            newRecs.Rows.Add(newRow);
        }
    }
    SqlBulkCopy bulkCopy = new SqlBulkCopy(connection);
    bulkCopy.WriteToServer(newRecs);
}

更多详细信息可在上面的MSDN链接中找到。

看起来已经有答案了。为什么要这样做而不是使用SSI和平面文件源?安装需要几分钟。SSIS使用大容量插入,这意味着它每秒可以插入数百行。或者您可以直接使用大容量插入来插入固定宽度的文件,如链接问题所示,使用SSIS会带来其他依赖项,而用户可能不需要这些依赖项。@RichRousseau唯一的“依赖项”是SQL Server本身。这是产品的一部分。事实上,只要选择Task>Import Data,就会出现一个向导,最终生成并执行SSIS包。无论如何,SSIS是ETL作业的正确工具使用单一连接没有错,还有其他选择,比如使用SSIS或bcp。所有可行的替代方案都使用批量导入机制,但其灵活性各不相同。她评论说,他使用的是多个连接——与每个插入连接,这在他发布的代码中并不正确。我在文件中没有字段终止符。就像你的第一只没有“|”的鱼子”