 |
|
สอบถามเกี่ยวกับการสร้าง Store procedure ในการเลือกข้อมูลจาก Checkedlistbox |
|
 |
|
|
 |
 |
|

จากรูปด้านบน เมื่อเราเลือก LocationID 202 และ 203 แล้วให้ข้อมูลมาโชว์ที่ Datagrid เราต้องเขียน Code อย่างไรครับ ใน Store Procedure Code ใน vb.net เป็นแบบด้านล่างครับ รบกวนช่วยแนะนำหน่อยครับ
Code (VB.NET)
rivate Sub TESTLoadStoredProcedure() '(ByVal strLine As String, ByVal strSection As String, ByVal strSite As String)
Dim ds As New DataSet
Dim da As New SqlDataAdapter
Dim comm As New SqlCommand
Dim dsrv As New imsDataServices
' Dim output As Integer
Dim paramResult As New SqlParameter
Try
With comm
.Connection = dsrv.Connection
.CommandType = CommandType.StoredProcedure
' .CommandText = "sp_MAC_imsSP_StockValueByItemAsOfTransDate"
.CommandText = " sp_MAC_TESTLoadDataListbox"
'.Parameters.AddWithValue("@AsOfDate", dtAsOfDate.Value)--
' .Parameters.AddWithValue("@ItemTypeCode", cboItemType.Text)
'.Parameters.AddWithValue("@ItemID", cboItemID.Text)
'.Parameters.AddWithValue("@ProdFam", cboProFam.Text)--
' .Parameters.AddWithValue("@Classification", cboCate.Text)
' .Parameters.AddWithValue("@LeadTimeCode", cboPurchase.Text)
' .Parameters.AddWithValue("@ItemStatusCode", cboStatus.Text)
'.Parameters.AddWithValue("@ItemStatusCode", cboLocationID.Text)
.Parameters.AddWithValue("@ItemStatusCode", chkLocation.Text)
End With
'max = comm.ExecuteScalar()
da.SelectCommand = comm
da.Fill(ds, "Show")
grdTEST.DataSource = ds.Tables("Show")
'grdCost.RowsDefaultCellStyle.BackColor = Drawing.Color.White
'grdCost.AlternatingRowsDefaultCellStyle.BackColor = Drawing.Color.PowderBlue
Catch exsql As SqlException
MsgBox(exsql.Message, MsgBoxStyle.Exclamation)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
End Sub
Tag : .NET, Ms SQL Server 2005, VB.NET
|
|
 |
 |
 |
 |
