(C#) ASP.NET MySql.Data.MySqlClient - Parameter Query (MySqlParameter) |
(C#) ASP.NET MySql.Data.MySqlClient - Parameter Query() การเขียน ASP.NET ใช้งาน NameSpace MySql.Data.MySqlClient กับ Parameter Query() - MySqlParameter() เพื่อกำหนดคุณสมบัติของพารามิเตอร์ที่จะทำการโยนค่าให้กับ Statement
Instance NameSpace
C#Using System.Data;
Using MySql.Data.MySqlClient;
MySqlParameter Syntax
String QueryString = "SELECT * FROM Table1 WHERE Field1 LIKE ?p1";
MySqlCommand Cmd = 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="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
Sample1();
}
void Sample1()
{
MySql.Data.MySqlClient.MySqlConnection objConn;
MySql.Data.MySqlClient.MySqlCommand objCmd;
String strConnString,strSQL;
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);
//*** Sample 1 ***//
/*
objCmd.Parameters.AddWithValue("?sCustomerID","C005");
objCmd.Parameters.AddWithValue("?sName","Weerachai Nukitram");
objCmd.Parameters.AddWithValue("?sEmail","webmasterthaicreate.com");
objCmd.Parameters.AddWithValue("?sCountryCode","TH");
objCmd.Parameters.AddWithValue("?sBudget","2000000");
objCmd.Parameters.AddWithValue("?sUsed","1000000");
*/
//*** Sample 2 ***//
objCmd.Parameters.Add(new MySqlParameter("?sCustomerID","C005"));
objCmd.Parameters.Add(new MySqlParameter("?sName","Weerachai Nukitram"));
objCmd.Parameters.Add(new MySqlParameter("?sEmail","[email protected]"));
objCmd.Parameters.Add(new MySqlParameter("?sCountryCode","TH"));
objCmd.Parameters.Add(new MySqlParameter("?sBudget","2000000"));
objCmd.Parameters.Add(new MySqlParameter("?sUsed","1000000"));
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);
objCmd.Parameters.Add(new MySqlParameter("?sBudget","4000000"));
objCmd.Parameters.Add(new MySqlParameter("?sCustomerID","C005"));
objCmd.ExecuteNonQuery();
*/
//*** FOR DELETE ***'
/*
strSQL = "DELETE FROM customer " +
" WHERE CustomerID = ?sCustomerID ";
objCmd = new MySql.Data.MySqlClient.MySqlCommand(strSQL,objConn);
objCmd.Parameters.Add(new MySqlParameter("?sCustomerID","C005"));
objCmd.ExecuteNonQuery();
*/
objCmd = null;
objConn.Close();
objConn = null;
}
</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
|