Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using a list of objects, Delphi

Posted on 2013-02-01
3
Medium Priority
?
658 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:Connie McBride
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 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:Connie McBride
ID: 38847106
thank you.  worked right out of the box (once I changed the (I) to [I])....
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

963 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