Solved

using a list of objects, Delphi

Posted on 2013-02-01
3
616 Views
Last Modified: 2013-02-02
I have this function in c# and a similar one in java, and  I am trying to implement in Delphi, but can't figure how to do it

basically, instead of writing a bunch of code to build a query, assign it to a query component, clearing and setting the parameters, I want to build a parameterized query, add parameters, and call a function that does the clean up, open, error checking, etc.

the parameters will be different data types.

I did this in c# and java, so there's gotta be a way to do it in Delphi - pretty much the only thing I'm stuck on is the object list.  I haven't used it before and can't seem to find a sample of how to do this, as all the samples seem to use a single object type...

is there a way to do this?
calling logic:

      List<object> parms = new List<object>();
...
            parms.Add(dOrder["CUSTUID"]);//string
            parms.Add(result);//date
            parms.Add(aUID);//long integer
            dbRoutines.execQuery("Update CustDocs Set CustUID = @p1, DateEdited = @p2 where RecordUID = @p3", parms);

Open in new window

the function -

   public static Boolean execQuery([SqlFacet(MaxSize=-1)]String mySQL, List<object> parms)
   {
      Boolean result = true;
	  using (SqlConnection sqlConn =
		 new SqlConnection("Context Connection=True"))
		 {
          SqlCommand sqlCmd = new SqlCommand("SET NOCOUNT ON; " + mySQL, sqlConn);
			sqlCmd.CommandType = CommandType.Text;
			sqlConn.Open();
         for (int x = 0; x <= parms.Count - 1; x++)
        {
           sqlCmd.Parameters.Add(new SqlParameter("@p" + (x + 1).ToString(), parms[x]));//parameterValue)
           
        }

			try
			{
			   sqlCmd.ExecuteNonQuery();
			}
         catch(Exception Ex)
         {
            SqlContext.Pipe.Send("Error Executing query " + Ex.GetType().ToString() + " " + Ex.Message);           
            SqlContext.Pipe.Send(mySQL);
            result = false;
         }
         finally
         {
			   sqlConn.Close();
         }
		 }
      return result;
   }

Open in new window

0
Comment
Question by:ccMcBride
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 38846500
You can use TStringList instead of a TList.
TList will involve extra work of creating the object to be stored and freeing it afterwards

Here is an example using DBExpress components, You can use any other datasets to accomplish the same. Tha basic idea is creating the parameterized query.

procedure ExampleCall;
var
  Params: TStringList;
begin
  Params := TStringList.Create;
  try
    Params.Add('CUSTID=' + dOrder['CUSTUID']);//string
    Params.Add('DateEdited=' +DateTimeToStr(result));//date
    Params.Add('RecordUID=' + aUID);//long integer
    dbRoutines.execQuery('Update CustDocs Set CustUID = :CUSTID, DateEdited = :DateEdited where RecordUID = :RecordUID', Params);
  finally
    FreeAndNil(Params);
  end;
end;

procedure TForm1.ExecQuery(SQLCmd: string; AParams: TStrings);
var
  I: Integer;
  Q: TSQLQuery;
  C: TSQLConnection;
begin
  C : TSQLConnection.Create(nil);
  try
    try
      //Set your connection
      C.Open;
      Q = TSQLQuery.Create(nil);
      try
        Q.SQLConnection := C;
        Q.SQL.Add(SQLCmd);
        Q.Prepared := True;

        for I := 0 to AParams.Count -1 do
          Q.ParamByName(AParams.Names[I]).Value := AParams.ValueFromIndex(I);
        Q.ExecSQL();
      finally
        FreeAndNil(Q);
      end;
    except
      on E: Exception do
      begin
        //Handle your exception
      end;
    end;
  finally
    FreeAndNil(C);
  end;
end;

Open in new window

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 38846504
Use QuotedStr function when assigning the parameters

Q.ParamByName(AParams.Names[I]).Value := QuotedStr(AParams.ValueFromIndex(I));

Open in new window

0
 

Author Closing Comment

by:ccMcBride
ID: 38847106
thank you.  worked right out of the box (once I changed the (I) to [I])....
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question