Thursday, 18 July 2013

what is TADOQuery component for Delphi?

TADOQuery component is aimed at execution of SQL-commands. It can be considered as an analog of TSQLQuery component from dbExpress. Connection to the database is set via Connection or ConnectionString properties. Query text is written to SQL property. If a query returns the dataset one should use Open() method or Active=true property. If a query does not return the dataset, it shall be run using ExecSQL method. ExecSQL returns the number of records processed during a query running. The same value is contained in RowsAffected property.

Wednesday, 17 July 2013

What is TADOConnection in Delphi

TADOConnection component connects to the data storage. TADOConnection resembles TSQLConnection component. The difference between them is that when working with TSQLConnection connection parameters are set via ConnectionNameDriverName and Paramsproperties. In TADOConnection all connection parameters are set via ConnectionString property. Moreover, it is possible to specify file name with connection parameters as a connection string in TADOConnection.

Connection String with Firebird database in Delphi XE2

procedure TForm1.Button1Click(Sender: TObject);
var 
 Conn: TSQLConnection;
begin
  Conn := TSQLConnection.Create(Self);
  try
    Conn.DriverName := 'Provider=MSDASQL.1; '+
                       'Persist Security Info=False; '+
                       'Data Source=DataBaseName; '+
                       'Initial Catalog=DataBaseName';'; 
    Conn.Params.Add('User_Name=SYSDBA');
    Conn.Params.Add('Password=masterkey');

    // Replace the dbname in the next line with the
    // value obtained at runtime, as  in
    // Conn.Params.Add('Database=' + YourNewPathAndDBName);
    Conn.Params.Add('Database=D:\FolderName\DataBaseName.fdb');

    Conn.Open;
    if Conn.Connected then
      ShowMessage('Connection successfully made to DB');
  finally
    Conn.Free;
  end;
end;

Monday, 8 July 2013

Q :- What is the difference between the SuperServerclassic and embedded servers ?

Ans:-SuperServer is a single-process multi-threaded server (like Apache on Windows, for example) Classic is a multi-process single-threade server - things are synced via a separate lock manager process Embedded is a DLL you hook up to your application and work with database file directly
SuperServer is good for OLTP, lot of users, etc. Classic is good for OLAP or few users running heavy queries. Embedded is good for desktop apps.
You should install and use SuperServer while developing, since embedded only allows one connection at a time, so you cannot access the database from your application and administration tool at the same time. Later, you can deploy using any of those 3 architectures.

Thursday, 18 April 2013

Simple Application in VB.Net


Public Class Form1
    Dim sqlconnection As New System.Data.SqlClient.SqlConnection
    Dim sqlCommand As New System.Data.SqlClient.SqlCommand
    Dim sqlDataAdapter As New System.Data.SqlClient.SqlDataAdapter
    Dim cmdBuilder As System.Data.SqlClient.SqlCommandBuilder
    Dim dsDataset As New DataSet
    Dim cmCurrency As CurrencyManager
    Dim LastUID As Integer = 0
    Dim newrow As DataRow

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
        sqlconnection.ConnectionString = "Data Source=Manoj;Initial Catalog=imported_data;User ID=sa;Password=sa"
        sqlCommand.CommandText = "select * from mstcompanies"
        sqlCommand.Connection = sqlconnection
        sqlDataAdapter.SelectCommand = sqlCommand

        cmdBuilder = New System.Data.SqlClient.SqlCommandBuilder(sqlDataAdapter)
        sqlDataAdapter.Fill(dt)
        DataGridView1.DataSource = dt

        txtUID.Text = dt.Rows(0).Item(0)
        txtName.Text = dt.Rows(0).Item(2)
        txtAddress.Text = dt.Rows(0).Item(4)
        txtCity.Text = dt.Rows(0).Item(5)

        dsDataset.Tables.Add(dt)
        dsDataset.Tables(0).TableName = "mstcompanies"
        sqlDataAdapter.TableMappings.Add(New System.Data.Common.DataTableMapping("Table", "mstCompanies"))

        Call bindFields(dt)
        cmCurrency = BindingContext(DataGridView1.DataSource, DataGridView1.DataMember)
    End Sub
    Public Function bindFields(ByVal dtSrc As DataTable)
        txtUID.DataBindings.Add("Text", dtSrc, "UID")
        txtName.DataBindings.Add("Text", dtSrc, "Name")
        txtAddress.DataBindings.Add("Text", dtSrc, "Address")
        txtCity.DataBindings.Add("Text", dtSrc, "City")
    End Function

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        txtUID.Text = ""
        txtName.Text = ""
        txtAddress.Text = ""
        txtCity.Text = ""

        newrow = dsDataset.Tables("mstCompanies").NewRow
        LastUID = dsDataset.Tables("mstcompanies").Rows(dsDataset.Tables("mstcompanies").Rows.Count - 1).Item("UID")
        txtUID.Text = LastUID + 1
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        With dsDataset.Tables("mstcompanies")

            If txtName.Text = "" Then
                MsgBox("Please Enter Company Name")
                Exit Sub
            End If

            dsDataset.Tables("mstcompanies").Rows.Add(newrow)
            newrow.Item("UID") = txtUID.Text + 1
            newrow.Item("Name") = txtName.Text
            newrow.Item("Address") = txtAddress.Text
            newrow.Item("City") = txtCity.Text

            sqlDataAdapter.Update(dsDataset)
            MsgBox("Record Save Successfully.")


        End With
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        cmCurrency.RemoveAt(cmCurrency.Position)
        sqlDataAdapter.Update(dsDataset)
        MsgBox("Record Delete Successfully.")
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        dsDataset.Tables("mstCompanies").Rows(cmCurrency.Position).Item("Name") = txtName.Text
        dsDataset.Tables("mstCompanies").Rows(cmCurrency.Position).Item("Address") = txtAddress.Text
        dsDataset.Tables("mstCompanies").Rows(cmCurrency.Position).Item("City") = txtCity.Text
        sqlDataAdapter.Update(dsDataset)
        MsgBox("Record Update Successfully.")
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        cmCurrency.Position = cmCurrency.Position - 1

    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        cmCurrency.Position = cmCurrency.Position + 1
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        cmCurrency.Position = 0
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        cmCurrency.Position = cmCurrency.Count
    End Sub
