C#_如何在每个列表SQL中插入一个字符串

时间:2017-04-21 06:59:14

标签: c# sql database string

如何在每个列表SQL中插入一个字符串 例如,我有一个字符串:

  

/Hanoi/a2.3b6.7c8.4/Tphcm/n7.2a5.2

我尝试了很多方法,但是我得到了这个结果:

enter image description here

我不知道无论如何将它插入表中,我希望它们看起来像:

enter image description here

你能为我推荐任何理想吗?

2 个答案:

答案 0 :(得分:2)

如果我需要开发它,这是我的方法。

class Program
{

    static void Main(string[] args)
    {
        List<Location> locations = new List<Location>();

        string foo = "/Hanoi/a2.3b6.7c8.4/Tphcm/n7.2a5.2";

        var bo = foo.Remove(0, 1).Split('/'); // split data by '/'

        // even bo elenment is address and odd element has specific data like number and letter
        for (int i = 0; i < bo.Length; i = i + 2)
        {
            var str = bo[i]; // Address
            var str1 = bo[i + 1]; // Letter and Number

            var arrLetters = str1.Where(c => char.IsLetter(c)).ToArray(); // Get Letters

            for (int j = 0; j < arrLetters.Length; j++)
            {
                string splittedLetter = string.Empty; 
                string number = string.Empty;

                if (j+1 != arrLetters.Length)
                {
                    splittedLetter = str1.Split(arrLetters[j + 1])[0]; 
                    number = Regex.Replace(splittedLetter, "[A-Za-z ]", "");
                    str1 = str1.Replace(splittedLetter, string.Empty);
                }
                else
                {
                    number = Regex.Replace(str1, "[A-Za-z ]", "");
                }

                // add to list, db or where you want :)
                locations.Add(new Location
                {
                    Address = str,
                    Letter = arrLetters[j].ToString(),
                    Number = number
                });
            }
        }
        Console.ReadKey();
    }
}

class Location
{
    public string Address { get; set; }

    public string Letter { get; set; }

    public string Number { get; set; }
}

列出成员数据,例如db结果。 enter image description here

我希望此解决方案对您有所帮助

答案 1 :(得分:1)

  1. 通过参数&#34; /&#34;

    从/Hanoi/a2.3b6.7c8.4/Tphcm/n7.2a5.2提到的输入字符串中提取所需信息

    一个。将完整的字符串拆分为数组
     string str =&#34; /Hanoi/a2.3b6.7c8.4/Tphcm/n7.2a5.2" ;;  string [] words = str.Split(&#39; /&#39;);

    湾从单词array

  2. 中提取所需的数据
  3. 编写一个sql查询/ LINQ,将每个字符串/数字插入相关列

  4. 如果您发现实施此问题,请告诉我们。