1

这很愚蠢,但我正在为 sqlcommand 声明一个 sql 字符串,我必须将它全部写在一行上

string strSQL = "SELECT [Year],[Week No],StartDate,EndDate,Dept,[Clock No],RTRIM(Name)+' '+   RTRIM(initial) as Name,[Own Hours],[Other Hours],[Total Hours],[OT Premium] FROM [Wages].[CHHours] Where [Year]=@WageYear and [Week No]=@Week Order by  [Year] DESC, [Week No] DESC,Dept,[Clock No]" ;

因为否则我会在不断的错误中得到换行符,但这看起来真的很难看而且难以阅读。在 vb 中,它会使用 &_ 继续,因为 c# 对空格不敏感(我想)我应该这样写

string strSQL = "SELECT [Year],[Week No],StartDate,EndDate,Dept,[Clock No],
              RTRIM(Name)+' '+ RTRIM(initial) as Name,[Own Hours],[Other Hours],
               [Total Hours],[OT Premium] 
               FROM [Wages].[CHHours] 
               Where [Year]=@WageYear and [Week No]=@Week 
               Order by  [Year] DESC, [Week No] DESC,Dept,[Clock No]" ;

还是我必须这样做

string strSQL = "SELECT [Year],[Week No],StartDate,EndDate,Dept,[Clock No],";

string strSQL = strSQL+" RTRIM(Name)+' '+ RTRIM(initial) as Name,[Own Hours]";

string strSQL = strSQL+",[Other Hours],[Total Hours],[OT Premium] ";

string strSQL = strSQL+"FROM [Wages].[CHHours] ";

string strSQL = strSQL+" Where [Year]=@WageYear and [Week No]=@Week ";

string strSQL = strSQL+"Order by  [Year] DESC, [Week No] DESC,Dept,[Clock No]" ;

这让我觉得很古老!

请伊恩最好的方法是什么

4

4 回答 4

6

使用 @ 符号,例如

string strSQL = @"SELECT [Year],[Week No],StartDate,EndDate,Dept,[Clock No],
         RTRIM(Name)+' '+ RTRIM(initial) as Name,[Own Hours],[Other Hours],
           [Total Hours],[OT Premium] 
           FROM [Wages].[CHHours] 
           Where [Year]=@WageYear and [Week No]=@Week 
           Order by  [Year] DESC, [Week No] DESC,Dept,[Clock No]" ;

那可能有用吗?

于 2013-10-25T12:23:09.597 回答
1

这个怎么样:

string strSQL = "SELECT [Year],[Week No],StartDate,EndDate,Dept,[Clock No]," 
                + " RTRIM(Name)+' '+ RTRIM(initial) as Name,[Own Hours]" 
                + ",[Other Hours],[Total Hours],[OT Premium] "
                + "FROM [Wages].[CHHours] "
                + " Where [Year]=@WageYear and [Week No]=@Week " 
                + "Order by [Year] DESC, [Week No] DESC,Dept,[Clock No]";
于 2013-10-25T12:24:45.007 回答
0
string strSQL = @"SELECT [Year],[Week No],StartDate,EndDate,Dept,[Clock No],
     RTRIM(Name)+' '+ RTRIM(initial) as Name,[Own Hours],[Other Hours],
       [Total Hours],[OT Premium] 
       FROM [Wages].[CHHours] 
       Where [Year]=@WageYear and [Week No]=@Week 
       Order by  [Year] DESC, [Week No] DESC,Dept,[Clock No]" ;

//Now replace the @... with your desired data
strSQL = strSQL.Replace("@WageYear",your year).Replace("@Week ",your week);

//Or in multiple lines
strSQL = strSQL.Replace("@WageYear",your year);
strSQL = strSQL.Replace("@Week ",your week);

希望这可以帮助

于 2013-10-25T12:52:02.870 回答
0

如果您将文本框中的信息添加到查询中,则可以使用两者的组合。像这样的东西

string strSQL = "SELECT [Year],[Week No],StartDate,EndDate,Dept,'" + textBox1.Text +  "'[Clock No],"
 +@" RTRIM(Name)+' '+ RTRIM(initial) as Name,[Own Hours]
 ,[Other Hours],[Total Hours],[OT Premium] 
 FROM [Wages].[CHHours] 
 Where [Year]=@WageYear and [Week No]=@Week  
 Order by [Year] DESC, [Week No] DESC,Dept,[Clock No]";
于 2013-12-13T16:16:30.700 回答