0

今日ここでいくつかの質問に目を通しましたが、ぐるぐる回っていると思います。

username私の Web フォームには、ドロップダウン リスト (SQL ステートメントによって入力される)を含む多くの要素があります。

フォームの送信時に、コード ビハインドaspx.vbファイルでselect top 1クエリを実行し、4 列の 1 行のデータを返します。

返された SQL クエリ結果の 4 列は、後で aspx.vb ファイルでのみ使用されるため、各列を変数に割り当てたいと考えています。私はこのタスクに苦労しており、変数をクエリの列結果に割り当てています。

Protected Sub submitbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitbtn.Click


    Dim connString1 As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ToString
    Dim conn As New SqlConnection(connString1)
    Dim sql1 As String = ""

    Dim col1h As String = ""
    Dim col2r As String = ""
    Dim col3title As String = ""
    Dim col4UQN As String = ""

    sql1 = "SELECT TOP 1 col1h,col2r,col3title, col4UNQ  from tblDistinctUserOHR where colID_Username= '" + username + "' "

    Dim cmd1 As New SqlCommand(sql1, conn)
'open the connection
'run the sql
'the result will always be found but in case its not some form of safety catch (the variables stay as empty strings 
'assign the results to the variables
'close connections
'lots of other code 
    End Sub

誰かが私を正しい方向に向けて、SQLを実行し、結果を変数に割り当てることができますか. 私はExecuteScalar()andについて読んできましSqlDataReaderたが、私が見つけた唯一の例は単一の結果または単一の列を持つ多くの行を処理するため、正しいオプションではないようです

サンプルとポインタをありがとう。

4

1 に答える 1