ASP.NET MySql.Data.MySqlClient - Parameter Query (MySqlParameter) |
ASP.NET MySql.Data.MySqlClient - Parameter Query() การเขียน ASP.NET ใช้งาน NameSpace MySql.Data.MySqlClient กับ Parameter Query() - MySqlParameter() เพื่อกำหนดคุณสมบัติของพารามิเตอร์ที่จะทำการโยนค่าให้กับ Statement
Instance NameSpace
VB.NETImports System.Data
Imports MySql.Data.MySqlClient
MySqlParameter Syntax
Dim QueryString As String = "SELECT * FROM Table1 WHERE Field1 LIKE ?p1"
Dim Cmd As MySqlCommand = New MySqlCommand(QueryString, Connection)
Cmd.Parameters.Add("?p1", MySqlDbType.VarChar, 3).Value = "a"
Language Code : VB.NET || C#
ParameterQuery.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="MySql.Data.MySqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Sample1()
End Sub
Sub Sample1()
Dim objConn As MySql.Data.MySqlClient.MySqlConnection
Dim objCmd As MySql.Data.MySqlClient.MySqlCommand
Dim strConnString,strSQL As String
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
objConn = New MySql.Data.MySqlClient.MySqlConnection(strConnString)
objConn.Open()
'*** FOR INSERT ***'
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
"VALUES (?sCustomerID,?sName,?sEmail,?sCountryCode,?sBudget,?sUsed)"
objCmd = New MySql.Data.MySqlClient.MySqlCommand(strSQL,objConn)
With objCmd
.Parameters.Add(new MySqlParameter("?sCustomerID", "C005"))
.Parameters.Add(new MySqlParameter("?sName", "Weerachai Nukitram"))
.Parameters.Add(new MySqlParameter("?sEmail", "[email protected]"))
.Parameters.Add(new MySqlParameter("?sCountryCode", "TH"))
.Parameters.Add(new MySqlParameter("?sBudget", "2000000"))
.Parameters.Add(new MySqlParameter("?sUsed", "100000"))
End With
objCmd.ExecuteNonQuery()
lblText.Text = lblText.Text & "- Record Inserted"
'*** FOR UPDATE ***'
'strSQL = "UPDATE customer SET Budget = ?sBudget " & _
'" WHERE CustomerID = ?sCustomerID "
'objCmd = New MySql.Data.MySqlClient.MySqlCommand(strSQL,objConn)
'With objCmd
' .Parameters.Add(new MySqlParameter("?sBudget", "4000000"))
' .Parameters.Add(new MySqlParameter("?sCustomerID", "C005"))
'End With
'objCmd.ExecuteNonQuery()
'*** FOR DELETE ***'
'strSQL = "DELETE FROM customer " & _
'" WHERE CustomerID = ?sCustomerID "
'objCmd = New MySql.Data.MySqlClient.MySqlCommand(strSQL,objConn)
'With objCmd
' .Parameters.Add(new MySqlParameter("?sCustomerID", "C005"))
'End With
'objCmd.ExecuteNonQuery()
objCmd = Nothing
objConn.Close()
objConn = Nothing
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - MySql.Data.MySqlClient</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="Server"></asp:Label>
</form>
</body>
</html>
Screenshot
MySqlDbType
MySqlDbType.Blob
MySqlDbType.Byte
MySqlDbType.Date
MySqlDbType.Datetime
MySqlDbType.Decimal
MySqlDbType.Double
MySqlDbType.Enum
MySqlDbType.Float
MySqlDbType.Geometry
MySqlDbType.Int16
MySqlDbType.Int24
MySqlDbType.Int32
MySqlDbType.Int64
MySqlDbType.LongBlob
MySqlDbType.MediumBlob
MySqlDbType.Newdate
MySqlDbType.Set
MySqlDbType.String
MySqlDbType.Time
MySqlDbType.Timestamp
MySqlDbType.TinyBlob
MySqlDbType.VarChar
MySqlDbType.Year
ASP.NET - Transaction
ตัวอย่างการเขียน .NET ร่วมกับ Parameter
|