【问题标题】:Can't append file extension to string C# asp.net core无法将文件扩展名附加到字符串 C# asp.net core
【发布时间】:2018-04-14 01:47:11
【问题描述】:

我正在尝试将文件扩展名连接到字符串,但它不起作用。我有一本这样的字典:

Dictionary<string, List<string>> myDict = new Dictionary<string, List<string>>();

从 SQL Server 存储过程的结果中填充键:

List<string> myList = new List<string>();

        using (SqlConnection conn = GetConnection())
        {
            conn.Open();
            SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "Execute getTitles @Title";
            comm.Parameters.Add("@Title", SqlDbType.VarChar).Value = title;

            using (var reader = comm.ExecuteReader())
            {
                while (reader.Read())
                {
                    myList.Add(reader["Title"].ToString());
                }
            }
            conn.Close();
        }

这个列表被加载到字典键中,但我不会进入值,因为它不应该是相关的,你可能不想阅读几十行代码。

一旦我准备好使用这些数据,我就会这样做:

string fileName = title + ".jpg";

但是当我调试时:

Debug.WriteLine("fileName: " + fileName);

它只写标题值(没有“.jpg”)。这可能是什么?

【问题讨论】:

  • 您的代码示例中不包含问题。您可能需要稍微隔离一下并发布相关代码。没有办法回答这个问题,因为它是目前写的。
  • 可能标题包含换行符?
  • 尝试将其归结为minimal reproducible example。我怀疑这样做会让您自己找到它;但如果没有,其他人届时将能够提供帮助。就目前的问题而言,当然还不够。您提供的 Sql 将与问题无关(但您应该将 SqlCommand 放在 using 块中)。

标签: c# sql asp.net-core


【解决方案1】:

事实证明,来自 SQL 表的结果确实包含空格,因此我能够将 Trim() 添加到结果中以获得我要查找的内容。

感谢可选选项为我指明正确的方向!

【讨论】:

  • 很高兴你解决了它。正如预测的那样,提供的 SQL 与解决“无法将文件扩展名附加到字符串”的问题无关。
猜你喜欢
  • 2018-07-16
  • 2011-10-30
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 2014-03-06
  • 2014-11-18
  • 2021-03-15
  • 1970-01-01
相关资源
最近更新 更多