SSIS与ODBC驱动程序发生冲突
本文关键字:冲突 驱动程序 ODBC SSIS | 更新日期: 2023-09-27 18:01:09
我正试图用ODBC驱动程序在MariaDB中创建一个Upsert,但更新部分有问题。我找不到用ODBC更新单行的组件。
概述数据流任务
我尝试了脚本组件,但我无法让它正常运行(C#不是我的强大套件(。
public override void Eingabe0_ProcessInputRow(Eingabe0Buffer Row)
{
ConnectionManagerOdbc mariaDbConnection = (ConnectionManagerOdbc)base.Connections.OTRSDB;
System.Data.SqlClient.SqlConnection sqlConn = (System.Data.SqlClient.SqlConnection) mariaDbConnection.AcquireConnection(null);
System.Data.SqlClient.SqlCommand sqlComm;
sqlConn.Open();
String sqlCommand = "UPDATE xdwdata.Contracts " +
"SET " +
" reference = " + Row.reference.ToString() +
" customer = " + Row.customer.ToString() +
" contract = " + Row.contract.ToString() +
" status = test " + Row.status.ToString() +
" change_date = " + Row.changedate.ToString() +
" WHERE " +
" id = " + Row.id.ToString() +
" AND client = " + Row.client.ToString();
sqlComm = new System.Data.SqlClient.SqlCommand(sqlCommand, sqlConn);
sqlComm.ExecuteNonQuery();
mariaDbConnection.ReleaseConnection(sqlConn);
}
有人能指出错误吗?
另一个问题是:是否可以在PreExecute方法中创建连接,这样我就不会为每一行打开和关闭连接?
不确定回答自己的问题是否是解决问题的正确方法,但我们来看看:
OdbcConnection odbcConn;
OdbcCommand odbcCmd;
OdbcParameter odbcParam;
public override void AcquireConnections(object Transaction)
{
string connectionString;
connectionString = this.Connections.OTRSDB.ConnectionString;
odbcConn = new OdbcConnection(connectionString);
odbcConn.Open();
}
public override void PreExecute()
{
odbcCmd = new OdbcCommand("UPDATE xdwdata_Contracts " +
"SET " +
" reference = ? " +
" ,customer = ? " +
" ,contract = ? " +
" ,status = ? " +
" ,change_date = ? " +
" WHERE " +
" id = ? " +
" AND client = ? ", odbcConn);
odbcParam = new OdbcParameter("@reference", OdbcType.VarChar, 128);
odbcCmd.Parameters.Add(odbcParam);
odbcParam = new OdbcParameter("@customer", OdbcType.VarChar, 40);
odbcCmd.Parameters.Add(odbcParam);
odbcParam = new OdbcParameter("@contract", OdbcType.VarChar, 14);
odbcCmd.Parameters.Add(odbcParam);
odbcParam = new OdbcParameter("@status", OdbcType.VarChar, 16);
odbcCmd.Parameters.Add(odbcParam);
odbcParam = new OdbcParameter("@change_date", OdbcType.DateTime);
odbcCmd.Parameters.Add(odbcParam);
odbcParam = new OdbcParameter("@id", OdbcType.Int, 11);
odbcCmd.Parameters.Add(odbcParam);
odbcParam = new OdbcParameter("@client", OdbcType.VarChar, 2);
odbcCmd.Parameters.Add(odbcParam);
}
public override void Eingabe0_ProcessInputRow(Eingabe0Buffer Row)
{
odbcCmd.Parameters["@reference"].Value = Row.reference;
odbcCmd.Parameters["@customer"].Value = Row.customer;
odbcCmd.Parameters["@contract"].Value = Row.contract;
odbcCmd.Parameters["@status"].Value = Row.status;
odbcCmd.Parameters["@change_date"].Value = Row.changedate;
odbcCmd.Parameters["@id"].Value = Row.id;
odbcCmd.Parameters["@client"].Value = Row.client;
odbcCmd.ExecuteNonQuery();
}
public override void ReleaseConnections()
{
odbcConn.Close();
}
声明方法之外的连接(是的,我知道,我很愚蠢(可以让你使用pre和post方法只打开和关闭一次连接。
其余内容如下:https://msdn.microsoft.com/en-us/library/ms345157.aspx
在我看来,它看起来比它应该的更复杂,但它是有效的。