End Class

Thursday, 11 April 2013

What is a Delphi unit?


A Delphi unit is a separate file used to store procedures and functions. If you know what a form is, a unit is exactly the same, except it has no visual interface. So you can't put windows controls on it like buttons and edit boxes. A form has windows controls and their associated code behind them, a unit only has the code.
They are useful if you have some functions that you use often from many different forms and you only want to write them once. For example:
function LeftStr(const S : String; Index : Integer) : String; 
begin 
If Index <= 0 
then 
Result := '' 
else 
Result := Copy(S, 1, Index); 
end;
function RightStr(const S : String; Index : Integer) : String; 
begin 
If Index > Length(S) 
then 
Result := '' 
else 
Result := Copy(S, Index, (Length(S)-Index+1)); 
end; 

Then you can have your unit's name in a forms uses clause and then use the functions LeftStr and RightStr from several different forms.

Tuesday, 9 April 2013

What is the function of Inheritance in Delphi?


  • The inheritance needs a parent and a child class where the child class inherits the property of the parent class. 
  • Child can have their own functions or inherit functions and properties from the parent class. 
  •  TFootball child class is the base class and inheriting the properties of TBall class of the parents. 
  • For Ex.
var
beachBall : TBall;
soccerBall : TFootball;
begin
beachBall := TBall.Create(5);
soccerBall := TFootball.Create(5, 12);

beachBall.Kick(10);
soccerBall.Kick(10);

ShowMessageFmt('Beach ball is moving at speed : %d',[beachBall.GetSpeed]);
ShowMessageFmt('Soccer ball is moving at speed : %d',[soccerBall.GetSpeed]);
end;

The output is shown as such:
Beach ball is moving at speed: 12
Soccer ball is moving at speed: 12

How does inheritance operate when using the object of the classes?


- The inheritance needs a parent and a child class where the child class inherits the property of the parent class. Child can have their own functions or inherit functions and properties from the parent class. 
TFootball child class is the base class and inheriting the properties of TBall class of the parents. 

For Ex.

var
beachBall : TBall;
soccerBall : TFootball;
begin
beachBall := TBall.Create(5);
soccerBall := TFootball.Create(5, 12);

beachBall.Kick(10);
soccerBall.Kick(10);

ShowMessageFmt('Beach ball is moving at speed : %d',[beachBall.GetSpeed]);
ShowMessageFmt('Soccer ball is moving at speed : %d',[soccerBall.GetSpeed]);
end;

The output is shown as such:
Beach ball is moving at speed: 12
Soccer ball is moving at speed: 12

What are the different types of inheritance present in Delphi?


  1.  Specialization inheritance: this is the type of inheritance in which the child class is the advanced version of the parent’s class. 
  2. Class inheritance: provides a way for the child class to include the features and complete properties of the parent’s class. It is used to extend the parent class and provide the left out features and methods. 
  3. Abstract inheritance: this is the method that provides other skeleton methods which is being implemented by the child class. 
  4. Interface: this is the way by which the class implements all the skeleton methods by using the interface itself.

What are the reasons involved in using the interface?


  • Interfaces are used to implement the skeleton methods and provide a way through which the class can extend other classes. 
  • The example shows that a sub-class can extend the bicycle class that contains mountain bike, etc. 
  • They provide lots of inherited functions and features that can be added to provide unique features. 
  • They are allowed to apply one method to multiple classes and extend more than one class functionality and characteristics.
  •  High level classes can be defined using the interface in this case it can consists of methods and other definitions of classes.

What is the function of Dynamic arrays?


 Dynamic arrays are the array type that defines their sizes at runtime or according to the data     entered. 
 The dynamic arrays are declared and defined as shown below:

var
wishes : array of string; 
begin
SetLength(wishes, 3); 
end;

  • SetLength is a routine that is used to set the array size according to the string entered. 
  •  The size of the string or the input is defined at runtime that is why it is called dynamic arrays. 
  • The array size can be changed and decreased or increased according to the requirements. 
  • Dynamic arrays starts with an index 0 and it can grow according to the need of the input.

What are the different types of pointers used in Delphi?


  • There are typed pointers types that are being provided by Delphi like PChar and PExtended. 
  • These points work with the Inc and Dec functions and don’t have any issue with the use of it. 
  • They can increment the Pint64 pointer type and add the SizeOf(Int64) bytes at the pointer address location. 
  • This way it allows the pointing of the pointer to point to the next memory location in the memory. 

Thursday, 28 March 2013

Virtual and Dynamic Methods in Delphi .


To make a method virtual or dynamic, include the virtual or dynamic directive in its declaration. Virtual and dynamic methods  can be overridden in descendent classes. When an overridden method is called, the actual (run-time) type of the class or object used in the method call--not the declared type of the variable--determines which implementation to activate.
To override a method, redeclare it with the override directive. An override declaration must match the ancestor declaration in the order and type of its parameters and in its result type (if any).
In the following example, the Draw method declared in TFigure is overridden in two descendent classes:

 type
     TFigure = class
       procedure Draw; virtual;
     end;
 
     TRectangle = class(TFigure)
       procedure Draw; override;
     end;
 
     TEllipse = class(TFigure)
       procedure Draw; override;
     end;

what is Static Methods in delphi ?

Methods are by default static. When a static method is called, the declared (compile-time) type of the class or object variable used in the method call determines which implementation to activate. In the following example, the Draw methods are static:


type
     TFigure = class
       procedure Draw;
     end;
 
     TRectangle = class(TFigure)
       procedure Draw;
     end;

Tuesday, 26 March 2013

What is TClientDataSet in Delphi ?

In order to display the information from the TSQLTable (or any dbExpress dataset), we need to cache it inside a TClientDataSet component, using a TDataSetProvider component as "connector". So, drop both a TDataSetProvider and a TClientDataSet component from the Data Access tab of the Component Palette. Assign the SQLTable component to the DataSet property of the DataSetProvider, and then assign the name of the DataSetProvider to the ProviderName property of the ClientDataSet. 
As soon as you open the ClientDataSet (for example by setting the Active property to True), the content of the TSQLTable will be traversed (just once) and the records in the resultset will be provided to the ClientDataSet, which will cache them from that moment on. We can now use a DataSource and (for example) a DBGrid component to display the contents - provided by the ClientDataSet component.

Monday, 25 March 2013

What is hierarchy of VCL ?

TObject -> TPersistence -> TComponent -> TControl -> TWinControl -> TCustomEdit -> TEdit.

Friday, 22 March 2013

Function String_Reverse(S : String): String;

Var
  i : Integer;
Begin
    Result := '';
    For i := Length(S) DownTo 1 Do
  Begin
   
Result := Result + Copy(S,i,1) ;
   End;
End;

What is the difference between TList and TStringlist?

Tlist is used to stores an array of pointers, is often used to maintain lists of objects. TList has properties and methods to
  • Add or delete the objects in the list.
  • Rearrange the objects in the list.
  • Locate and access objects in the list.
  • Sort the objects in the list.
TStringList maintains a list of strings.TStringList is used to
  • Add or delete strings at specified positions in the list.
  • Rearrange the strings in the list.
  • Access the string at a particular location.
  • Read the strings from or write the strings to a file or stream.
  • Associate an object with each string in the list.
  • Store and retrieve strings as name-value pairs.
  • Sort the strings in the list.
  • Prohibit duplicate strings in sorted lists.
  • Respond to changes in the contents of the list.
  • Control whether strings are located, sorted, and identified as duplicates in a case-sensitive or case-insensitive manner.

Describe different concepts of oops in delphi .

OOPs concepts are -
  1. object
  2. class
  3. encapsulation
  4. abstraction
  5. polymorphism
  6. inheritance
  7. message passing
  8. dynamic binding
BASIC CONCEPT OF OOPS:
1.OBJECTS:
An object is an abstraction of a real world entity. It may represent a person,a place, a number and icons or something else that can be modelled.Any data in an object occupy some space in memory and can communicate with each other. 
 
2.CLASSES:

A class is a collection of objects having common features .It is a user defined data types which has data members as well functions that manipulate these data's.
 
3.ABSTRACTION:

An abstract method is a virtual or dynamic method that has no implementation in the class where it is declared. Its implementation is deferred to a descendent class. Abstract methods must be declared with the directive abstract after virtual or dynamic.
 For example:

 procedure DoSomething; virtual; abstract;
 
4.ENCAPSULATION:

It is a mechanism that puts the data and function together. It is the result of hiding implementation details of an object from its user .The object hides its data to be accessed  by only those functions which are packed in the class of that object.
 
5.INHERITANCE:

It is the relationship between two classes of object such that one of the classes ,the child takes all the relevant features of other class -the parent.Inheritance bring about  re-usability.
 
6.POLYMORPHISM:

polymorphism means having many forms that in a single entity can takes more than one form.Polymorphism is implemented through operator overloading and function overloading.
 
7.DYNAMIC BINDING:

Dynamic binding is the process of resolving the function to be associated with the respective functions calls during their run time rather than compile time.

8.MESSAGE PASSING:

Every data in an object in oops that is capable of processing request known as message .All object can communicate with each other by sending message to each other.

For Ex.

  type
     TTextBox = class(TCustomControl)
       private
        procedure WMChar(var Message: TWMChar); message WM_CHAR;
        ...
     end;


What's is class in Delphi ?

A class is a collection of objects having common features .It is a user defined data types which has data members as well functions that manipulate these data's.

Thursday, 21 March 2013

What is RTTI in Delphi ?.

Runtime Type Information is information about an object's data type that is set into memory at run-time.
RTTI provides a way to determine if an object's type is that of a particular class or one of its descendants. The is and as operators commonly used in Delphi language.
The is operator, which performs dynamic type checking, is used to verify the actual runtime class of an object ans as operator performs checked typecasts.

Ex.
Here's a small example of the powers of the RTTI. This example shows how to obtain the ancestry of a component using the ClassType and ClassParent properties. It uses a button and a list box on a form. When the user clicks the button, the name of the button’s class and the names of its parent classes are added to the list box (found in the Delphi Help).

procedure TForm1.Button1Click(Sender: TObject) ;

var
   ClassRef: TClass;
begin
   ListBox1.Clear;
   ClassRef := Sender.ClassType;
   while ClassRef <> nil do
   begin
     ListBox1.Items.Add(ClassRef.ClassName) ;
     ClassRef := ClassRef.ClassParent;
   end;

end;

Wednesday, 20 March 2013

What is different between Forms and DataModule in Delphi .

Ans.
     On the Forms you can place visual and non visual component  (Ex. Tlable, Tbutton, Adotable). but on the  Data  module you can place only non visual component.

When working with a large number of records in the dataset, what DataSet method should the application call to prevent data - aware controls from updating every time t he active record changes?

a) BlockUpdate
b) DisableControls
c) Disable
d) None of the above
 
Answer: b. DisableControls 

Which generics classes will destroy an object when they are removed from the list, assuming the list owns the object? ( Choose all that apply )

a) TList
b) TObjectList
c) TDictionary
d) TObjectDictionary
e) TObjectList and TObjectDictionary.
 
Answer : e. TObjectList and TObjectDictionary

In which memory area are local variables and procedure parameters stored?

a) Stack
b) Heap
c) Random access area
d) None of above.
 
Answer : a. Stack

In the Delphi language, which symbols or words define the declaration of an attribute?

a) { }
b) < >
c) [ ]
d) & &
e) None of the above
 
Answer :c. [ ]