 |
|
เรื่อง Colume ใน Insert INTO หากมันเยอะมาก เราจะมีวิธีเขียน ย่อไหมครับ |
|
 |
|
|
 |
 |
|
จริงๆ ไม่ต้องเขียนเลยก็ได้ครับ แค่กำหนดค่าในวงเล็บหลัง Values แค่เรียงลำดับฟิลให้ถูกก็พอครับ เช่น
Code (SQL)
INSERT INTO Test1 VALUES ( [Field_Name1] , [Field_Name2] , [Field_Name3] , ... , [Field_NameN])

|
ประวัติการแก้ไข 2013-08-07 13:53:43 2013-08-07 13:54:54
 |
 |
 |
 |
Date :
2013-08-07 13:53:01 |
By :
01000010 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากๆครับ
|
 |
 |
 |
 |
Date :
2013-08-07 14:11:21 |
By :
Bill17259 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ว่าง ๆ จัด Entity Framework กับ OR Mapping ซะหน่อย แล้วต่อด้วย MVC ให้สุด ๆ ไปเลย โย๊ว 
|
 |
 |
 |
 |
Date :
2013-08-07 14:41:55 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ๆ แล้วถ้า Field มีเยอะมากๆ เป็นพันล่ะครับ จะเขียนยังไงหรอ
|
 |
 |
 |
 |
Date :
2013-08-07 14:55:42 |
By :
Bill17259 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เมื่อก่อนผมก็ใช้วิธีเดียวกับ คุณ 0100 0010 เหมือนกันครับ
แต่อยู่ไปนานวัน มีน้องเข้ามาทำงานเดิมที่เคยทำไว้ แล้วไม่พอใจลำดับของฟิลด์ในฐานข้อมูล
ก็เลยไปจัดเรียงใหม่ แทบร้องเลยครับ โปรแกรมเอ๋อรับประทาน ( ฐานข้อมูลผม SQL Server)
ปัจจุบันก็เลยทำเป็นคลาสไปซะเลย จะลบ เพิ่ม แก้ไข ก็ใส่เข้าไป ทำครั้งเดียวใช้ตลอดการ
จะแก้ไขก็ง่ายดี OOP งับ งับ
|
 |
 |
 |
 |
