如何在 Web 服务调用中引用数据表列? [英] How to refer to a datatable column in a web service call?

查看:26
本文介绍了如何在 Web 服务调用中引用数据表列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不太了解 .net 或 Web 服务调用.所以如果这个问题之前已经回答过,我很抱歉.

First off, I dont know much about .net or web service calls. So my apologies if this question has been answered before.

我有以下一段代码,它遍历由 SSIS 中的执行 SQL 任务填充的数据集.我需要获取这些值并将它们发送到 Web 服务,以便 Web 服务可以为所欲为.我已经转到可以迭代记录的部分,但我不确定如何引用 Web 服务调用中的列.意思是,如果服务调用是 xyz(val1,val2,val3,val4),我如何将 val 值与我创建的数据表相关联?

I have the following piece of code that iterates over a dataset populated by an Execute SQL Task in SSIS. I need to take these values and send them to the web service so that the web service can do whatever it wants. I have go to the part where I can iterate over the records, but I am not sure how to refer to the columns in the web service call. Meaning, if the service call was xyz(val1,val2,val3,val4), how do I associate val values to the datatable that I have created?

        public void Main()
        {
            // TODO: Add your code here
            var caseObj = Dts.Variables["User::QCase"].Value;
            var serviceURI = Dts.Variables["User::WebServiceURI"].Value.ToString();
            var oleDA = new OleDbDataAdapter();
            var httpBinding = new BasicHttpBinding();

            var dt = new DataTable();
            string sMsg;

            oleDA.Fill(dt, Dts.Variables["User::QCases"].Value);

            foreach (DataRow row in dt.Rows)
                foreach (DataColumn col in dt.Columns)
                {
                    var caseProxy = new CaseService.CServicesProxyClient(httpBinding, new EndpointAddress(serviceURI));
                    sMsg = "";
                    sMsg = sMsg + col.ColumnName + ": " + (col.Ordinal).ToString() + vbCrLf;
                    MessageBox.Show(sMsg);
                    var results = caseProxy.SaveCaseStatusChange(dt, 0, false, null); //This is the service call which will push the values to the web service. dt is the data table.

                }
          /*  if (results)
            {
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            else
            {
                Dts.TaskResult = (int)ScriptResults.Failure;
            }*/
        }

        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion


        public object vbCrLf { get; set; }
    }
}

我希望我提供了足够的详细信息,但如果没有,我将非常乐意提供.感谢您花时间阅读问题.

I hope I have provided sufficient details, but if not, I will be more than happy to provide. Thank you for taking the time to read the question.

推荐答案

除非我遗漏了一些细微差别,否则这很简单.您唯一需要记住的是数组中的第一个元素将位于零位置

Unless there's some nuance I'm missing, this is pretty straight forward. The only thing you have to keep in your head is that the first element in an array is going to be in position zero

您当前的方法,遍历 Columns 集合,可能对允许通用数据集的东西有用,但我假设来自 User::QCases 的数据总是要返回 N 列数据.深度,我不在乎,宽度我在乎.

Your current approach, iterating through the Columns collection, might be useful for something that allows for generic data sets but I'm assuming the data from User::QCases is always going to return N columns of data. Depth, I don't care about, width I do.

您需要查看您的 webservice 方法以了解它期望的数据类型并将您的数据从行强制转换为该类型.此外,您需要处理 NULL(或不处理,根据您的数据),但 .NET 原始类型不允许 NULL 值.您可以使用 ?类型,但这使得响应时间比我可以自由输入的时间更长.

You'll need to look at your webservice method to see what data types it expects and coerce your data from the row into that type. Also, you'll need to deal with NULLs (or not, based on your data) but .NET primitive types don't allow for NULL values. You can "cheat" by using the ? types but that makes this a longer response than I can free type.

foreach (DataRow row in dt.Rows)
{
    // row is going to contain the current row worth of data
    // All you need to do is access the values.

    var results = caseProxy.SaveCaseStatusChange(row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString());

}

有一些机制可以按名称而不是它们的顺序位置对列进行寻址,这将允许您始终访问 Id 列,无论它是第一列还是最后一列.

There are mechanisms for addressing columns by name and not their ordinal position which would allow you always access the Id column whether it was the first or the last column.

如何foreach"一列使用 C# 的数据表?

这篇关于如何在 Web 服务调用中引用数据表列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