C# 如何在c中转换具有多种类型的字符串

C# 如何在c中转换具有多种类型的字符串,c#,C#,我收到Url值的强制转换错误。有人知道如何将值强制转换为包含多种值类型的字符串吗 CmdRecipients = new OleDbCommand(QueryRecipients, ConnRecipients); Recipients = CmdRecipients.ExecuteReader(); while (Recipients.Read()) { Url = "https://bulktext.vodafone.ie/sendmessage.aspx" +

我收到Url值的强制转换错误。有人知道如何将值强制转换为包含多种值类型的字符串吗

CmdRecipients = new OleDbCommand(QueryRecipients, ConnRecipients);
Recipients = CmdRecipients.ExecuteReader();

while (Recipients.Read())
{
    Url = "https://bulktext.vodafone.ie/sendmessage.aspx"
          + "?user=user&password=user1!&api_id=89&to="
          + Recipients.GetString(8)
          + "&text=" + Confirmations.GetString(4)
          + "%0d%0a" + Confirmations.GetString(5)
          + "%0d%0a" + Confirmations.GetString(6)
          + "&from=Service";

    Console.Write("Sending text to " + Recipients.GetString(8) + " ... ");

    //Send SMS Here
    HttpWebRequest req = WebRequest.Create(Url) as HttpWebRequest;
    string result = null;
    using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
    {
        StreamReader reader = new StreamReader(resp.GetResponseStream());
        result = reader.ReadToEnd();
        if (result == "0\nRequest deferred successfully")
        {
            QueryRecipients = "UPDATE [Test].[dbo].[MessagesToSend] SET Previous_Status = Current_Status,"
                              + "Current_Status = \"Sent\", DateTime_sent = Now() "
                              + "WHERE Task_ID = \"" + Recipients.GetString(2) + "\";";

            OleDbCommand Update = new OleDbCommand(QueryRecipients, ConnConfirmations);
            Update.ExecuteNonQuery();

            Console.WriteLine("Done!");
        }
        else
        {
            Console.WriteLine("The text wasn't delivered properly.");
        }
    }
}

您的问题有点模糊,因为您没有告诉我们异常抛出的位置,也没有告诉我们异常的确切内容


我怀疑问题在于您使用Confirmations.GetStringx和Recipients.GetStringx。据我所知,这些是数据库调用的结果。如果select返回的各个字段不包含字符串,则GetString调用将失败。您需要使用与字段类型匹配的方法,如GetBoolean、GetInt32等。

具有多种类型的字符串-不完全确定这是什么意思。Url是什么类型的?如果它不是一个字符串,那么就不能像在代码中那样为它指定字符串值。此外,如果您发布了错误,那么错误本身可能非常有用……SQL注入警告!您正在对收件人和确认调用SqlDataReader.GetStringint i-您试图获取的字段是否实际为字符串值?如果不是,则将引发InvalidCastException。Url将是字符串或System.Uri,因为它稍后会在WebRequest.CreateUrl中使用-请编辑原始帖子以显示Url变量的声明,以提供更多信息。