这个web服务几乎从VB转换为C#,除了我在DataRow arow对象上显示如下所示的错误,当我在下面的foreach语句中使用它时,用DataSet对象填充结果类...任何想法? ?
错误:名为'arow'的局部变量不能在此范围内声明,因为它会给'arow'赋予不同的含义,'arow'已在'父或当前'范围内用于表示其他内容
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 {
public string Ticker;
public string BuyDate;
public string Buy;
public string SellDate;
public string Sell;
public string Profit;
public string 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];
DataRow arow;
foreach(DataRow arow ** in myds.Tables[0].Rows)
{
dts[i] = new Results();
dts[i].Ticker = arow["Ticker"].ToString();
dts[i].BuyDate = arow["BuyDate"].ToString();
dts[1].Buy = arow["Buy"].ToString();
dts[i].SellDate = arow["SellDate"].ToString();
dts[i].Sell = arow["Sell"].ToString();
dts[i].Profit = arow["Profit"].ToString();
dts[i].Period = arow["Period"].ToString();
i+=1;
}
return dts;
}
}
** ERROR ON THIS 'AROW' OBJECT
答案 0 :(得分:2)
aRow
被宣布两次,一次在foreach
,另一次在其上方。删除foreach上方的DataRow aRow
,你应该很好。