如何用c#代码删除数据库中的字符? [英] How to remove characters in database with c# code behind?

查看:56
本文介绍了如何用c#代码删除数据库中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个数据库表,它有课程代码,有些人插错了课程代码



相同的课程有不同的代码MATH103 MATH-103 MATH 103 MATH_103这些是相同的课程



我将从数据库到网格视图的课程就像一课(我将使用distinct)但我不会从数据库中删除此控件必须是c#代码隐藏< br $> b $ b

如何删除这些字符



我不知道这些课程代码有哪些字符? - / _等

there is a database table it has lesson codes some people insert wrong lessoncodes

same lessons has different alots of codes MATH103 MATH-103 MATH 103 MATH_103 These are same lesson

I will get this lessons from database to gridview like one lesson(I will use distinct) but I will not remove from database this control must be c# code behind

how to remove these characters

I dont know these lesson codes has which characters ? - / _ " " etc.

推荐答案

如果你使用LINQ,这很容易。



如果你已经加载了一个集合中的所有记录,你只需在它上面运行Distinct。

例如的IEnumerable<串GT; code = codesDistinct();< / string>



这里 [ ^ ]就是一个例子。

另一个 example [ ^ ]。
Quite easy to do if you use LINQ.

If you have loaded all the records in a collection, you can simply run Distinct on it.
For e.g. IEnumerable<string> code = codesDistinct();</string>

Here[^] is an example.
Another example[^].


以下使用Regex构建各种格式的课程代码。我还扩展了解决方案,以满足其他课程,例如HIST for History,ENGL for English,也适合非大写条目。

我用了一个字符串数组来检查

The following uses Regex to construct the lesson code from various formats. I''ve also expanded the solution to cater for other lessons e.g. HIST for History, ENGL for English and also to cater for non-upper case entries.
I''ve used a string array for my checking
private void button2_Click(object sender, EventArgs e)
{
    // various possible entries on the database...
    string[] lessons = {"....", "hist=101", "MATH103","MATH-103", "MATH 103", "MATH_103", "MATH--101", "engl   100"};

    foreach (string singleLesson in lessons)
    {
        // m will contain the lesson number 103, 101, 100 etc
        Match m = Regex.Match(singleLesson, @"\d+", RegexOptions.IgnoreCase);
        // n will contain the lesson type MATH, ENGL, HIST etc etc
        Match n = Regex.Match(singleLesson, @"\b[A-Z|a-z]*", RegexOptions.IgnoreCase);

        // if we have both a lesson type and a lesson number ...
        if (m.Success && n.Success )
        {
            string lesson = n.ToString().ToUpper() + m.ToString();
            Debug.Print(lesson);
        }
        else
            Debug.Print("Invalid Entry");


}





输出:



Output:

Invalid Entry<br />
HIST101<br />
MATH103<br />
MATH103<br />
MATH103<br />
MATH103<br />
MATH101<br />
ENGL100<br />


这篇关于如何用c#代码删除数据库中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