タイトル : Re^3: SQLからLabelに入れるやり方 投稿日 : 2006/02/01(Wed) 16:25 投稿者 : なおこ(・∀・)
お世話になります。 たとえば、ですが。 クラスのメンバ変数に以下の様に宣言しておいて =============================================== Private m_dt As DataTable = New DataTable =============================================== Form_Load 等で 以下の様なコードを書いて、 (※コネクションなどのオブジェクトの破棄については hhttp://jeanne.wankuma.com/tips/programing/dispose.html を参照) =============================================== Dim oleCon As OleDb.OleDbConnection = New OleDb.OleDbConnection("接続文字列") Dim da As OleDb.OleDbDataAdapter oleCon.Open() da = New OleDb.OleDbDataAdapter da.SelectCommand = New OleDb.OleDbCommand da.SelectCommand.Connection = oleCon da.SelectCommand.CommandText = "SELECT バイク名, 排気量 FROM 国外車" Me.m_dt.Clear() da.Fill(Me.m_dt) =============================================== Button1 を クリックした時に、 TextBox1 に入力されたバイク名の排気量を、Label1 に 表示するのは、こんな感じ。 =============================================== Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click Dim rows() As DataRow = Me.m_dt.Select("バイク名='" + Me.TextBox1.Text + " '") If rows.Length < 1 Then Me.Label1.Text = "" '該当するものがない Else Me.Label1.Text = DirectCast(rows(0)("排気量"), String) End If End Sub =============================================== |