Date :
2013-08-07 16:21:53 |
By :
คนงานตัดอ้อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Public Class MItem
Private _itemCode As String
Private _itemName As String
Private _barCode As String
Private _unitPrice As Double
Public Property ItemCode() As String
Get
Return _itemCode
End Get
Set(ByVal value As String)
_itemCode = value
End Set
End Property
Public Property ItemName() As String
Get
Return _itemName
End Get
Set(ByVal value As String)
_itemName = value
End Set
End Property
Public Property BarCode() As String
Get
Return _barCode
End Get
Set(ByVal value As String)
_barCode = value
End Set
End Property
Public Property UnitPrice() As Double
Get
Return _unitPrice
End Get
Set(ByVal value As Double)
_unitPrice = value
End Set
End Property
Public Sub New()
_itemCode = String.Empty
_itemName = String.Empty
_barCode = String.Empty
_unitPrice = 0.0
End Sub
Public Function GetData(ByVal pItemCode As String) As Boolean
Dim isSuccess As Boolean = False
Dim query As String = "SELECT * FROM MItem WHERE ItemCode = " & gSQ(pItemCode)
Dim dt As New DataTable
dt = gGetDataTable(query)
If dt IsNot Nothing Then
If dt.Rows.Count > 0 Then
isSuccess = FillData(dt.Rows(0))
End If
dt.Dispose()
End If
GC.SuppressFinalize(dt)
dt = Nothing
GC.Collect()
Return isSuccess
End Function
Public Function SaveData() As Boolean
Dim effect As Integer = 0
Dim query As String = "INSERT INTO MItem(ItemCode,ItemName,BarCode,UnitPrice) VALUES(@itemCode,@itemName,@barCode,@unitPrice)"
Dim conString As String = "Connection String ของคุณๆท่านๆทั้งหลาย"
Dim conn As New SqlClient.SqlConnection(conString)
Try
conn.Open()
Catch ex As Exception
conn.Dispose()
conn = Nothing
Return False
End Try
Dim cmd As New SqlClient.SqlCommand(query, conn)
With cmd
.Parameters.AddWithValue("@itemCode", _itemCode)
.Parameters.AddWithValue("@itemName", _itemName)
.Parameters.AddWithValue("@barCode", _barCode)
.Parameters.AddWithValue("@unitPrice", _unitPrice)
effect = .ExecuteNonQuery
End With
conn.Close()
conn.Dispose()
conn = Nothing
cmd.Dispose()
cmd = Nothing
If effect > 0 Then
Return True
Else
Return False
End If
End Function
Private Function FillData(ByVal pDataRow As DataRow) As Boolean
Try
If Not IsDBNull(pDataRow("ItemCode")) Then
_itemCode = pDataRow("ItemCode")
End If
If Not IsDBNull(pDataRow("ItemName")) Then
_itemName = pDataRow("ItemName")
End If
If Not IsDBNull(pDataRow("BarCode")) Then
_barCode = pDataRow("BarCode")
End If
If Not IsDBNull(pDataRow("UnitPrice")) Then
_unitPrice = pDataRow("UnitPrice")
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "MItem.FillData", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
Return True
End Function
Private Function gSQ(ByVal pString As String) As String
Return "'" & pString.Replace("'", "") & "'"
End Function
Private Function gGetDataTable(ByVal pQueryString As String) As DataTable
Dim connString As String = "Data Source = (local); Database = ABCD; Integrated Security = True; Persist Security Info = True"
Dim conn As New SqlClient.SqlConnection(connString)
Try
conn.Open()
Catch ex As Exception
MessageBox.Show(ex.Message, "gGetDataTable", MessageBoxButtons.OK, MessageBoxIcon.Error)
conn.Dispose()
conn = Nothing
Return Nothing
End Try
Dim cmd As New SqlClient.SqlCommand(pQueryString, conn)
Dim da As New SqlClient.SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
conn.Close()
conn.Dispose()
conn = Nothing
cmd.Dispose()
cmd = Nothing
da.Dispose()
da = Nothing
Return dt
End Function
End Class
คลาสข้างบนผมสมมุติว่าผมมีตารางที่เก็บข้อมูลเกี่ยวกับสินค้า
ประกอบด้วย 4 ฟิลด์ก็สร้างเป็นคุณสมบัติของคลาสไว้ (Property)
แล้วก็มีฟังก์ชั่นสำหรับทำงานในคลาสผมยกตัวอย่างไว้แค่ 2 แบบ คือ
GetData ก็คือ SELECT และ SaveData ก็คือ INSERT
สามารถไปเพิ่มได้ตามต้องการ เช่น
GetDataList ก็คือ เรียกดูข้อมูลทีละเยอะๆ คืนค่าเป็น List of MItem โดยจะเรียกดูจาก LIKE ItemCode หรือ LIKE ItemName ก็ว่าไป
DeleteData ก็คือสำหรับลบข้อมูล
UpdateData ก็คือ ปรับปรุงข้อมูล
และจะเห็นว่ามันต้องใช้ Connection ซึ่งแทบจะต้องใช้ในทุกฟังก์ชั่น ก็สามารถไปสร้างเป็นฟังก์ชั่นสาธารณะเรียกใช้เอาก็ได้
อย่างเช่นจะเห็น gGetDataTable แน่นอนว่าในทุกๆคลาสจะต้องมีการเรียกดูข้อมูล เราก็ยกมันไปไว้นอกคลาส เอาไปเป็นฟังก์ชั่น
แบบสาธารณะไปเลย เป็นต้น
หรือ จะเอาไปสร้างคลาสสำหรับทำงานเกี่ยวกับดาต้าเบสโดยตรงอีกคลาสนึง แล้วคลาสอื่นก็สืบทอดมาใช้อีกทีก็ได้ ตามสะดวก
โดยการเรียกใช้งานคลาส MItem ก็จะสามารถทำได้ดัง ความคิดเห็นถัดไปครับ
|
ประวัติการแก้ไข 2013-08-07 19:28:40 2013-08-07 19:31:02
 |
 |
 |
 |
Date :
2013-08-07 19:28:05 |
By :
คนงานตัดอ้อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim objMItem As New MItem
objMItem.ItemCode = "ITEM0001"
objMItem.ItemName = "แฟซ่าซองละ 2 บาท"
objMItem.BarCode = "8851100221144"
objMItem.UnitPrice = 2.0
If objMItem.SaveData Then
MessageBox.Show("Save Data Complete", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Save Data Failed", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
อะครับ แบบนี้วิธีใช้
ผมไม่ได้ลองรันนะครับ เอาไปงัดแงะแกะเกาเอาเน้อ ผิดตรงไหนก็เอามาแปะไว้ให้คนอื่นดูด้วย
ไม่ต้องกลัวผมอาย ผมหน้าด้านครับ 
|
 |
 |
 |
 |
Date :
2013-08-07 19:35:14 |
By :
คนงานตัดอ้อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ของผมทำแบบนี้ครับ
1. select top 0 * from table_name
2. return เข้า datarow
3. อัดข้อมูลที่อยากบันทึกเข้า datarow
4. insert from datarow
ข้อดี
---->>> function เดียว รับได้ทุก Table
ข้อเสีย
---->>> พิมพ์ชื่อ field กันทุกรอบเลย 
|
 |
 |
 |
 |
Date :
2013-08-08 09:45:13 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|