简体   繁体   English

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

[英]how do i convert a string with multiple types in c#

I am getting a cast error for the Url value. 我收到Url值的转换错误。 Does anyone know how to cast a value to a string that contains multiple value types? 有谁知道如何将值转换为包含多个值类型的字符串?

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.");
        }
    }
}

Your question is a bit vague as you're not telling us where the exception is thrown, nor what the exception is exactly. 您的问题有点含糊,因为您没有告诉我们在何处抛出异常,也没有告诉我们确切的异常是什么。

I would suspect that the problem lies in your use of Confirmations.GetString(x) and Recipients.GetString(x) . 我怀疑问题出在您使用Confirmations.GetString(x)Recipients.GetString(x) As I understand, these are results from database calls. 据我了解,这些是数据库调用的结果。 If the respective fields returned by the select don't contain strings, the GetString call will fail. 如果select返回的各个字段不包含字符串,则GetString调用将失败。 You need to use the method that matches the field's type, like GetBoolean , GetInt32 , etc. 您需要使用与字段类型匹配的方法,例如GetBooleanGetInt32等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM