insert data from dbgrid into SQL SERVER database table

Thread Solved

Join Date: Nov 2009
Posts: 6
Reputation: maracaibolago is an unknown quantity at this point 
Solved Threads: 0
maracaibolago maracaibolago is offline Offline
Newbie Poster

insert data from dbgrid into SQL SERVER database table

 
0
  #1
23 Days Ago
Hello,
Is it possible to insert the data from dbgrid component into an SQL SERVER database table.
The data that is in the grid is imported from Excel worksheet.
It may look silly but I need to insert that data into a table.

Any help is appreciated, thank you
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: cao is an unknown quantity at this point 
Solved Threads: 4
cao cao is offline Offline
Newbie Poster
 
0
  #2
23 Days Ago
Since you did not state which components you use to connect to Excel I have used ADO

Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.  
  4. {Depending on which components you use to connect to SQL
  5.   Server the syntax might vary but the underlying concept should
  6.   be the same
  7.  
  8.   This example does not not how to connect to the SQL Server
  9.   but how to get data from the Excel spreadsheet using TADODataset
  10.   }
  11.  
  12. { ADOQuery1 sql should be INSERT INTO "table_name" ("column1", "column2", }
  13. { VALUES ("value1", "value2", ...) }
  14.  
  15. ADOConnection1.connected := True;
  16.  
  17. with ADODAtaSet1 do
  18. begin
  19. Active := True;
  20. DisableControls;
  21. try
  22. First;
  23. { Initialize each parameter with excel data, execute the query and repeat }
  24. while not Eof do
  25. begin
  26. { Process each record here }
  27.  
  28. ADOQuery1.Parameters.ParamByName('Value1').Value :=
  29. Fields.Fields[1].AsString;
  30. ADOQuery1.Parameters.ParamByName('Value2').Value :=
  31. Fields.Fields[2].AsString;
  32. ADOQuery1.Parameters.ParamByName('Value3').Value :=
  33. Fields.Fields[3].AsString;
  34. ...
  35. ADOQuery1.ExecSQL;
  36.  
  37. Next;
  38. end;
  39. finally
  40. EnableControls;
  41. end;
  42. end;
  43. end;
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: maracaibolago is an unknown quantity at this point 
Solved Threads: 0
maracaibolago maracaibolago is offline Offline
Newbie Poster

Thank you!

 
0
  #3
20 Days Ago
Thank you very much for your help!

this is what I have so far:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7. Dialogs, DB, ADODB, ExtCtrls, DBCtrls, Grids, DBGrids, ComCtrls, StdCtrls,
  8. Buttons,typinfo;
  9.  
  10. type
  11. TForm1 = class(TForm)
  12. Label1: TLabel;
  13. Edit1: TEdit;
  14. Label2: TLabel;
  15. Edit2: TEdit;
  16. ComboBox1: TComboBox;
  17. Label3: TLabel;
  18. ListBox1: TListBox;
  19. StatusBar1: TStatusBar;
  20. DataSource1: TDataSource;
  21. DBGrid1: TDBGrid;
  22. DBNavigator1: TDBNavigator;
  23. ADOConnection1: TADOConnection;
  24. ADOQuery1: TADOQuery;
  25. BitBtn1: TBitBtn;
  26. Button1: TButton;
  27. OpenDialog1: TOpenDialog;
  28. Button2: TButton;
  29. ADODataSet1: TADODataSet;
  30. Button3: TButton;
  31. procedure FormCreate(Sender: TObject);
  32. procedure DisplayException(Sender:TObject; E: Exception);
  33. procedure ConnectToExcel;
  34. procedure FetchData;
  35. procedure BitBtn1Click(Sender: TObject);
  36. procedure OpenDialog1CanClose(Sender: TObject; var CanClose: Boolean);
  37. procedure Button1Click(Sender: TObject);
  38. procedure GetFieldInfo;
  39. procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  40. DataCol: Integer; Column: TColumn; State: TGridDrawState);
  41. procedure Button2Click(Sender: TObject);
  42. procedure Button3Click(Sender: TObject);
  43. private
  44. { Private declarations }
  45. public
  46. { Public declarations }
  47. end;
  48.  
  49. var
  50. Form1: TForm1;
  51.  
  52. implementation
  53.  
  54. {$R *.dfm}
  55.  
  56. procedure TForm1.FetchData;
  57. begin
  58. StatusBar1.SimpleText:='';
  59.  
  60. ConnectToExcel;
  61. AdoQuery1.Close;
  62. AdoQuery1.SQL.Text := Edit2.Text;
  63. try
  64. AdoQuery1.Open;
  65. except
  66. ShowMessage('Unable to read data from Excel, make sure the query '+Edit1.Text+' is meaningful!');
  67. raise;
  68.  
  69. end;
  70. end;
  71.  
  72. procedure TForm1.FormCreate(Sender: TObject);
  73. begin
  74. AdoConnection1.loginprompt := False;
  75. AdoQuery1.Connection := AdoConnection1;
  76. DataSource1.DataSet := AdoQuery1;
  77. DBGrid1.DataSource := DataSource1;
  78. DBNavigator1.DataSource := DataSource1;
  79.  
  80. Application.OnException := DisplayException;
  81. end;
  82.  
  83.  
  84. procedure TForm1.GetFieldInfo;
  85.  
  86. var
  87. i : integer;
  88. ft : TFieldType;
  89. sft : string;
  90. fname : string;
  91. begin
  92. Listbox1.Clear;
  93. for i := 0 to AdoQuery1.Fields.Count - 1 do
  94. begin
  95. ft := AdoQuery1.Fields[i].DataType;
  96. sft := GetEnumName(TypeInfo(TFieldType), Integer(ft));
  97. fname := AdoQuery1.Fields[i].FieldName;
  98.  
  99. ListBox1.Items.Add(Format('%d) NAME: %s TYPE: %s',[1+i, fname, sft]));
  100. end;
  101. end;
  102.  
  103. procedure TForm1.OpenDialog1CanClose(Sender: TObject; var CanClose: Boolean);
  104. begin
  105. Edit1.Text:=OpenDialog1.Filename;
  106. end;
  107.  
  108. procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  109. DataCol: Integer; Column: TColumn; State: TGridDrawState);
  110. begin
  111. if (adoquery1.RecNo mod 2) = 0 then
  112. begin
  113. if dbgrid1.Canvas.Brush.Color = DBGrid1.Color then
  114. dbgrid1.Canvas.Brush.Color := clInfoBk;
  115. end;
  116. dbgrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  117.  
  118. end;
  119.  
  120. procedure TForm1.DisplayException(Sender: TObject; E: Exception);
  121. begin
  122. StatusBar1.SimpleText := E.Message;
  123. end;
  124.  
  125. procedure TForm1.BitBtn1Click(Sender: TObject);
  126. begin
  127. FetchData;
  128. GetFieldInfo;
  129. end;
  130.  
  131. procedure TForm1.Button1Click(Sender: TObject);
  132. begin
  133. Edit1.Clear;
  134. OpenDialog1.Execute;
  135. end;
  136.  
  137. procedure TForm1.Button2Click(Sender: TObject);
  138. begin
  139. Listbox1.Visible := not ListBox1.visible;
  140. end;
  141.  
  142. procedure TForm1.ConnectToExcel;
  143. var strConn : widestring;
  144. begin
  145. strConn := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
  146. 'Data Source=' + Edit1.Text + ';' +
  147. 'Extended Properties=Excel 8.0;';
  148.  
  149. AdoConnection1.Connected:=False;
  150. AdoConnection1.ConnectionString:=strConn;
  151. try
  152. AdoConnection1.Open;
  153. AdoConnection1.GetTableNames(ComboBox1.Items,True);
  154. except
  155. Showmessage('Unable to connect to Excel, make sure the workbook ' + Edit1.Text + ' exist!');
  156. raise;
  157. end;
  158. end;
  159.  
  160. end.
