|
 |
|
Asp.net/Ms access วิธีใช้ New ประกาศตัวแปร & การใช้ ExecuteScalar ของ OledbCommand |
|
 |
|
|
 |
 |
|
จากหมายเหตุโค้ดข้างล่างนี้ มีข้อสงสัย 2 ข้อค่ะ เป็นโค้ดที่เขียนดัดแปลงจาก ไทยครีเอทดอทคอม นี่ล่ะค่ะ
++++++++++++++++++++++++++++++++
1) ถามตรงโค้ดสีแดงก่อนค่ะ - จะเห็นว่ามีการใช้ OledbCommand กับ sql 2 คำสั่ง คือ select และต่อมา update
ตอนนี้ดิฉันกำลังเข้าใจเสมอมาว่า ทุกครั้งที่ใช้ sql คำสั่งใหม่ใน OledbCommand จะต้องใส่ New ข้างหน้า
เช่น dbcomm = New OleDbCommand(sql, dbconn) ดิฉันเข้าใจถูกไม๊คะ ???
หรือว่า
เราจะสามารถเขียนโค้ดอีกแบบที่ 2 คือ ใส่ new เฉพาะตอนประกาศตัวแปร หลังจากนั้นไม่ต้องใส่
(ขอเขียนแบบย่อๆนะ)
Dim dbcomm As New OleDbCommand
sql = "SELECT PId,AmountSale FROM ptab where PId = " & Trim(txtPId.Text)
dbcomm = OleDbCommand(sql, dbconn)
sql = "Update ptab Set AmountSale = AmountSale + " & Trim(txtUpdateAmountSale.Text) & " WHERE PId = " &
Trim(txtPId.Text)
dbcomm = OleDbCommand(sql, dbconn)
เขียนแบบที่ 2 นี้ได้ไม๊คะ ???
เอ่อ ...แล้ว New คืออะไรหรือคะ.???..ยังไม่รู้เลยค่ะ พื้นฐานไม่ค่อยแน่นค่ะ
++++++++++++++++++++++++++++++++
2) ถามตรงโค้ดสีเขียวข้างล่างค่ะ - CheckRows = dbcomm.ExecuteScalar()
ดิฉันเคยอ่านในเว็บนี้ ExecuteScalar จะมีตัวแปรรับค่า CheckRows เป็น Integer แต่ของดิฉันเป็น String จะผิดไม๊ค่ะ ??? (...แต่ตอนนี้โค้ดก็ทำงานได้ค่ะ ไม่ error แต่สงสัยจิงๆว่าผิดไม๊)
แล้วถ้าไม่ผิด ตรงโค้ดที่เขียนว่า
If CheckRows = Nothing Then
dbconn.Close()
มันเป็นสติรง ใช้ nothing จะถูกไม๊คะ ???
สาเหตุที่ต้องใช้ string
เพราะบรรทัดต่อมา จะมี ExecuteReader อ่านค่ามาใช้ต่ออีกน่ะค่ะ
++++++++++++++++++++++++++++++++
หมายเหตุ
Dim dbconn As OleDbConnection
Dim dbcomm As OleDbCommand
Dim dbread As OleDbDataReader
Dim sql, CheckRows As String
sql = "SELECT PId,AmountSale FROM ptab where PId = " & Trim(txtPId.Text)
dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath
("database/mydb.mdb") & ";Jet OLEDB:Database Password=;")
dbconn.Open()
dbcomm = New OleDbCommand(sql, dbconn)
CheckRows = dbcomm.ExecuteScalar()
If CheckRows = Nothing Then
dbconn.Close()
Page.RegisterClientScriptBlock("OnLoad", "<script>alert('ไม่มี รหัส นี้ในฐานข้อมูล กรุณากรอกใหม่')</script>")
Exit Sub
Else
dbread = dbcomm.ExecuteReader()
dbread.read()
'//////// Show Output OldAmountSale ////////////////
spOldAmountSale.InnerHtml = dbread.item("AmountSale").ToString
dbread.Close()
sql = "Update ptab Set AmountSale = AmountSale + " & Trim(txtUpdateAmountSale.Text) & " WHERE PId = " &
Trim(txtPId.Text)
dbcomm = New OleDbCommand(sql, dbconn)
Try
dbcomm.ExecuteNonQuery()
Catch ex As Exception
dbconn.Close()
dbconn = Nothing
Page.RegisterClientScriptBlock("OnLoad", "<script>alert('Update ลงฐานข้อมูลไม่ได้ / ดู Error Message ที่ด้านบนสุด
ของหน้าจอ')</script>")
Response.Write(ex.Message)
Exit Sub
End Try
dbconn.Close()
dbconn = Nothing
Tag : .NET, Ms Access, Web (ASP.NET), VS 2003 (.NET 1.1)

|
|
 |
 |
 |
 |
Date :
2011-12-11 20:52:54 |
By :
jinnie |
View :
1572 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. ถ้าคุณ New แล้วก็ไม่ต้อง New อีกครับ แต่ถ้ายังไม่ได้ New ไ New ตอนที่กำหนดค่าตัวแปรก็ได้ครับ
2. ควรทำการแปลงค่าก่อนการตรวจสอบครับ 
|
 |
 |
 |
 |
Date :
2011-12-12 06:34:25 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|