使用ODBC驱动程序的SSIS Upsert

时间:2016-08-05 09:39:31

标签: c# ssis odbc upsert

我正在尝试使用ODBC驱动程序将Upsert创建到MariaDB中,但是我遇到了更新部分的问题。我找不到使用ODBC更新单行的组件。

Overview Dataflow Task

我尝试过脚本组件,但是我无法让它运行(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方法中创建Connection,因此我不打开和关闭每一行的连接?

1 个答案:

答案 0 :(得分:1)

不确定如果回答自己的问题是解决问题的正确方法,但我们走了:

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

在我看来,它看起来应该更复杂,但它确实有效。