2017-12-18 100 views
6

मैं एक resultset कि जो तत्व मैं खोजने के लिए अनुसार गतिशील है प्राप्त करना चाहते हैं एसक्यूएल से गतिशील धुरी पाने के लिए।कैसे वीबी को

declare @til DateTime = dateadd(MINUTE, -0, getdate()) 
declare @fra datetime = DATEADD(MINUTE, -350, @til) 

declare @title nvarchar(max) = 'test title' 
DECLARE @cols AS NVARCHAR(MAX), 
@query AS NVARCHAR(MAX); 

create table errors (collection_id bigint, nr smallint, position smallint, stamp datetime) 
create table t (collection_id bigint, collection_name nvarchar(max), nr smallint, [status] smallint, stamp datetime) 

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(errors.position) 
         from t t 
         left join errors on errors.collection_id = t.collection_id and errors.nr = t.nr 
         where t.Status = 4 and errors.Stamp > @fra and t.collection_name = ''' + @title + ''' and errors.collection_id is not null 
         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 1, ''); 

select @cols 

set @query = 'declare @til DateTime = dateadd(MINUTE, -0, getdate()) 
      declare @fra datetime = DATEADD(MINUTE, -350, @til) 

      ;with cte as (select t.collection_name, errors.position, count(errors.Tryksag_Nedtagsfejl_Id) antal 
      from t 
      left join errors on errors.collection_id = s.collection_id and errors.nr = t.nr 
      where t.Status = 4 and errors.Stamp > @fra and and t.collection_name = ''' + @title + ''' 
      group by t.collection_name, errors.position) 

       SELECT collection_name, ' + @cols + ' from 
       cte 
      pivot 
      (
       sum(antal) 
       for position in (' + @cols + ') 
      ) p ' 

execute(@query) 

अब तक मैं एक प्रश्न मैं पर SSMS चला सकते हैं बना दिया है और उत्पादन मैं इच्छा देता है: यहाँ अपनी क्वेरी का एक नमूना है। जो इस तरह होगा:

enter image description here

कैसे मैं vb.net में इस resultset मेरे पास उपलब्ध कर सकते हैं? जब मैं सिर्फ यह सब एक प्रश्न के रूप में चलाने यह मेरे परिणाम दे नहीं करता है (यह मानते हुए कि resultset पर अमल से नहीं देखा है)

जोड़ा गया वीबी कोड

Dim var_til As Short = 0 
      Dim var_fra As Short = -60 
      Dim Linie As String = "Red" 
      Dim tx = "Test title" 
      Dim Stt2 = "declare @til DateTime = dateadd(MINUTE, " & var_til & ", getdate()) " _ 
       & "declare @fra datetime = DATEADD(MINUTE, " & var_fra & " , @til) " _ 
       & "DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) " _ 
       & "declare @linie as nvarchar(max) = '" & Linie & "' " _ 
       & "declare @title as nvarchar(max) = '" & tx & "' " _ 
       & "select @cols = STUFF((SELECT distinct ',' + QUOTENAME(errors.position) " _ 
       & "from t " _ 
       & "left join errors on errors.collection_id = t.collection_id And errors.nr = t.nr " _ 
       & "where t.Status = 4 And errors.Stamp > @fra And t.collection_name = ''' + @title + ''' and errors.collection_id is not null " _ 
       & "FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 1, '') " _ 
       & " " _ 
       & "set @query = ';with cte as (select t.collection_name, errors.position, count(errors.Tryksag_Nedtagsfejl_Id) antal " _ 
       & "from t " _ 
       & "left join errors on errors.collection_id = s.collection_id And errors.nr = t.nr " _ 
       & "where t.Status = 4 And errors.Stamp > @fra And And t.collection_name = ''' + @title + ''' " _ 
       & "group by t.collection_name, errors.position) " _ 
       & "SELECT collection_name, ' + @cols + ' from " _ 
       & "cte " _ 
       & "pivot " _ 
       & "(" _ 
       & "sum(antal) " _ 
       & "for position in (' + @cols + ') " _ 
       & ") p ' " _ 
       & "execute(@query) " 

      Dim sqlConnection2 As New SqlConnection("Data Source=CONDOR-TI;Initial Catalog=Condor_db;Integrated Security=True") 
      Dim cmd2 As New SqlCommand 
      Dim reader2 As SqlDataReader 
      cmd2.CommandText = Stt2 
      cmd2.CommandType = CommandType.Text 
      cmd2.Connection = sqlConnection2 
      sqlConnection2.Open() 
      reader2 = cmd2.ExecuteReader() 
      While reader2.Read 
       Console.Write(reader2(0)) 
      End While 
      Console.WriteLine() 
      sqlConnection2.Close() 
      reader2.Close() 
+2

क्या की तरह अपने VB कोड दिखता है? – SMM

+0

[mcve] कृपया: टेबल और सामान ... – Blag

+0

आप अपने VB कोड में इस क्वेरी चला सकता हूँ? – FLICKER

उत्तर

4

मुझे लगता है कि यह एक संग्रहीत बनाने के लिए बेहतर है प्रक्रिया इस एसक्यूएल आदेश पर अमल, और निम्नलिखित कोड का उपयोग कर VB.Net से निष्पादित करने के लिए:

Dim sqlConnection1 As New SqlConnection("Your Connection String") 
Dim cmd As New SqlCommand 
Dim reader As SqlDataReader 

cmd.CommandText = "StoredProcedureName" 
cmd.CommandType = CommandType.StoredProcedure 
cmd.Connection = sqlConnection1 

sqlConnection1.Open() 

reader = cmd.ExecuteReader() 
' Data is accessible through the DataReader object here. 

sqlConnection1.Close() 

संदर्भ

3

आप दो चरणों में चला सकते हैं। सुनिश्चित करें कि आप उन दोनों आदेशों को एक ही कनेक्शन पर चलाएं।

पहले आदेश में CTE ExecuteNonQuery का उपयोग कर एक #temp तालिका में चयन किया है।

दूसरा आदेश कॉल ExecuteReader() में

Dim var_til As Short = 0 
Dim var_fra As Short = -60 
Dim Linie As String = "Red" 
Dim tx = "Test title" 
Dim Stt2 = "declare @til DateTime = dateadd(MINUTE, " & var_til & ", getdate()) " _ 
    & "declare @fra datetime = DATEADD(MINUTE, " & var_fra & " , @til) " _ 
    & "DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) " _ 
    & "declare @linie as nvarchar(max) = '" & Linie & "' " _ 
    & "declare @title as nvarchar(max) = '" & tx & "' " _ 
    & "select @cols = STUFF((SELECT distinct ',' + QUOTENAME(errors.position) " _ 
    & "from t " _ 
    & "left join errors on errors.collection_id = t.collection_id And errors.nr = t.nr " _ 
    & "where t.Status = 4 And errors.Stamp > @fra And t.collection_name = ''' + @title + ''' and errors.collection_id is not null " _ 
    & "FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') , 1, 1, '') " _ 
    & " " _ 
    & "set @query = ';with cte as (select t.collection_name, errors.position, count(errors.Tryksag_Nedtagsfejl_Id) antal " _ 
    & "from t " _ 
    & "left join errors on errors.collection_id = s.collection_id And errors.nr = t.nr " _ 
    & "where t.Status = 4 And errors.Stamp > @fra And And t.collection_name = ''' + @title + ''' " _ 
    & "group by t.collection_name, errors.position) " _ 
    & "SELECT collection_name, ' + @cols + ' INTO #OUT from " _ 
    & "cte " _ 
    & "pivot " _ 
    & "(" _ 
    & "sum(antal) " _ 
    & "for position in (' + @cols + ') " _ 
    & ") p ' " _ 
    & "execute(@query) " 

dim stt3 as string = "select * from #OUT" 

Dim sqlConnection2 As New SqlConnection("Data Source=CONDOR-TI;Initial Catalog=Condor_db;Integrated Security=True") 
Dim cmd2 As New SqlCommand 
cmd2.CommandText = Stt2 
cmd2.CommandType = CommandType.Text 
cmd2.Connection = sqlConnection2 

Dim cmd3 As New SqlCommand 
Dim reader3 As SqlDataReader 
cmd3.CommandText = Stt3 
cmd3.CommandType = CommandType.Text 
cmd3.Connection = sqlConnection2 

sqlConnection2.Open() 
cmd2.ExecuteNonQuery() 
reader3 = cmd3.ExecuteReader() 
While reader3.Read 
    Console.Write(reader3(0)) 
End While 
Console.WriteLine() 
sqlConnection2.Close() 
reader3.Close() 
+0

यह एक अच्छा विचार आवेदन कोड में एसक्यूएल कमांड पोस्ट करने के लिए नहीं है, मुझे लगता है कि यह संग्रहित प्रक्रियाओं या तालिका महत्वपूर्ण कार्यों – Yahfoufi

+0

मैं सहमत हूँ उपयोग करने के लिए बेहतर है। विशेष रूप से एक लाइन उन्मुख भाषा में वीबी की तरह। कम से कम सी # में यह सब एसक्यूएल स्ट्रिंग के एक ब्लॉक में हो सकता है। मैं इस तरह एसक्यूएल एम्बेडिंग से बचने के लिए। मुझे लगता है कि इस दृष्टिकोण के लिए अन्य कारण होना चाहिए। – suresubs