我正在使用带有SQL Server 2012数据库的vb.net。
我想检查数据库中文件列表的状态。查询很简单,就像这样。
DECLARE @Filename NVARCHAR(100)
SET @Filename = 'MyFileName.doc'
SELECT DISTINCT
Docs.Filename,
Status.Name as 'Status'
FROM
[Documents] AS Docs
INNER JOIN
[Status] AS Status ON Status.StatusID = Docs.CurrentStatusID
WHERE
Docs.Filename LIKE @Filename
它适用于一个文件名,我可以使用sql连接在vb.net中轻松启动它,如本例所示。
Dim conn As New SqlConnection
If conn.State = ConnectionState.Closed Then
conn.ConnectionString = PDMConnectionString
End If
Try
conn.Open()
Dim sqlquery As String =
"DECLARE @Filename NVARCHAR(100)
SELECT DISTINCT
Docs.Filename,
Status.Name as 'Status'
FROM [Documents] AS Docs
INNER JOIN [Status] AS Status
ON Status.StatusID = Docs.CurrentStatusID
WHERE Docs.Filename LIKE @Filename "
Dim data As SqlDataReader
Dim adapter As New SqlDataAdapter
Dim parameter As New SqlParameter
Dim command As SqlCommand = New SqlCommand(sqlquery, conn)
With command.Parameters
.Add(New SqlParameter("@filename", "MyFileName.doc"))
End With
command.Connection = conn
adapter.SelectCommand = command
data = command.ExecuteReader()
While data.Read
'do something'
End While
Catch ex As Exception
End Try
问题是我需要找到很多文件的状态,我只想用一个查询来做。
我可以直接在查询中通过更改最后一行来这样做,删除vb.net中的参数并直接发送查询:
WHERE
Docs.Filename IN ('MyFileName.doc', 'MyOtherFileName.doc')
但它意味着很多字符串连接,我不太喜欢该解决方案的代码。
在这种情况下使用的最佳做法是什么,以便使用较少的字符串连接并使代码更易于管理?
答案 0 :(得分:0)
您可以使用函数来获取逗号分隔的字符串并返回表格...
let uniqueProcessPool = WKProcessPool()
let configA = WKWebViewConfiguration()
configA.processPool = uniqueProcessPool
let webViewA = init(frame: CGRect.zero, configuration: configA)
let configB = WKWebViewConfiguration()
configB.processPool = uniqueProcessPool
let webViewB = init(frame: CGRect.zero, configuration: configB)
然后在你的SQL中加入它......
CREATE FUNCTION [dbo].[FileNames]( @FilenameValues nvarchar(max) )
RETURNS @Result TABLE( FileName nvarchar(max) )
AS
BEGIN
-- convert to an xml string
DECLARE @xml XML
SELECT @xml = CAST( '<A>' + REPLACE( @FilenameValues, ',', '</A><A>' ) + '</A>' AS XML )
-- select rows out of the xml string
INSERT INTO @Result
SELECT DISTINCT LTRIM( RTRIM( t.value( '.', 'nvarchar(max)' ) ) ) AS [FileName]
FROM @xml.nodes( '/A ') AS x(t)
RETURN
END
或在WHERE中使用......
JOIN (
SELECT * FROM dbo.FileNames( 'MyFileName.doc, MyOtherFileName.doc' )
) FileNames ON FileNames.FileName = Docs.Filename