用于将记录插入数据库的 C# 或 BIML 代码
C# or BIML code for inserting records into db
我想在 biml 代码为 运行 并且包已完成扩展时将值插入数据库,这可以使用 BIML 或 c# 实现吗?
我在我的数据库中创建了一个名为 BIML 扩展的 table 并且我有 test.biml 加载包 test.dtsx 每当 BIML 扩展完成时,应将一条记录插入我的table 扩展已经完成。
如果您有任何问题或需要任何其他信息,请告诉我。
来自评论
我试过你的代码
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你能告诉我背后的问题吗?
在最简单的形式中,您的 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 连接管理器并使用 class 方法。您可以从 Biml Connections 集合中提取连接字符串(取决于它在其中执行的层)等。
注意事项
根据产品(BimlStudio 与 BimlExpress)的不同,可能会有一个后台进程编译您的 BimlScript,以确保所有元数据都已准备好供智能感知接收。您可能需要将该逻辑存储到一个非常 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);
#>
这是您要解决的问题吗?
寻址comment/questions
鉴于
的查询
string SrcTablequery=@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES (@audit_id, @Package,@audit_Logtime)";
由于未指定@audit_id而出错。这是有道理的 - 此查询指定它将提供三个变量并提供 none。
选项 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) 创建一个Parameter list并关联values
除了引用之外还有很多很多例子,但它是第一次成功。只需忽略 Access 连接字符串并使用您的原始值。
我想在 biml 代码为 运行 并且包已完成扩展时将值插入数据库,这可以使用 BIML 或 c# 实现吗?
我在我的数据库中创建了一个名为 BIML 扩展的 table 并且我有 test.biml 加载包 test.dtsx 每当 BIML 扩展完成时,应将一条记录插入我的table 扩展已经完成。
如果您有任何问题或需要任何其他信息,请告诉我。
来自评论
我试过你的代码
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你能告诉我背后的问题吗?
在最简单的形式中,您的 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 连接管理器并使用 class 方法。您可以从 Biml Connections 集合中提取连接字符串(取决于它在其中执行的层)等。
注意事项
根据产品(BimlStudio 与 BimlExpress)的不同,可能会有一个后台进程编译您的 BimlScript,以确保所有元数据都已准备好供智能感知接收。您可能需要将该逻辑存储到一个非常 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);
#>
这是您要解决的问题吗?
寻址comment/questions
鉴于
的查询string SrcTablequery=@"INSERT INTO BIML_audit (audit_id,Package,audit_Logtime) VALUES (@audit_id, @Package,@audit_Logtime)";
由于未指定@audit_id而出错。这是有道理的 - 此查询指定它将提供三个变量并提供 none。
选项 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) 创建一个Parameter list并关联values
除了引用之外还有很多很多例子,但它是第一次成功。只需忽略 Access 连接字符串并使用您的原始值。