2016-08-05 134 views
0

我正在嘗試使用ODBC驅動程序創建MariaDB的Upsert,但是我遇到了更新部分的問題。我找不到用ODBC更新單行的組件。帶有ODBC驅動程序的SSIS Upsert

Overview Dataflow Task

我試圖腳本組件,但我不能讓它proberly運行(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,因此我不打開和關閉每個Row的連接?

+0

你會得到什麼錯誤信息? –

回答

0

不知道是否回答了自己的問題是解決這個問題的正確途徑,但在這裏我們去:

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

在我看來,它看起來更加複雜,那麼它應該,但它的作品。