简体   繁体   English

带ODBC驱动程序的SSIS Upsert

[英]SSIS Upsert with ODBC driver

I am trying to create an Upsert into a MariaDB with an ODBC driver, but I have problems with the Update part. 我正在尝试使用ODBC驱动程序在MariaDB中创建Upsert,但是Update部分存在问题。 I can't find a component to Update single Rows with ODBC. 我找不到用ODBC更新单行的组件。

Overview Dataflow Task 概述数据流任务

I tried the Script Component, but I can't get it to run proberly (C# isn't my strong suite). 我尝试了脚本组件,但无法让它运行探针(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);

}

Can someone point to me the Error? 有人可以指出我的错误吗?

Another Question is: Is it possible to create the Connection in the PreExecute method, so I don't open and close a connection for every Row? 另一个问题是:是否可以在PreExecute方法中创建Connection,所以我不为每个Row打开和关闭连接?

Not Sure if answering the own question is the right way to solve it, but here we go: 不确定是否回答自己的问题是解决问题的正确方法,但是我们开始:

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();
}  

Declaring the connections outside the methods(yeah i know, i am stupid) lets you use the pre and post method to only open and close the connection once. 在方法之外声明连接(是的,我知道,我很愚蠢)使您可以使用pre和post方法仅打开和关闭一次连接。

The rest is from here: https://msdn.microsoft.com/en-us/library/ms345157.aspx 其余的是从这里: https : //msdn.microsoft.com/en-us/library/ms345157.aspx

In my opinion it looks more complicated then it should, but it works. 在我看来,它看起来比它应该的要复杂,但是它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM