Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.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# 如何在表中插入文件夹中可用文件的列表_C#_Sql_Sql Server_Ssis_Ssis 2008 - Fatal编程技术网

C# 如何在表中插入文件夹中可用文件的列表

C# 如何在表中插入文件夹中可用文件的列表,c#,sql,sql-server,ssis,ssis-2008,C#,Sql,Sql Server,Ssis,Ssis 2008,作为标题,我想在SQL表中插入使用C#的文件夹中可用的文件列表。所有文件都是CSV 在脚本结束时,我无法在表中找到任何记录,但我没有任何错误代码。 我想将此代码用于ssis脚本 SqlConnection theSqlServer = new SqlConnection(@"Server=sql\insta; Database=database; Trusted_Connection=True;"); theSqlServer.Open(); SqlCommand theSqlComma

作为标题,我想在SQL表中插入使用C#的文件夹中可用的文件列表。所有文件都是CSV

在脚本结束时,我无法在表中找到任何记录,但我没有任何错误代码。 我想将此代码用于ssis脚本

 SqlConnection theSqlServer = new SqlConnection(@"Server=sql\insta; Database=database; Trusted_Connection=True;");
 theSqlServer.Open();

 SqlCommand theSqlCommand = new SqlCommand("INSERT INTO [File] ([FileName]) VALUES (" + fileName + " )", theSqlServer);

 MessageBox.Show(fileName);
 theSqlServer.Close();

您能帮我找出这段代码中的错误吗?

如果您有权访问SQL Server探查器,您可以捕获服务器再次发出的实际SQL语句(如果确实发出了!)

首先,您创建了一个
SqlCommand
,但您没有执行它,您需要使用sqlcommand.ExecuteNonQuery()

使用SSI实现这一目标的另一种方法(更好的方法)

使用SQL任务来实现这一点不是最佳做法(它将为每个命令打开一个连接,并且您的查询没有参数化,因此您有一个
SQL注入警报!!

在SSIS中,您可以执行以下操作:

假设已将OLEDB连接管理器添加到要导入数据的SQL server中

  • 添加类型为
    System.String
    的SSIS变量
    User::FolderPath
    ,其值等于包含CSV文件的旧路径
  • 添加数据流任务
    DFT导入
  • 在数据流任务内部添加脚本组件(将其用作源)
  • 在脚本组件中添加类型为
    DT_WSTR
    和长度为
    4000
  • User::FolderPath
    变量添加到脚本
    ReadOnlyVariables
  • 在脚本窗口中编写以下代码(我使用的是Vb.net):

  • 添加一个OLEDB目的地

  • 将脚本输出连接到OLEDB连接

  • 您缺少命令的执行-
    sqlcommand.ExecuteNonQuery()
    请将此作为答案发布,让我们向上投票:-)!抱歉,这是一个复制\粘贴错误!:P
    Private strFolderPath As String = String.Empty
    Public Overrides Sub PreExecute()
    
        MyBase.PreExecute()
    
        strFolderPath = Variables.RowCount
    
    End Sub
    
    Public Overrides Sub CreateNewOutputRows()
    
        Dim strFiles() As String = IO.Directory.GetFiles(strFolderPath, "*.csv", IO.SearchOption.AllDirectories)
    
        For Each strFiles As String In strFiles
    
            With Output0Buffer
    
                .AddRow()
                .OutFilename = strFiles
    
            End With
    
        Next
    
    End Sub