(C#) ASP.NET System.Data.Odbc - Parameter Query (OdbcParameter) |
(C#) ASP.NET System.Data.Odbc - Parameter Query() การเขียน ASP.NET ใช้งาน NameSpace System.Data.Odbc กับ Parameter Query() - OdbcParameter() เพื่อกำหนดคุณสมบัติของพารามิเตอร์ที่จะทำการโยนค่าให้กับ Statement
Instance NameSpace
C#Using System.Data;
Using System.Data.Odbc;
Language Code : VB.NET || C#
OdbcParameter Syntax
String QueryString = "SELECT * FROM Table1 WHERE Field1 LIKE ?";
OdbcCommand Cmd = new OdbcCommand(QueryString, Connection);
Cmd.Parameters.Add("?p1", OdbcType.VarChar, 3).Value = "a";
ParameterQuery.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.Odbc"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
Sample1();
}
void Sample1()
{
System.Data.Odbc.OdbcConnection objConn;
System.Data.Odbc.OdbcCommand objCmd;
String strConnString,strSQL;
strConnString = "DSN=mydatabase;UID=;PWD=;";
objConn = new System.Data.Odbc.OdbcConnection(strConnString);
objConn.Open();
//*** FOR INSERT ***//
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
"VALUES (?,?,?,?,?,?)";
objCmd = new System.Data.Odbc.OdbcCommand(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 OdbcParameter("@sCustomerID","C005"));
objCmd.Parameters.Add(new OdbcParameter("@sName","Weerachai Nukitram"));
objCmd.Parameters.Add(new OdbcParameter("@sEmail","[email protected]"));
objCmd.Parameters.Add(new OdbcParameter("@sCountryCode","TH"));
objCmd.Parameters.Add(new OdbcParameter("@sBudget","2000000"));
objCmd.Parameters.Add(new OdbcParameter("@sUsed","1000000"));
objCmd.ExecuteNonQuery();
lblText.Text = lblText.Text + "- Record Inserted";
//*** FOR UPDATE ***'
/*
strSQL = "UPDATE customer SET Budget = ? " +
" WHERE CustomerID = ? ";
objCmd = new System.Data.Odbc.OdbcCommand(strSQL,objConn);
objCmd.Parameters.Add(new OdbcParameter("@sBudget","4000000"));
objCmd.Parameters.Add(new OdbcParameter("@sCustomerID","C005"));
objCmd.ExecuteNonQuery();
*/
//*** FOR DELETE ***'
/*
strSQL = "DELETE FROM customer " +
" WHERE CustomerID = ? ";
objCmd = new System.Data.Odbc.OdbcCommand(strSQL,objConn);
objCmd.Parameters.Add(new OdbcParameter("@sCustomerID","C005"));
objCmd.ExecuteNonQuery();
*/
objCmd = null;
objConn.Close();
objConn = null;
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - System.Data.Odbc</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="Server"></asp:Label>
</form>
</body>
</html>
Screenshot
OdbcType
OdbcType.BigInt
OdbcType.Binary
OdbcType.Bit
OdbcType.Char
OdbcType.DateTime
OdbcType.Decimal
OdbcType.Numeric
OdbcType.Double
OdbcType.Image
OdbcType.Int
OdbcType.NChar
OdbcType.NText
OdbcType.NVarChar
OdbcType.Real
OdbcType.UniqueIdentifier
OdbcType.SmallDateTime
OdbcType.SmallInt
OdbcType.Text
OdbcType.Timestamp
OdbcType.TinyInt
OdbcType.VarBinary
OdbcType.VarChar
OdbcType.Date
OdbcType.Time
ASP.NET - Transaction
ตัวอย่างการเขียน .NET ร่วมกับ Parameter
Go to : ASP.NET Access BLOB Binary Data and Parameterized Query
Go to : ASP.NET MySQL BLOB Binary Data and Parameterized Query
Go to : ASP.NET SQL Server BLOB Binary Data and Parameterized Query
Go to : ASP.NET Oracle BLOB Binary Data and Parameterized Query
|