我无法弄清楚我的生活..我正在使用SQL DataSet Query迭代到一个类对象,它充当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
答案 0 :(得分:1)
C#中类成员的默认access modifier是私有的。尝试将您的字段更改为公共字段(就像您在VB代码中一样)。
答案 1 :(得分: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;
}
答案 2 :(得分: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;
}
答案 3 :(得分: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中隐含地进行转换,但你需要在C#中明确地进行转换
e.g。
dts[i].Ticker = arow["Ticker"].ToString();