VB.NET & System.Data.SqlClient - Parameter Query (OleDbParameter) - How to learn Connector/NET ADO.NET component System.Data.SqlClient namespace is the .NET Framework Data Provider for SQL Server data source, Using the SqlParameter for mapping to a DataSet column (SQL Server 2000,2005,2008 Database)
ShotDev Focus:
- VB.NET & System.Data.SqlClient - Parameter Query (OleDbParameter)
Example
ParameterQuery.aspx
<%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.SqlClient"%> <%@ 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.SqlClient.SqlConnection Dim objCmd As System.Data.SqlClient.SqlCommand Dim strConnString,strSQL As String strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;" objConn = New System.Data.SqlClient.SqlConnection(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.SqlClient.SqlCommand(strSQL,objConn) '*** Sample 1 ***' 'With objCmd ' .Parameters.AddWithValue("@sCustomerID","C005") ' .Parameters.AddWithValue("@sName","Weerachai Nukitram") ' .Parameters.AddWithValue("@sEmail","webmaster@shotdev.com") ' .Parameters.AddWithValue("@sCountryCode","TH") ' .Parameters.AddWithValue("@sBudget","2000000") ' .Parameters.AddWithValue("@sUsed","1000000") 'End With '*** Sample 2 ***' With objCmd .Parameters.Add(New SqlParameter("@sCustomerID","C005")) .Parameters.Add(New SqlParameter("@sName","Weerachai Nukitram")) .Parameters.Add(New SqlParameter("@sEmail","webmaster@shotdev.com")) .Parameters.Add(New SqlParameter("@sCountryCode","TH")) .Parameters.Add(New SqlParameter("@sBudget","2000000")) .Parameters.Add(New SqlParameter("@sUsed","1000000")) End With objCmd.ExecuteNonQuery() lblText.Text = "Record Insert Sucessful." '*** FOR UPDATE ***' 'strSQL = "UPDATE customer SET Budget = @sBudget " & _ '" WHERE CustomerID = @sCustomerID " 'objCmd = New System.Data.SqlClient.SqlCommand(strSQL,objConn) 'With objCmd ' .Parameters.Add(New SqlParameter("@sBudget","4000000")) ' .Parameters.Add(New SqlParameter("@sCustomerID","C005")) 'End With 'objCmd.ExecuteNonQuery() '*** FOR DELETE ***' 'strSQL = "DELETE FROM customer " & _ '" WHERE CustomerID = @sCustomerID " 'objCmd = New System.Data.SqlClient.SqlCommand(strSQL,objConn) 'With objCmd ' .Parameters.Add(New SqlParameter("@sCustomerID","C005")) 'End With 'objCmd.ExecuteNonQuery() objCmd = Nothing objConn.Close() objConn = Nothing End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:Label id="lblText" runat="Server"></asp:Label> </form> </body> </html>
Screenshot