ASP.net SQL数据读取器将WebLink转换为XML

时间:2016-01-26 12:35:35

标签: c# sql asp.net xml

我正在尝试从SQL中提取一些信息以放入网络链接我有一些帮助将代码放在一起然后出了问题。

这种方式有效;

SQLDB,获取2个值>将2个值放入网址>阅读URL给我的XML>放入文本框。

这是我当前遇到问题的一大堆代码;

SqlConnection conn = null;

try
{
    string sql = "SELECT ApiKey, VeriF FROM dbo.Users WHERE UserName = @username";

    conn = new SqlConnection(ConfigurationManager.ConnectionStrings["READER"].ConnectionString);

    SqlCommand cmd = new SqlCommand(sql, conn);

    SqlParameter UserN = new SqlParameter();
    UserN.ParameterName = "@username";
    UserN.Value = User.Identity.Name;
    cmd.Parameters.Add(UserN);

    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();

    string url;

    if (reader.HasRows)
    {
        url = string.Format(
            "https://api.eveonline.com/account/Characters.xml.aspx?keyID={0}&&vCODE={1}",
            reader.GetString(0),
            reader.GetString(1));

        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(url);
        XmlNodeList ndlist = xdoc.SelectNodes("/eveapi/result/rowset/row");
        foreach(XmlNode xmlnode in ndlist)
        {                        
                ename.Text = xmlnode.Attributes["Name"].InnerText;
        }                

    }

    else
    {
        url = null; // username not found                     
    }

}
finally
{
    if (conn.State == System.Data.ConnectionState.Open)
        conn.Close();
}

它应该从SQL中提取的值是:

Apikey = 4602486
VeriF = BHGVeXQkRLKLkIkZQHdeyUxmUz9EfUwbvGzoc2eO4ZR8kRMYxk8PbD4LMwLF7BvH

,最后一个链接应为:

https://api.eveonline.com/account/Characters.xml.aspx?keyID=4602486&&vCODE=BHGVeXQkRLKLkIkZQHdeyUxmUz9EfUwbvGzoc2eO4ZR8kRMYxk8PbD4LMwLF7BvH

我在这部分收到错误:

Line 52:                 if (reader.HasRows)

错误描述是:

  

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

这是我编辑过的代码;但我仍然有同样的问题:@

 private void SQLC()
    {
        SqlConnection conn = null;

        try
        {
            string URL;
            string sql = "SELECT ApiKey, VeriF FROM dbo.Users WHERE UserName = @username";

            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["READER"].ConnectionString);

            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlParameter UserN = new SqlParameter();
            UserN.ParameterName = "@username";
            UserN.Value = User.Identity.Name;
            cmd.Parameters.Add(UserN);

            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    URL = string.Format(
                   "https://api.eveonline.com/char/AccountBalance.xml.aspx?keyID={0}&&vCODE={1}",
                     dr.GetString(0),
                     dr.GetString(1));


                    XmlDocument xdoc = new XmlDocument();
                    xdoc.Load(URL);
                    XmlNodeList ndlist = xdoc.SelectNodes("/eveapi/result/key/rowset/row");
                    foreach (XmlNode xmlnode in ndlist)
                    {
                        iskammount.Text = xmlnode.Attributes["characterName"].InnerText;
                    }
                }
            }
            else {
                URL = iskammount.Text = "Error";
            } 
        }
        finally
        {
            if (conn.State == System.Data.ConnectionState.Open)
                conn.Close();
        }
    }

出现新错误:

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.

0 个答案:

没有答案