【问题标题】:Can anyone help convert this VB Webservice?任何人都可以帮助转换这个 VB Webservice 吗?
【发布时间】:2010-05-24 09:22:46
【问题描述】:

我这辈子都想不通。我正在使用 SQL DataSet 查询来迭代一个类对象,该对象充当 Flex 的数据模型...最初我使用 VB.net,但现在需要转换为 C#.. 除了最后一部分我创建 DataRow arow 然后尝试将 DataSet 值添加到类(结果类)之外,此转换已完成...我收到一条错误消息..

“VTResults.Results.Ticker”因其保护级别而无法访问 (这是在底部)

 using System;
 using System.Web;
 using System.Collections;
 using System.Web.Services;
 using System.Web.Services.Protocols;
 using System.Data;
 using System.Data.SqlClient;
 using System.Configuration;
 /// <summary>
 /// Summary description for VTResults
 /// </summary>
 [WebService(Namespace = "http://velocitytrading.net/ResultsVT.aspx")]
 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 public class VTResults : System.Web.Services.WebService {
     public class Results {
         string Ticker;
         string BuyDate;
         decimal Buy;
         string SellDate;
         decimal Sell;
         string Profit;
         decimal Period;
     }
     [WebMethod]
     public Results[] GetResults() {
         string conn =                
 ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
    SqlConnection myconn = new SqlConnection(conn);
    SqlCommand mycomm = new SqlCommand();
    SqlDataAdapter myda = new SqlDataAdapter();
    DataSet myds = new DataSet();

    mycomm.CommandType = CommandType.StoredProcedure;
    mycomm.Connection = myconn;
    mycomm.CommandText = "dbo.Results";

    myconn.Open();
    myda.SelectCommand = mycomm;
    myda.Fill(myds);
    myconn.Close();
    myconn.Dispose();

    int i = 0;

    Results[] dts = new Results[myds.Tables[0].Rows.Count];
    foreach(DataRow arow in myds.Tables[0].Rows)
    {
        dts[i] = new Results();
        dts[i].Ticker = arow["Ticker"];
        dts[i].BuyDate = arow["BuyDate"];
        dts[1].Buy = arow["Buy"];
        dts[i].SellDate = arow["SellDate"];
        dts[i].Sell = arow["Sell"];
        dts[i].Profit = arow["Profit"];
        dts[i].Period = arow["Period"];
        i+=1;
    }
    return dts;
     }    
   }

运行良好的 VB.NET WEBSERVICE 我正在尝试转换为 C#。

 Imports System.Web
 Imports System.Web.Services
 Imports System.Web.Services.Protocols
 Imports System.Data
 Imports System.Data.SqlClient
 <WebService(Namespace:="http://localhost:2597/Results/ResultsVT.aspx")> _
 <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
 <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
 Public Class VTResults
     Inherits System.Web.Services.WebService
     Public Class Results
         Public Ticker As String
         Public BuyDate As String
         Public Buy As Decimal
         Public SellDate As String
         Public Sell As Decimal
         Public Profit As String
         Public Period As Decimal
     End Class
     <WebMethod()> _
     Public Function GetResults() As Results()
         Try
             Dim conn As String =  
 ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString
             Dim myconn = New SqlConnection(conn)
             Dim mycomm As New SqlCommand
             Dim myda As New SqlDataAdapter
             Dim myds As New DataSet

             mycomm.CommandType = CommandType.StoredProcedure
             mycomm.Connection = myconn
             mycomm.CommandText = "dbo.Results"

             myconn.Open()
             myda.SelectCommand = mycomm
             myda.Fill(myds)
             myconn.Close()
             myconn.Dispose()

             Dim i As Integer
             i = 0

             Dim dts As Results() = New Results(myds.Tables(0).Rows.Count - 1) {}
             Dim aRow As DataRow

             For Each aRow In myds.Tables(0).Rows
                 dts(i) = New Results
                 dts(i).Ticker = aRow("Ticker")
                 dts(i).BuyDate = aRow("BuyDate")
                 dts(i).Buy = aRow("Buy")
                 dts(i).SellDate = aRow("SellDate")
                 dts(i).Sell = aRow("Sell")
                 dts(i).Profit = aRow("Profit")
                 dts(i).Period = aRow("Period")
                 i += 1
             Next
             Return dts

         Catch ex As DataException
             Throw ex
         End Try
     End Function

 End Class

【问题讨论】:

  • 您还应该将 SqlConnection、SqlCommand 和 SqlDataAdapter 放入 using 块中,以便在抛出异常时将它们处理掉。
  • 谢谢 John,我会在完成转换后继续努力。

标签: c# vb.net web-services


【解决方案1】:

C# 中类成员的默认access modifier 是私有的。尝试将您的字段更改为公开(就像您在 VB 代码中所做的那样)。

【讨论】:

    【解决方案2】:

    我认为您需要将其全部公开,因为您将遇到其他每个属性的相同错误。

    public class Results { 
             public string Ticker; 
             public string BuyDate; 
             public decimal Buy; 
             public string SellDate; 
             public decimal Sell; 
             public string Profit; 
             public decimal Period; 
         }
    

    【讨论】:

      【解决方案3】:

      正如其他人已经说过的,您需要公开结果的成员。

      此外,一旦你这样做了,你会遇到以下几行的错误:

          dts[i].Ticker = arow["Ticker"];
          dts[i].BuyDate = arow["BuyDate"];
          dts[1].Buy = arow["Buy"];
          dts[i].SellDate = arow["SellDate"];
          dts[i].Sell = arow["Sell"];
          dts[i].Profit = arow["Profit"];
          dts[i].Period = arow["Period"];
      

      其中的转换在 VB.NET 中隐式工作,但您需要在 C# 中显式地进行它们

      例如

          dts[i].Ticker = arow["Ticker"].ToString();
      

      【讨论】:

      • 知道为什么我在 DataRow arow 对象上收到此错误吗?结果[] dts = 新结果[myds.Tables[0].Rows.Count]; DataRow 行; foreach(myds.Tables[0].Rows 中的 DataRow arow){ dts[i] = new Results(); dts[i].Ticker = arow["Ticker"].ToString();
      • 那是不可读的.. 无论如何我在 arow 对象上得到一个错误.....我使用 C# DataRow arow 声明它然后我在这个语句中使用它 foreach(DataRow arow in myds.Tables[ 0].Rows) 这是程序说“不能在这个范围内声明一个名为'arow'的变量,因为它会给它一个不同的含义...... ??
      • 我在这个主题上发布了一个单独的问题...'C# Conversion VB.net Webservice'
      【解决方案4】:

      正如其他人所说,C# 中的默认访问修饰符是 private,因此您必须将“string Ticker”和其余字段设为 public。此外,除非您打算稍后使用方法扩展它,否则您可能应该将其设为结构。

      public class Results
      {
          public string Ticker;
          public string BuyDate;
          public decimal Buy;
          public string SellDate;
          public decimal Sell;
          public string Profit;
          public decimal Period;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-13
        • 1970-01-01
        • 2013-02-28
        • 1970-01-01
        相关资源
        最近更新 更多