en-UShe-IL
You are here:   Blog
Register   |  Login

Blog Archive:

Maximize
* Can be used in order to search for older blogs Entries

Search in blogs


Blog Categories:

Maximize
* Can be used in order to search for blogs Entries by Categories

Blog Tags:

Maximize
* Can be used in order to search for blogs by keywords

TNWikiSummit


Awared MVP 


 


Microsoft® Community Contributor 


Microsoft® Community Contributor


 Read first, before you you use the blog! Maximize
דצמ22

Written by: ronen ariely
22/12/2010 11:04 RssIcon

Using Parameters in Dot.Net SQL Queries

 

Pass parameter to OleDbCommand

 

String connect =

   "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Employee.mdb";
OleDbConnection con = new OleDbConnection(connect);
con.Open();
OleDbCommand cmd1 = con.CreateCommand();
cmd1.CommandText = "SELECT ID FROM Employee "
                 + "WHERE id BETWEEN ? AND ?";
OleDbParameter p1 = new OleDbParameter();
OleDbParameter p2 = new OleDbParameter();
cmd1.Parameters.Add(p1);
cmd1.Parameters.Add(p2);
p1.Value = "01";
p2.Value = "03";
OleDbDataReader reader = cmd1.ExecuteReader();
while(reader.Read())
  Console.WriteLine("{0}", reader.GetInt32(0));
reader.Close();
con.Close();


 

 

public void CreateMyOleDbCommand(
                OleDbConnection connection,
                string queryString,
                OleDbParameter[] parameters)
{
    OleDbCommand command = new OleDbCommand(queryString, connection);
 
    command.CommandText =
        "SELECT CustomerID, CompanyName " +
        "FROM Customers " +
        "WHERE Country = ? AND City = ?";
    command.Parameters.Add(parameters);
 
    for (int j=0; j
    {
        command.Parameters.Add(parameters[j]) ;
    }
    string message = "";
    for (int i = 0; i < command.Parameters.Count; i++)
    {
        message += command.Parameters[i].ToString() + "\n";
    }
    Console.WriteLine(message);
}

 

 

Public Sub CreateMyOleDbCommand(connection As OleDbConnection, _
 
   queryString As String, parameters() As OleDbParameter)
    Dim command As New OleDbCommand(queryString, connection)
    command.CommandText = _
       "SELECT CustomerID, CompanyName FROM Customers WHERE Country = ? AND City = ?"
    command.Parameters.Add(parameters)
    Dim j As Integer
    For j = 0 To command.Parameters.Count - 1
       command.Parameters.Add(parameters(j))
    Next j
    Dim message As String = ""
    Dim i As Integer
    For i = 0 To command.Parameters.Count - 1
        message += command.Parameters(i).ToString() + ControlChars.Cr
    Next i
    Console.WriteLine(message)
End Sub