2010-05-24 62 views
1

我無法弄清楚我的生活..我使用SQL數據集查詢來迭代到一個類對象,它充當Flex的數據模型...最初我使用VB.net,但現在需要轉換爲C#..除了創建DataRow arow的最後一部分,然後嘗試將DataSet值添加到類(結果類)之外,此轉換完成...我收到一條錯誤消息..任何人都可以幫助轉換此VB Webservice?

' 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 
+1

您還應該將SqlConnection,SqlCommand和SqlDataAdapter放入'using'塊中,以便在引發異常時將它們丟棄。 – 2010-05-24 01:24:25

+0

謝謝約翰當我完成轉換後,我會繼續努力。 – CraigJSte 2010-05-24 20:34:47

回答

0

正如其他人已經說過的,您需要將結果的成員公開。

此外,一旦你這樣做,你會用下列行打的錯誤:

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中隱含的工作,但你需要做這些明確in C#

eg

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

任何想法爲什麼我在DataRow arow對象上出現此錯誤? 結果[] dts = new結果[myds.Tables [0] .Rows.Count]; DataRow arow; (DataRow arow in myds.Tables [0] .Rows) { dts [i] = new Results(); dts [i] .Ticker = arow [「Ticker」]。ToString(); – CraigJSte 2010-05-24 20:35:37

+0

這是無法讀取..無論如何,我得到一個錯誤的arow對象.....我聲明它使用C#DataRow arow然後我用它在這個聲明 foreach(DataRow arow in myds.Tables [0] .Rows)this是程序所在的地方「一個名爲'arow'的變量不能在這個範圍內聲明,因爲它會給它一個不同的含義.... ?? – CraigJSte 2010-05-24 20:37:19

+0

我發佈了一個關於這個主題的單獨問題......'C#Conversion VB.net網絡服務' – CraigJSte 2010-05-24 20:42:57

0

正如其他人指出C#中的默認接取修飾符是私人所以你必須做出「字符串北京時間」和領域公共的其餘部分。此外,除非您計劃稍後使用方法擴展它,否則您應該將其作爲結構體。

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

爲什麼要使用具有值語義的結構? – 2010-05-24 01:25:21

1

C#中類成員的默認access modifier是私有的。嘗試將您的字段更改爲公開(就像您在VB代碼中一樣)。

1

我認爲你需要把它全部公開,因爲你會碰到每個其他屬性相同的錯誤。

public class Results { 
     public string Ticker; 
     public string BuyDate; 
     public decimal Buy; 
     public string SellDate; 
     public decimal Sell; 
     public string Profit; 
     public decimal Period; 
    } 
相關問題