如何将SQL语句存储到变量中

时间:2015-06-16 02:14:59

标签: sql excel excel-vba vba

我目前正面临一个问题。我有一个名为" DESC1"在一个名为" Master"的表中。我试图根据这个方面的内容来检索价值......

I'm called.
// <note: 5-second wait is here>
End.
java.util.concurrent.CompletableFuture@<...>[Completed normally]
java.util.concurrent.CompletableFuture@<...>[Completed normally]
java.util.concurrent.CompletableFuture@<...>[Completed normally]

我试图在correctString1和correctString旁边的excel上显示

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

希望你能帮助我。

1 个答案:

答案 0 :(得分:1)

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

Read more

<强> ==更新==

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

See more