Date :
2013-06-11 13:37:13 |
By :
s_oil_p |
View :
825 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Code (VB.NET)
Private Sub TESTLoadStoredProcedure() '(ByVal strLine As String, ByVal strSection As String, ByVal strSite As String)
Dim ds As New DataSet
Dim da As New SqlDataAdapter
Dim comm As New SqlCommand
Dim dsrv As New imsDataServices
' Dim output As Integer
Dim paramResult As New SqlParameter
Dim a As String
Dim b As String
Dim c As String
Dim tempLocation As String
Try
With comm
.Connection = dsrv.Connection
.CommandType = CommandType.StoredProcedure
' .CommandText = "sp_MAC_imsSP_StockValueByItemAsOfTransDate"
.CommandText = "sp_MAC_TESTLoadDataListbox"
'.Parameters.AddWithValue("@AsOfDate", dtAsOfDate.Value)--
' .Parameters.AddWithValue("@ItemTypeCode", cboItemType.Text)
'.Parameters.AddWithValue("@ItemID", cboItemID.Text)
'.Parameters.AddWithValue("@ProdFam", cboProFam.Text)--
' .Parameters.AddWithValue("@Classification", cboCate.Text)
' .Parameters.AddWithValue("@LeadTimeCode", cboPurchase.Text)
' .Parameters.AddWithValue("@ItemStatusCode", cboStatus.Text)
'.Parameters.AddWithValue("@ItemStatusCode", cboLocationID.Text)
Dim i As Integer
'chkLocation.SetItemChecked(i, True)
If chkLocation.CheckedItems.Count = 0 Then
Exit Sub
Else
tempLocation = chkLocation.SelectedItem(0).ToString
End If
' For i = 0 To chkLocation.Items.Count - 1
'chkLocation.Text = i + 1
' Next
' For Each itm As String In chkLocation.CheckedItems
'If tempLocation.ToString = -1 Then
'tempLocation.Insert(0, itm)
' End If
' Next
.Parameters.AddWithValue("@LocationID", tempLocation)
End With
'max = comm.ExecuteScalar()
da.SelectCommand = comm
da.Fill(ds, "Show")
grdTEST.DataSource = ds.Tables("Show")
'grdCost.RowsDefaultCellStyle.BackColor = Drawing.Color.White
'grdCost.AlternatingRowsDefaultCellStyle.BackColor = Drawing.Color.PowderBlue
Catch exsql As SqlException
MsgBox(exsql.Message, MsgBoxStyle.Exclamation)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
End Sub
จากตัวอย่าง และ Code ด้านบน กรณีที่ผมเลือกตัวเดียวก็สามารถเก็บค่ามาไว้ใน tempLocation ได้แล้ว แต่ถ้าเกิดเราเลือกหลายตัวเราจะต้องเขียน Code อย่างไรครับ
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2013-06-11 14:53:29 |
By :
s_oil_p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim comm As New SqlCommand
With comm
If CheckedListBox1.CheckedItems.Count = 0 Then
' ไม่เลือกเลย
Exit Sub
End If
Dim objArrList = New ArrayList(CheckedListBox1.CheckedItems.Count)
For Each chItem In CheckedListBox1.CheckedItems
objArrList.Add(chItem)
Next
If CheckedListBox1.CheckedItems.Count = 1 Then
' เลือก 1 ช่อง
.Parameters.AddWithValue("@LocationID", objArrList(0))
End If
If CheckedListBox1.CheckedItems.Count = 2 Then
' เลือก 2 ช่อง
.Parameters.AddWithValue("@LocationID", objArrList(0))
.Parameters.AddWithValue("@LocationID1", objArrList(1))
End If
If CheckedListBox1.CheckedItems.Count = 3 Then
' เลือก 3 ช่อง
.Parameters.AddWithValue("@LocationID", objArrList(0))
.Parameters.AddWithValue("@LocationID1", objArrList(1))
.Parameters.AddWithValue("@LocationID2", objArrList(2))
End If
If CheckedListBox1.CheckedItems.Count = 4 Then
' เลือก 4 ช่อง
.Parameters.AddWithValue("@LocationID", objArrList(0))
.Parameters.AddWithValue("@LocationID1", objArrList(1))
.Parameters.AddWithValue("@LocationID2", objArrList(2))
.Parameters.AddWithValue("@LocationID3", objArrList(3))
End If
If CheckedListBox1.CheckedItems.Count = 5 Then
' เลือก 5 ช่อง
.Parameters.AddWithValue("@LocationID", objArrList(0))
.Parameters.AddWithValue("@LocationID1", objArrList(1))
.Parameters.AddWithValue("@LocationID2", objArrList(2))
.Parameters.AddWithValue("@LocationID3", objArrList(3))
.Parameters.AddWithValue("@LocationID4", objArrList(4))
End If
End With
คงจะเป็นประโยชน์นะ
|
 |
 |
 |
 |
Date :
2013-06-11 17:46:24 |
By :
a |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ สำหรับคำตอบ
แต่คงใช้ไม่ได้หรอกครับ เพราะข้อมูลผมเยอะมาก เพราะอาจจะมีการ Update ฐานข้อมูลอยู่เรื่อยๆๆ
|
 |
 |
 |
 |
Date :
2013-06-11 18:21:38 |
By :
s_oil_p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|