I am trying to create an Upsert into a MariaDB with an ODBC driver, but I have problems with the Update part. I can't find a component to Update single Rows with ODBC.
I tried the Script Component, but I can't get it to run proberly (C# isn't my strong suite).
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?
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.
The rest is from here: https://msdn.microsoft.com/en-us/library/ms345157.aspx
In my opinion it looks more complicated then it should, but it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.