【问题标题】:C# or BIML code for inserting records into db用于将记录插入数据库的 C# 或 BIML 代码
【发布时间】:2019-06-19 16:02:52
【问题描述】:

我想在运行 biml 代码并且包已完成扩展时将值插入数据库,这可以使用 BIML 或 c# 吗?

我在我的数据库中创建了一个名为 BIML 扩展的表,并且我有 test.biml,它会在 BIML 扩展完成时加载包 test.dtsx,一条记录应插入到我的表中,表明扩展已完成。

如果您有任何问题或需要任何其他信息,请告诉我。

来自 cmets

我试过你的代码

string connectionString = "Data Source=hq-dev-sqldw01;Initial Catalog=IM_Stage;Integrated Security=SSPI;Provider=SQLNCLI11.1"; 
string SrcTablequery=@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES (@audit_id, @Package,@audit_Logtime)"; 
DataTable dt = ExternalDataAccess.GetDataTable(connectionString,SrcTablequery);

下面有一个错误必须声明标量变量audit_id你能告诉我背后的问题吗?

【问题讨论】:

    标签: ssis biml


    【解决方案1】:

    在最简单的形式中,您的 Biml 脚本中会包含这样的内容

    // Define the connection string to our database 
    string connectionStringSource = @"Server=localhost\dev2012;Initial Catalog=AdventureWorksDW2012;Integrated Security=SSPI;Provider=SQLNCLI11.1";
    
    // Define the query to be run after *ish* expansion
    string SrcTableQuery = @"INSERT INTO dbo.MyTable (BuildDate) SELECT GETDATE()";
    
    // Run our query, nothing populates the data table
    DataTable dt = ExternalDataAccess.GetDataTable(connectionStringSource, SrcTableQuery);
    

    有很多不同的方法可以做到这一点 - 您可以创建自己的 OLE/ADO 连接管理器并使用类方法。您可以从 Biml Connections 集合中提取连接字符串(取决于执行此操作的层)等。

    注意事项

    根据产品(BimlStudio 与 BimlExpress)的不同,可能会有一个后台进程编译您的 BimlScript,以确保所有元数据都已准备好供 intellisense 提取。您可能需要将该逻辑存储到一个非常 high tiered Biml 文件中,以确保仅在您准备好时才调用它。例如

    <#@ template tier="999" #>
    <#
    // Define the connection string to our database 
    string connectionStringSource = @"Server=localhost\dev2012;Initial Catalog=AdventureWorksDW2012;Integrated Security=SSPI;Provider=SQLNCLI11.1";
    
    // Define the query to be run after *ish* expansion
    string SrcTableQuery = @"INSERT INTO dbo.MyTable (BuildDate) SELECT GETDATE()";
    // Run our query, nothing populates the data table
    DataTable dt = ExternalDataAccess.GetDataTable(connectionStringSource, SrcTableQuery);
    #>
    

    这就是你要解决的问题吗?

    处理评论/问题

    给定查询

    string SrcTablequery=@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES (@audit_id, @Package,@audit_Logtime)"; 
    

    由于未指定@audit_id 而出错。这是有道理的 - 此查询指定它将提供三个变量,但没有提供。

    选项 1 - 懒惰的方式

    最快的解决方法是以这样的方式重新定义您的查询

    string SrcTablequery=string.Format(@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES ({0}, '{1}', '{2})'", 123, "MyPackageName", DateTime.Now); 
    

    我使用字符串库的 Format 方法将实际值注入占位符。我假设 audit_id 是一个数字,另外两个是字符串,因此 1 和 2 周围有刻度线。您需要为您的审核 ID 定义一个值,但我以 123 为例。如果我正在生成包,我的 packageName 可能会有一个变量,所以我也会在我的声明中引用它。

    选项 2 - 更好的方法

    将第三行替换为 .NET 库用法,就像您在 using parameters inserting data into access database 上的 heikofritz 中看到的那样。

    1) 创建数据库连接 2) 打开连接 3)创建命令对象并与连接关联 4)指定您的语句(使用?作为您的序号标记而不是命名参数,因为这是oledb) 5)创建参数列表并与值关联

    除了引用之外,还有很多很多例子,但它是第一个热门。只需忽略 Access 连接字符串并使用您的原始值。

    【讨论】:

    • 我试过你的代码字符串 connectionString = "Data Source=hq-dev-sqldw01;Initial Catalog=IM_Stage;Integrated Security=SSPI;Provider=SQLNCLI11.1"; string SrcTablequery=@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES (@audit_id,@Package,@audit_Logtime)"; DataTable dt = ExternalDataAccess.GetDataTable(connectionString,SrcTablequery);下面有一个错误必须声明标量变量audit_id你能告诉我它背后的问题吗?
    • 感谢您的帮助,我有上面的代码,但它有一些问题,必须声明 (@audit_id)。你能告诉我如何解决它吗?
    • 我试过其他方法 string connectionString = "Data Source=HQ-DEV-SQLDW01;Initial Catalog=IM_STAGE;Integrated Security=SSPI;Auto Translate=False;Provider=System.Data.SqlClient. SqlConnection,System.Data,版本=2.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089;"; SqlConnection con = new SqlConnection(connectionString); con.open(); SqlCommand cmd = new SqlCommand("INSERT INTO audit.BIML_audit (Package,audit_Logtime) VALUES ('xyz', GETDATE())", con); cmd.ExecuteNonQuery();
    • 上面的代码产生以下错误'SqlConnection'不包含'open'的定义,并且找不到接受'SqlConnection'类型的第一个参数的扩展方法'open'(你是否缺少一个使用指令还是程序集引用?
    • C# 中的方法区分大小写,因此con.Open
    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多