0

这个程序已经写好了,我需要添加一个更新 SQL 语句来更新另一个基于 @IN 的表。声明 SQL 命令后如何让它执行,我可以使用与其他 sql 命令相同的 SQLConnection 吗?该程序所做的是将文件解析为名为个人的列表,然后我只想更新 SQL 中的字段。第一个 SQL 已经写成似乎可以正确执行,现在我需要执行 UpdtHasInv SQL 并实际获取执行更新的代码是我需要帮助的地方。

继承人我到目前为止:

 static void InsertCustomData(Collection<Individuals> individuals)
    {
        #region
        string insertsql = "";

        insertsql = @"Insert into IND (IND_ID, ITEM_NAME,   INDL_ITEM_VALUE, XPTIM)
                        select  IND_ID, '{0}' as Item, '{1}' as val, (current timestamp -      curtime)"
                        FROM    IND 
                        where   IN = @IN";

    // this is SQL I added for update
          **UpdtHasInv = @"UPDATE Ind
                       SET    HAS_I = -1
                       WHERE  IN = @IN";**

        #endregion

        using (DB2Connection conn = DB2Helper.getConnection())
        {
            DB2Command cmd = conn.CreateCommand();                
            cmd.CommandTimeout = 600;
            cmd.Parameters.Add("@IN", "");

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            foreach (Individuals individual in individuals)
            {
                cmd.Parameters["@IN"].Value = individual.In;


                foreach (CustomData customData in individual.CustomData)
                {
                    cmd.CommandText = string.Format(insertIndsql,   customData.Data.Key,            customData.Data.Value);  

我可以写:'cmd.CommandText = UpdtIsInv;' 在这些行之后:

 foreach (Individuals individual in individuals)
            {
                cmd.Parameters["@IN"].Value = individual.in
4

1 回答 1

0

您可以简单地在使用分号UPDATE之后附加您的。INSERT

insertIndsql = @"
INSERT INTO IND (IND_ID, ITEM_NAME,   INDL_ITEM_VALUE, XPTIMESTAMP)
SELECT  INDIVIDUAL_ID, '{0}' as Item, '{1}' as val, (current timestamp - curtime)
FROM    IND 
WHERE   TIN = @TIN;

UPDATE DB2INST1.Individual
SET    HAS_INVESTMENT = -1
WHERE  TIN = @TIN;";

执行时,两条语句将作为同一命令的一部分执行。

于 2012-10-10T14:22:55.007 回答