none
SQL,怎么修改这个来着? RRS feed

  • 问题

  • /*现在我的数据库中的"FName"字段,基本上每个记录后面都有空格...怎么做简单一点?
     
    我在窗体的load时候,
    我发觉我这样做不行...好奇怪,*/
    T_BookTableAdapter bookAdapter = new T_BookTableAdapter();
                var datas = bookAdapter.GetData();
                datas.Select(); //从数据库选择.
                foreach(var data in datas) {
                    if(data.FName.EndsWith(string.Empty) || data.FName.StartsWith(string.Empty)) {
                        bookAdapter.UpdateNameById(data.FName.Trim(), data.FId);
                    }
                }
    
    /*
    UPDATE [dbo].[T_Book] SET 
    [FName] = @name
    WHERE [FId] = @id 
    */
    为SQL语句...
    应该是没有问题的哈...

    2013年6月23日 2:05

答案

  • Sorry,

    我再重申一次,你必须保证:

    1)不能使用char或者nchar,使用var开头的(varchar或者varnchar)。

    2)以下的话多余:

      datas.Select(); //从数据库选择.
    3)你那句if建议改为:

      foreach(var data in datas) {
                   
    if(data.FName.Length>data.FName.Trim().Length){
                        bookAdapter
    .UpdateNameById(data.FName.Trim(), data.FId);
                   
    }


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月24日 1:22
    版主

全部回复

  • FName必须是varchar(N),varnchar(N)这些类型,请确定不是char或者nchar,否则你字符串长度如果没有达到N,末尾自动补充空格的。

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月23日 2:11
    版主
  • 或者:

    UPDATE [dbo].[T_Book] SET 
    Trim([FName]) = @name
    WHERE [FId] = @id 

    你的@name不能有任何空格(开头和结尾)。


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月23日 2:13
    版主
  • 对了,你说对了...然后我用命令去修改类型..成功了...但是...为什么我在load的时候还是修改不了?
    2013年6月23日 6:59
  • Trim([FName]) = @name

    这个都有语法错误好不好...

    2013年6月23日 7:04
  • 对了,你说对了...然后我用命令去修改类型..成功了...但是...为什么我在load的时候还是修改不了?
    我建议你先停止使用程序,修改表结构,然后再使用。

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月23日 7:04
    版主
  • Trim([FName]) = @name

    这个都有语法错误好不好...

    对不起,看错了。

    请保证你查询where条件:

    select …… where Trim([FName]) = @name


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月23日 8:09
    版主
  • 晕...你都不懂我要做什么么?

    "select …… where Trim([FName]) = @name"有错误,而且,貌似你真的不懂我问的....

    select …… where Trim([FName]) = @name是错误的,你是想在搜索的时候忽略两头空格是吧...

    我现在是想把所有记录中,字段为 FName的值,后面有空格的全部去掉...你那个什么 

    但是不懂为什么我做的不行...

    T_BookTableAdapter bookAdapter = new T_BookTableAdapter();
                var datas = bookAdapter.GetData();
                datas.Select(); //从数据库选择.
                foreach(var data in datas) {
                    if(data.FName.EndsWith(string.Empty) || data.FName.StartsWith(string.Empty)) {
                        bookAdapter.UpdateNameById(data.FName.Trim(), data.FId);
                    }
                }
    
    

    UpdateNameById方法的SQL语句是:

    UPDATE [dbo].[T_Book] SET 
    [FName] = @name
    WHERE [FId] = @id

    我感觉我表达得很清楚了哈...


    2013年6月23日 8:53
  • Sorry,

    我再重申一次,你必须保证:

    1)不能使用char或者nchar,使用var开头的(varchar或者varnchar)。

    2)以下的话多余:

      datas.Select(); //从数据库选择.
    3)你那句if建议改为:

      foreach(var data in datas) {
                   
    if(data.FName.Length>data.FName.Trim().Length){
                        bookAdapter
    .UpdateNameById(data.FName.Trim(), data.FId);
                   
    }


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月24日 1:22
    版主