VB.Net换行符失效导致SQL查询执行无结果问题求助
我来帮你搞定这两个问题——VB.Net里换行符失效的显示问题,以及由此牵连的SQL查询执行无结果的问题。
一、解决VB.Net换行符失效的显示问题
你提到用vbCrLf、Environment.NewLine甚至vbCr都没达到预期换行效果,先搞清楚一个关键点:换行符本身是存在于字符串中的,大概率是你输出/显示字符串的场景不识别这些换行符。
举几个常见场景的解决方案:
- 如果是在
MessageBox里显示:MessageBox.Show(sTest)会正确识别vbCrLf换行; - 如果是在
TextBox控件里:必须把控件的Multiline属性设为True,否则即使字符串里有换行符,也会被挤成一行; - 如果是在网页前端显示:HTML不识别
vbCrLf这种换行符,需要把它替换成<br>标签,比如sTest.Replace(vbCrLf, "<br>"); - 如果是在控制台输出:
Console.WriteLine(sTest)会自动解析换行符,或者用Debug.WriteLine(sTest)看输出窗口的内容,确认换行符是否真的存在。
你可以先验证字符串里是否真的包含换行符:
Dim hasNewLine As Boolean = sTest.Contains(vbCrLf) Debug.WriteLine("是否包含换行符:" & hasNewLine)
二、解决SQL查询执行无结果的问题
你说把计算年龄的SQL存到VB变量里执行没结果,但直接在查询窗口跑正常,推测是换行符的问题,但其实SQL Server本身会忽略SQL语句里的换行和空格,所以核心问题可能是VB里拼接SQL语句时的语法错误,或者换行符导致的语句断裂。
给你两种正确的VB里定义多行SQL的方式:
方式1:用vbCrLf拼接(兼容所有VB.Net版本)
Dim sqlQuery As String = "declare @now date,@dob date, @now_i int,@dob_i int, @days_in_birth_month int" & vbCrLf & _ "declare @years int, @months int, @days int" & vbCrLf & _ "set @now = '2013-02-28'" & vbCrLf & _ "set @dob = '2012-02-29' -- Date of Birth" & vbCrLf & _ "set @now_i = convert(varchar(8),@now,112) -- iso formatted: 20130228" & vbCrLf & _ "set @dob_i = convert(varchar(8),@dob,112) -- iso formatted: 20120229" & vbCrLf & _ "set @years = ( @now_i - @dob_i)/10000 -- (20130228 - 20120229)/10000 = 0 years" & vbCrLf & _ "set @months =(1200 + (month(@now)- month(@dob))*100 + day(@now) - day(@dob))/100 %12 -- (1200 + 0228 - 0229)/100 % 12 = 11 months" & vbCrLf & _ "set @days_in_birth_month = day(dateadd(d,-1,left(convert(varchar(8),dateadd(m,1,@dob),112),6)+'01'))" & vbCrLf & _ "set @days = (sign(day(@now) - day(@dob))+1)/2 * (day(@now) - day(@dob)) + (sign(day(@dob) - day(@now))+1)/2 * (@days_in_birth_month - day(@dob) + day(@now)) -- ( (-1+1)/2*(28 - 29) + (1+1)/2*(29 - 29 + 28))" & vbCrLf & _ "-- Explain: if the days of now is bigger than the days of birth, then diff the two days" & vbCrLf & _ "-- else add the days of now and the distance from the date of birth to the end of the birth month" & vbCrLf & _ "select @years,@months,@days -- 0, 11, 28"
方式2:用VB.Net多行字符串(VB 14及以上版本支持,更简洁)
直接用三重引号"""包裹多行内容,不需要手动拼接换行符:
Dim sqlQuery As String = """declare @now date,@dob date, @now_i int,@dob_i int, @days_in_birth_month int declare @years int, @months int, @days int set @now = '2013-02-28' set @dob = '2012-02-29' -- Date of Birth set @now_i = convert(varchar(8),@now,112) -- iso formatted: 20130228 set @dob_i = convert(varchar(8),@dob,112) -- iso formatted: 20120229 set @years = ( @now_i - @dob_i)/10000 -- (20130228 - 20120229)/10000 = 0 years set @months =(1200 + (month(@now)- month(@dob))*100 + day(@now) - day(@dob))/100 %12 -- (1200 + 0228 - 0229)/100 % 12 = 11 months set @days_in_birth_month = day(dateadd(d,-1,left(convert(varchar(8),dateadd(m,1,@dob),112),6)+'01')) set @days = (sign(day(@now) - day(@dob))+1)/2 * (day(@now) - day(@dob)) + (sign(day(@dob) - day(@now))+1)/2 * (@days_in_birth_month - day(@dob) + day(@now)) -- ( (-1+1)/2*(28 - 29) + (1+1)/2*(29 - 29 + 28)) -- Explain: if the days of now is bigger than the days of birth, then diff the two days -- else add the days of now and the distance from the date of birth to the end of the birth month select @years,@months,@days -- 0, 11, 28"""
另外,执行SQL的时候,建议用标准的SqlCommand流程,确保连接和命令正确释放:
Using conn As New SqlConnection("你的数据库连接字符串") conn.Open() Using cmd As New SqlCommand(sqlQuery, conn) Dim reader As SqlDataReader = cmd.ExecuteReader() If reader.Read() Then Dim years As Integer = reader.GetInt32(0) Dim months As Integer = reader.GetInt32(1) Dim days As Integer = reader.GetInt32(2) Debug.WriteLine($"年龄:{years}年{months}月{days}天") End If End Using End Using
如果还是不确定问题出在哪,可以把VB里生成的SQL字符串打印出来(比如用Debug.WriteLine(sqlQuery)),然后复制到SQL查询窗口里执行——如果复制过去能正常执行,那就是VB执行代码的问题;如果复制过去也报错,那就是字符串拼接时的语法错误。
内容的提问来源于stack exchange,提问作者Sixthsense
相关产品推荐
相关产品推荐

