2016-07-27 1 views
0

ここでの最初の質問と事前の警告私は基本的なことを理解しようとしています。SQL Serverのコンボボックス項目を単一の(および可変の)列を使用して追加する

基本的には、SQLサーバーから指定されたリスト内のすべての項目を選択して配列に追加し、その配列でコンボボックスを埋めています。次のように

SQLサーバーの構造は次のとおりです。 Just a representation

ユーザーは、彼らがログインしたとき、彼らが構築されている製品を選択し、その後、私はに関連する部品の全てを充填するコンボボックスを希望しますその項目。

現在のサンプルコードは、

Public Class frmProduct 
    Private Sub frmProduct_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Dim mycon As SqlConnection = New SqlConnection('connection string') 
     mycon.Open() 
     Using mycon 
      Dim prodlist As String = frmMain.cmbProduct.SelectedItem 
      Dim cmds As String = "SELECT DISTINCT(Product) FROM [Products] where Product = @prodlist" 
      Dim cmde As New SqlCommand(sqlt, mycon) 
      cmde.Parameters.AddWithValue("@Product", prodlist) 
      Dim dr As SqlDataReader = scmd.ExecuteReader 
      If dr.HasRows() Then 
       cmbFailure.Items.Add(dr.GetString(0)) 
      End If 
      scmd.ExecuteNonQuery() 
     End Using 

    End Sub 
End Class 

ユーザログインフォームに商品を選択し、選択された製品は、パーツリスト形式に持ち越されます。

私が現在受け取っているエラーは、「無効な列名製品」である可能性が高いです。選択した製品が持ち越されていない問題があると思ったが、テキストを「prodlist」に変更しているラベルを追加し、ラベルテキストを正確に変更した。

また、私はこのすべてで新鮮な肉ですので、明白な破損についてお詫び申し上げます。ご協力ありがとうございました!

+0

「製品」テーブルに「Product」という名前の列がないことを示すエラーメッセージが表示されます。 – jmcilhinney

+0

また、その列が存在していても、SQLコードは意味を持ちません。あなたの言うことは、 "Product値が何らかの入力と等しいProductsテーブルからすべてのProduct値を取得する"です。つまり、入力結果と同じ結果しか得られません。あなたは製品と部品について話しますので、ProductId列を主キーとして、ProductId列を外部キーとして持つPartsテーブルを持つProductsテーブルを用意する必要があります。次に、productIdに基づいて、そのPartsテーブルからPartNameまたはDescriptionなどの値をすべて取得します。 – jmcilhinney

答えて

0

「Product1、Product2、Product3」という画像が固定されていますか? ある場合は、select文でこれらの列を使用する必要があります。これは悪いデザインですが、うまくいくでしょう。 私はそれが勉強練習だと思うので、後で2つのテーブル、1つは製品のためのもの、もう1つは製品の貯蔵部のためのプロジェクトを強化することができます。

0

コードについては、こちらをご覧ください。

Private Sub frmProduct_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Dim mycon As SqlConnection = New SqlConnection('connection string') 
    Dim cmds As String = "SELECT DISTINCT(Product) FROM [Products] where Product = @prodlist" 

    Try 
     ' Most of the DB will having a connection fail, so programmer must set a try catch for it ' 
     mycon.Open() 
     Using mycon 
      Dim prodlist As String = [Form].[ComBoBox].SelectedItem 
      Dim cmde As New SqlCommand(sqlt, mycon) 
      cmde.Parameters.AddWithValue("@Product", prodlist) 
      Dim dr As SqlDataReader = scmd.ExecuteReader 
      If dr.HasRows() Then 
       cmbFailure.Items.Add(dr.GetString(0)) 
      End If 
      scmd.ExecuteNonQuery() 
      ' For more save for this sql will execute successfully, some of programmer will use Transaction with commit ' 
      ' Microsoft Link: https://msdn.microsoft.com/en-us/library/5ha4240h(v=vs.110).aspx ' 
     End Using 
     ' mycon.Close() ' 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) ' Exception Message ' 
     ' [Optional]Release object ' 
     ' mycon.Close() ' 
    End Try 
End Sub 

また、scmdがcmdeと異なるため、結果がクラッシュする可能性があります。
[scmd]がスタンドであることを説明してください。

関連する問題