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