when I create this new button and put the code you gave me it says: Missing connection or ConnectionString...

I know, I am dumb with this. I am just starting with Delphi.

Thank you very much!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: maracaibolago is an unknown quantity at this point 
Solved Threads: 0
maracaibolago maracaibolago is offline Offline
Newbie Poster

a solution I found, I hope it helps others

 
0
  #4
17 Days Ago
Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. begin
  3.  
  4.  
  5. ADOConnection1.connected := True;
  6.  
  7. with ADOQuery1 do
  8. begin
  9. Active := True;
  10. DisableControls;
  11. try
  12. First;
  13. { Initialize each parameter with excel data, execute the query and repeat }
  14. while not Eof do
  15. begin
  16. { Process each record here }
  17.  
  18. AdoQuery2.Close;
  19. AdoQuery2.SQL.Clear;
  20. AdoQuery2.SQL.Add('INSERT INTO AMIR');
  21. AdoQuery2.SQL.Add('(IDENT,IME,PREZIME,GRAD)');
  22. AdoQuery2.SQL.Add('VALUES');
  23. AdoQuery2.SQL.Add('(:parIDENT,:parIME,:parPREZIME,:parGRAD)');
  24.  
  25. AdoQuery2.Parameters.ParamByName('parIDENT').DataType := ftString;
  26. AdoQuery2.Parameters.ParamByName('parIME').DataType := ftString;
  27. AdoQuery2.Parameters.ParamByName('parPREZIME').DataType := ftString;
  28. AdoQuery2.Parameters.ParamByName('parGRAD').DataType := ftString;
  29.  
  30. AdoQuery2.Parameters.ParamByName('parIDENT').Direction := pdInput;
  31. AdoQuery2.Parameters.ParamByName('parIME').Direction := pdInput;
  32. AdoQuery2.Parameters.ParamByName('parPREZIME').Direction := pdInput;
  33. AdoQuery2.Parameters.ParamByName('parGRAD').Direction := pdInput;
  34.  
  35. AdoQuery2.Parameters.ParamByName('parIDENT').Value:= Fields.Fields[0].AsString;
  36. AdoQuery2.Parameters.ParamByName('parIME').Value:= Fields.Fields[1].AsString;
  37. AdoQuery2.Parameters.ParamByName('parPREZIME').Value:= Fields.Fields[2].AsString;
  38. AdoQuery2.Parameters.ParamByName('parGRAD').Value:= Fields.Fields[3].AsString;
  39. AdoQuery2.ExecSQL;
  40.  
  41. NEXT;
  42.  
  43.  
  44.  
  45. // Next;
  46. end;
  47. showmessage('Uspješno');
  48. finally
  49. EnableControls;
  50. end;
  51. end;
  52.  
  53. end;
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: cao is an unknown quantity at this point 
Solved Threads: 4
cao cao is offline Offline
Newbie Poster

