SQL命令字符串清理

时间:2016-11-23 01:41:47

标签: sql-server vb.net string

我在vb.net中将一个查询声明为变量。我希望将其格式化,以便将来开发/更好地理解,因此声明级别的清理将无效。

public class Carnivore extends Animal
{
   public Food eat(Food x) throws Exception
    {
        if (x.equals("Plants")) {
                throw new Exception("Carnivores only eat meat!");
            } else {
                return x;
            }

    }
    public void makeNoise()  
    {
        noise = null;
    }
    public String getNoise()  
    {
        return noise;
    }   
}

我保持登录从app发送到数据库的所有SQL命令的专用表,一旦作为日志表中的字符串值传递给SQL Server,此查询就会变得混乱:

abstract public class Animal {

String name;
int age;  
String noise;

abstract public void makeNoise();

public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

abstract public Food eat(Food x);

}

所以我正在寻找摆脱所有不必要角色的方法。是否有任何有效而简单的方法来清理它以获得干净的SQL语句?不需要格式化,例如FROM等单独的行。

2 个答案:

答案 0 :(得分:0)

Dim query As String ="SELECT * FROM (" & vbcrlf &
        "    SELECT TOP 10 T.name TableName, i.Rows NumberOfRows" & vbcrlf &
        "    FROM  sys.tables T" & vbcrlf &
        "    JOIN sys.sysindexes I ON T.OBJECT_ID = I.ID" & vbcrlf &
        "    WHERE indid IN (0,1)" & vbcrlf &
        "    ORDER BY i.Rows DESC, T.name ASC" & vbcrlf &
        " ) AS TableA" & vbcrlf &
        " ORDER BY TableName ASC"

答案 1 :(得分:0)

我终于用正则表达式解决了这个问题

Query = Regex.Replace(Query, "\s{2,}", " ").Trim