ASP.NET System.Data.OleDb - Parameter Query (OleDbParameter) |
ASP.NET System.Data.OleDb - Parameter Query() การเขียน ASP.NET ใช้งาน NameSpace System.Data.OleDb กับ Parameter Query() - OleDbParameter() เพื่อกำหนดคุณสมบัติของพารามิเตอร์ที่จะทำการโยนค่าให้กับ Statement
Instance NameSpace
VB.NETImports System.Data
Imports System.Data.OleDb
OleDbParameter Syntax
Dim QueryString As String = "SELECT * FROM Table1 WHERE Field1 LIKE @p1"
Dim Cmd As OleDbCommand = New OleDbCommand(QueryString, Connection)
Cmd.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a"
Language Code : VB.NET || C#
ParameterQuery.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Sample1()
End Sub
Sub Sample1()
Dim objConn As System.Data.OleDb.OleDbConnection
Dim objCmd As System.Data.OleDb.OleDbCommand
Dim strConnString,strSQL As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& _
Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
objConn = New System.Data.OleDb.OleDbConnection(strConnString)
objConn.Open()
'*** FOR INSERT ***'
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
"VALUES (@sCustomerID,@sName,@sEmail,@sCountryCode,@sBudget,@sUsed)"
objCmd = New System.Data.OleDb.OleDbCommand(strSQL,objConn)
'*** Sample 1 ***'
'With objCmd
' .Parameters.AddWithValue("@sCustomerID","C005")
' .Parameters.AddWithValue("@sName","Weerachai Nukitram")
' .Parameters.AddWithValue("@sEmail","[email protected]")
' .Parameters.AddWithValue("@sCountryCode","TH")
' .Parameters.AddWithValue("@sBudget","2000000")
' .Parameters.AddWithValue("@sUsed","1000000")
'End With
'*** Sample 2 ***'
With objCmd
.Parameters.Add(New OleDbParameter("@sCustomerID","C005"))
.Parameters.Add(New OleDbParameter("@sName","Weerachai Nukitram"))
.Parameters.Add(New OleDbParameter("@sEmail","[email protected]"))
.Parameters.Add(New OleDbParameter("@sCountryCode","TH"))
.Parameters.Add(New OleDbParameter("@sBudget","2000000"))
.Parameters.Add(New OleDbParameter("@sUsed","1000000"))
End With
objCmd.ExecuteNonQuery()
lblText.Text = lblText.Text & "- Record Inserted"
'*** FOR UPDATE ***'
'strSQL = "UPDATE customer SET Budget = @sBudget " & _
'" WHERE CustomerID = @sCustomerID "
'objCmd = New System.Data.OleDb.OleDbCommand(strSQL,objConn)
'With objCmd
' .Parameters.Add(New OleDbParameter("@sBudget","4000000"))
' .Parameters.Add(New OleDbParameter("@sCustomerID","C005"))
'End With
'objCmd.ExecuteNonQuery()
'*** FOR DELETE ***'
'strSQL = "DELETE FROM customer " & _
'" WHERE CustomerID = @sCustomerID "
'objCmd = New System.Data.OleDb.OleDbCommand(strSQL,objConn)
'With objCmd
' .Parameters.Add(New OleDbParameter("@sCustomerID","C005"))
'End With
'objCmd.ExecuteNonQuery()
objCmd = Nothing
objConn.Close()
objConn = Nothing
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - System.Data.OleDb</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="Server"></asp:Label>
</form>
</body>
</html>
Screenshot
OleDbType
OleDbType.BigInt
OleDbType.Binary
OleDbType.Boolean
OleDbType.BSTR
OleDbType.Char
OleDbType.Currency
OleDbType.Date
OleDbType.DBDate
OleDbType.DBTime
OleDbType.DBTimeStamp
OleDbType.Decimal
OleDbType.Double
OleDbType.Empty
OleDbType.Error
OleDbType.Filetime
OleDbType.Guid
OleDbType.IDispatch
OleDbType.Integer
OleDbType.IUnknown
OleDbType.LongVarBinary
OleDbType.LongVarChar
OleDbType.LongVarWChar
OleDbType.Numeric
OleDbType.PropVariant
OleDbType.Single
OleDbType.SmallInt
OleDbType.TinyInt
OleDbType.UnsignedBigInt
OleDbType.UnsignedInt
OleDbType.UnsignedSmallInt
OleDbType.UnsignedTinyInt
OleDbType.VarBinary
OleDbType.VarChar
OleDbType.Variant
OleDbType.VarNumeric
OleDbType.VarWChar
OleDbType.WChar
ASP.NET - Transaction
ตัวอย่างการเขียน .NET ร่วมกับ Parameter
|