insert data from dbgrid into SQL SERVER database table

 
0
  #5
16 Days Ago
You do not need to recreate the query or parameter list with each iteration. See my change below

Originally Posted by maracaibolago View Post
Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. begin
  3.  
  4.  
  5. ADOConnection1.connected := True;
  6.  
  7. with ADOQuery1 do
  8. begin
  9. Active := True;
  10. DisableControls;
  11. try
  12. First;
  13. { Initialize each parameter with excel data, execute the query and repeat }
  14.  
  15. AdoQuery2.Close;
  16. AdoQuery2.SQL.Clear;
  17. AdoQuery2.SQL.Add('INSERT INTO AMIR');
  18. AdoQuery2.SQL.Add('(IDENT,IME,PREZIME,GRAD)');
  19. AdoQuery2.SQL.Add('VALUES');
  20. AdoQuery2.SQL.Add('(:parIDENT,:parIME,:parPREZIME,:parGRAD)');
  21.  
  22. AdoQuery2.Parameters.ParamByName('parIDENT').DataType := ftString;
  23. AdoQuery2.Parameters.ParamByName('parIME').DataType := ftString;
  24. AdoQuery2.Parameters.ParamByName('parPREZIME').DataType := ftString;
  25. AdoQuery2.Parameters.ParamByName('parGRAD').DataType := ftString;
  26.  
  27. AdoQuery2.Parameters.ParamByName('parIDENT').Direction := pdInput;
  28. AdoQuery2.Parameters.ParamByName('parIME').Direction := pdInput;
  29. AdoQuery2.Parameters.ParamByName('parPREZIME').Direction := pdInput;
  30. AdoQuery2.Parameters.ParamByName('parGRAD').Direction := pdInput;
  31.  
  32. { You do not need to recreate the query or parameter list with each iteration. Thats the benefit of paramaters the query needs to be prepared once only on the server }
  33.  
  34. while not Eof do
  35. begin
  36. { Process each record here }
  37.  
  38. AdoQuery2.Parameters.ParamByName('parIDENT').Value:= Fields.Fields[0].AsString;
  39. AdoQuery2.Parameters.ParamByName('parIME').Value:= Fields.Fields[1].AsString;
  40. AdoQuery2.Parameters.ParamByName('parPREZIME').Value:= Fields.Fields[2].AsString;
  41. AdoQuery2.Parameters.ParamByName('parGRAD').Value:= Fields.Fields[3].AsString;
  42. AdoQuery2.ExecSQL;
  43.  
  44. NEXT;
  45.  
  46.  
  47.  
  48. // Next;
  49. end;
  50. showmessage('Uspješno');
  51. finally
  52. EnableControls;
  53. end;
  54. end;
  55.  
  56. end;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC