簡體   English   中英

如何將SQL語句存儲到變量中

[英]How do I store a SQL statement into a variable

我目前在這里面臨一個問題。 我在名為“ Master”的表中有一個名為“ DESC1”的列。 我正在嘗試根據與此類似的東西來檢索值...

"Select DESC1 FROM Master WHERE '" & TextBox1.Text & "' "

我試圖在excel上顯示正確字符串1和正確字符串旁邊

 oWB.Sheets(1).Range("A2", "K2").Insert(Shift:=Excel.XlDirection.xlDown)
 oWB.Sheets(1).Range("E2").Value = " " & correctString1 & " : " &  correctString & " "

希望你能幫助我。

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

cmd.CommandText = "Select DESC1 FROM Master WHERE '" & TextBox1.Text & "' "
cmd.CommandType = CommandType.Text
cmd.Connection = sqlConnection1

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

If reader.HasRows Then 
        Do While reader.Read()
            Console.WriteLine(reader.GetInt32(0) _
              & vbTab & reader.GetString(1))
        Loop 
    Else
        Console.WriteLine("No rows found.")
    End If   
sqlConnection1.Close()

考慮第一NUMBER類型,第二VARCHAR

閱讀更多

== ==更新

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

查看更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM