จะเก็บข้อมูลในอาเรย์ทำอย่างไรคะ [VB.Net] คือว่าอยากดึงข้อมูลจากฟิลด์ A (โดยที่ฟิลด์ A มีข้อมูลอยู่หลายข้อมูล
Field A ของคุณนี่คือ database หรือครับ ไม่เคลียร์ ถ้าใช่ ก็
SELECT A FROM "table คุณ ชื่ออะไร"
แล้วเอาค่าที่ได้ใส่ data table มันก็คือ array ดีๆ นี่เอง คุณจะไปปู้ยี๋ปู้ยำ ไงก็ได้แล้ว...
Date :
2010-02-03 17:11:01
By :
numenoy
ขอบคุณค่ะ
Date :
2010-02-04 14:45:31
By :
MOR
คือว่าอ่าน datatable แล้วยังงงอยู่อะค่ะ เลยเอาไปเขียนแบบนี้ค่ะ
Dim MyConn As String = WebConfigurationManager.ConnectionStrings("MORConnect").ToString()
Dim Conn As New SqlConnection(MyConn)
Sub Select_Scala_Stock()
Dim ad As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd As SqlCommand
Dim sql As String
sql = "Select a.StockCode,a.Description1,a.UserDefined3,a.ProductGroup,a.NetWeight,a.SupplierCode,"
sql = sql & "a.Purchaser,a.QueryQueNo,a.ExtprodGroup,a.AltProdGroup,a.StdCostPric1,a.ArticlStatus,"
sql = sql & "a.DelTimeWeek,a.DelTimeUnit,b.stockPM"
sql = sql & " from ScalaDB.dbo.[V-SC0101-Stock file] a inner join ScalaDB.dbo.[Stockextend0100] b on a.StockCode = b.SC01001"
ad = New SqlDataAdapter(sql, Conn)
ad.Fill(ds, "stock")
Dim dview As DataView
dview = ds.Tables("Stock").DefaultView
Dim drowview As DataRowView
For Each drowview In dview
Session("St_StockCode") = drowview.Item("StockCode")
Session("St_Description") = drowview.Item("Description1")
Session("St_MainGroup") = drowview.Item("UserDefined3")
Session("St_ProductGroup") = drowview.Item("ProductGroup")
Session("St_NetWeight") = drowview.Item("NetWeight")
Session("St_SupplierCode") = drowview.Item("SupplierCode")
Session("St_PM") = drowview.Item("stockPM")
Session("St_TMode") = drowview.Item("Purchaser")
Session("St_SubDivision") = drowview.Item("QueryQueNo")
Session("St_BF") = drowview.Item("ExtprodGroup")
Session("St_SBU") = drowview.Item("AltProdGroup")
Session("St_StdCost") = drowview.Item("StdCostPric1")
Session("St_ArticleStatus") = drowview.Item("ArticlStatus")
Session("St_DelTimeWeek") = drowview.Item("DelTimeWeek")
Session("St_DelTimeUnit") = drowview.Item("DelTimeUnit")
Next
sql = "insert into MOR_Stock (StockCode,Description,MainGroup,ProductGroup,NetWeight,SupplierCode,"
sql = sql & "PM,TMode,SubDivision,BF,SBU,StdCost,ArticleStatus,DelTimeWeek,DelTimeUnit)Values("
sql = sql & "'" & Session("St_StockCode") & "'" & "," & "'" & Session("St_Description") & "'" & ","
sql = sql & "'" & Session("St_MainGroup") & "'" & "," & "'" & Session("St_ProductGroup") & "'" & ","
sql = sql & Session("St_NetWeight") & "," & "'" & Session("St_SupplierCode") & "'" & ","
sql = sql & "'" & Session("St_PM") & "'" & "," & "'" & Session("St_TMode") & "'" & ","
sql = sql & "'" & Session("St_SubDivision") & "'" & "," & "'" & Session("St_BF") & "'" & ","
sql = sql & "'" & Session("St_SBU") & "'" & "," & Session("St_StdCost") & ","
sql = sql & "'" & Session("St_ArticleStatus") & "'" & "," & "'" & Session("St_DelTimeWeek") & "'" & ","
sql = sql & "'" & Session("St_DelTimeUnit") & "'" & ")"
cmd = New SqlCommand(sql, Conn)
Conn.Open()
cmd.ExecuteNonQuery()
Conn.Close()
End Sub
คิดว่ามันสามารถอ่านทีละเรคคอร์ดแล้วบันทึกจนหมด ที่ไหนได้กลับบันทึกได้แค่เรคคอร์ดเดียว อย่างนี้ต้องทำอย่างไรคะ
Date :
2010-02-04 21:53:10
By :
MOR
Dim MyConn As String = WebConfigurationManager.ConnectionStrings("MORConnect").ToString()
Dim Conn As New SqlConnection(MyConn)
Sub Select_Scala_Stock()
Dim ad As New SqlDataAdapter
Dim ds As New DataSet
Dim cmd As SqlCommand
Dim sql As String
sql = "Select a.StockCode,a.Description1,a.UserDefined3,a.ProductGroup,a.NetWeight,a.SupplierCode,"
sql = sql & "a.Purchaser,a.QueryQueNo,a.ExtprodGroup,a.AltProdGroup,a.StdCostPric1,a.ArticlStatus,"
sql = sql & "a.DelTimeWeek,a.DelTimeUnit,b.stockPM"
sql = sql & " from ScalaDB.dbo.[V-SC0101-Stock file] a inner join ScalaDB.dbo.[Stockextend0100] b on a.StockCode = b.SC01001"
ad = New SqlDataAdapter(sql, Conn)
ad.Fill(ds, "stock")
Dim dview As DataView
dview = ds.Tables("Stock").DefaultView
Dim drowview As DataRowView
For Each drowview In dview
Session("St_StockCode") = drowview.Item("StockCode")
Session("St_Description") = drowview.Item("Description1")
Session("St_MainGroup") = drowview.Item("UserDefined3")
Session("St_ProductGroup") = drowview.Item("ProductGroup")
Session("St_NetWeight") = drowview.Item("NetWeight")
Session("St_SupplierCode") = drowview.Item("SupplierCode")
Session("St_PM") = drowview.Item("stockPM")
Session("St_TMode") = drowview.Item("Purchaser")
Session("St_SubDivision") = drowview.Item("QueryQueNo")
Session("St_BF") = drowview.Item("ExtprodGroup")
Session("St_SBU") = drowview.Item("AltProdGroup")
Session("St_StdCost") = drowview.Item("StdCostPric1")
Session("St_ArticleStatus") = drowview.Item("ArticlStatus")
Session("St_DelTimeWeek") = drowview.Item("DelTimeWeek")
Session("St_DelTimeUnit") = drowview.Item("DelTimeUnit")
Next
sql = "insert into MOR_Stock (StockCode,Description,MainGroup,ProductGroup,NetWeight,SupplierCode,"
sql = sql & "PM,TMode,SubDivision,BF,SBU,StdCost,ArticleStatus,DelTimeWeek,DelTimeUnit)Values("
sql = sql & "'" & Session("St_StockCode") & "'" & "," & "'" & Session("St_Description") & "'" & ","
sql = sql & "'" & Session("St_MainGroup") & "'" & "," & "'" & Session("St_ProductGroup") & "'" & ","
sql = sql & Session("St_NetWeight") & "," & "'" & Session("St_SupplierCode") & "'" & ","
sql = sql & "'" & Session("St_PM") & "'" & "," & "'" & Session("St_TMode") & "'" & ","
sql = sql & "'" & Session("St_SubDivision") & "'" & "," & "'" & Session("St_BF") & "'" & ","
sql = sql & "'" & Session("St_SBU") & "'" & "," & Session("St_StdCost") & ","
sql = sql & "'" & Session("St_ArticleStatus") & "'" & "," & "'" & Session("St_DelTimeWeek") & "'" & ","
sql = sql & "'" & Session("St_DelTimeUnit") & "'" & ")"
cmd = New SqlCommand(sql, Conn)
Conn.Open()
cmd.ExecuteNonQuery()
Conn.Close()
End Sub
For loop คุณ เอาค่าลง session ไว้ มันก็ loop ถูกครับ แต่พอ จะ INSERT Database คุณ ไม่เอา เข้า Loop ด้วย มันจะ Insert เป็น Loop ได้ไงละครับ
For Each drowview In dview
Session("St_StockCode") = drowview.Item("StockCode")
Session("St_Description") = drowview.Item("Description1")
Session("St_MainGroup") = drowview.Item("UserDefined3")
Session("St_ProductGroup") = drowview.Item("ProductGroup")
Session("St_NetWeight") = drowview.Item("NetWeight")
Session("St_SupplierCode") = drowview.Item("SupplierCode")
Session("St_PM") = drowview.Item("stockPM")
Session("St_TMode") = drowview.Item("Purchaser")
Session("St_SubDivision") = drowview.Item("QueryQueNo")
Session("St_BF") = drowview.Item("ExtprodGroup")
Session("St_SBU") = drowview.Item("AltProdGroup")
Session("St_StdCost") = drowview.Item("StdCostPric1")
Session("St_ArticleStatus") = drowview.Item("ArticlStatus")
Session("St_DelTimeWeek") = drowview.Item("DelTimeWeek")
Session("St_DelTimeUnit") = drowview.Item("DelTimeUnit")
sql = "insert into MOR_Stock (StockCode,Description,MainGroup,ProductGroup,NetWeight,SupplierCode,"
sql = sql & "PM,TMode,SubDivision,BF,SBU,StdCost,ArticleStatus,DelTimeWeek,DelTimeUnit)Values("
sql = sql & "'" & Session("St_StockCode") & "'" & "," & "'" & Session("St_Description") & "'" & ","
sql = sql & "'" & Session("St_MainGroup") & "'" & "," & "'" & Session("St_ProductGroup") & "'" & ","
sql = sql & Session("St_NetWeight") & "," & "'" & Session("St_SupplierCode") & "'" & ","
sql = sql & "'" & Session("St_PM") & "'" & "," & "'" & Session("St_TMode") & "'" & ","
sql = sql & "'" & Session("St_SubDivision") & "'" & "," & "'" & Session("St_BF") & "'" & ","
sql = sql & "'" & Session("St_SBU") & "'" & "," & Session("St_StdCost") & ","
sql = sql & "'" & Session("St_ArticleStatus") & "'" & "," & "'" & Session("St_DelTimeWeek") & "'" & ","
sql = sql & "'" & Session("St_DelTimeUnit") & "'" & ")"
cmd = New SqlCommand(sql, Conn)
Conn.Open()
cmd.ExecuteNonQuery()
Conn.Close()
Next <<< ย้ายลงมาข้างล่างแบบนี้ก็ Insert ได้แล้ว แต่!!! คุณ เปิดปิด connection บ่อยๆ แบบนี้ไม่ดีครับ ให้คุณไปศึกษาเรื่อง transaction เพิ่มนะครับ ผมแค่ guide ให้คุณเท่านั้น
Date :
2010-02-05 08:56:51
By :
numenoy
มันก็ insert ได้แค่เรคคอร์ดเดียวเองค่ะ
เรื่อง transaction นี่ไรคะ แต่จะศึกษาดูค่ะ
Date :
2010-02-05 09:17:50
By :
somooo
แต่พอเอา pk ออกมันสามารถมาได้เพิ่มอีก 3 เรคคอร์ดค่ะ ซึ่ง 3 เรคคอร์ดเนี้ย ข้อมูลเหมือนกันหมดเลยค่ะ
พอลองเอาคำสั่ง sql ไปคิวรี่ดู มันแสดงมาประมาณ 30 เรคคอร์ด อย่างนี้ต้องทำอย่างไรคะ
Date :
2010-02-05 10:56:38
By :
somooo
ได้แล้วค่ะ
เอาตัวแปรมารับค่าแทน session ถึงจะได้ค่ะ
Date :
2010-02-05 13:45:37
By :
somooo
Load balance : Server 00