Uconn.~pas
上传用户:ddd699
上传日期:2010-01-18
资源大小:636k
文件大小:2k
源码类别:

SQL Server

开发平台:

Delphi

  1. unit Uconn;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids, DBCtrls, dbcgrids;
  6. type
  7.   TftmConn = class(TForm)
  8.     Memo1: TMemo;
  9.     connDate: TADOConnection;
  10.     ADOQuery: TADOQuery;
  11.     DataSource1: TDataSource;
  12.     BtnConn: TButton;
  13.     btnSQLOpen: TButton;
  14.     btnClose: TButton;
  15.     GroupBox1: TGroupBox;
  16.     GroupBox2: TGroupBox;
  17.     GroupBox3: TGroupBox;
  18.     GroupBox4: TGroupBox;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     Label3: TLabel;
  22.     Label4: TLabel;
  23.     txtDS: TEdit;
  24.     txtDB: TEdit;
  25.     txtName: TEdit;
  26.     txtPWD: TEdit;
  27.     DBGrid1: TDBGrid;
  28.     procedure BtnConnClick(Sender: TObject);
  29.     procedure btnSQLOpenClick(Sender: TObject);
  30.     procedure btnCloseClick(Sender: TObject);
  31.   private
  32.     { Private declarations }
  33.   public
  34.      aConStr :string;{ Public declarations }
  35.   end;
  36. var
  37.   ftmConn: TftmConn;
  38. implementation
  39. {$R *.dfm}
  40. procedure TftmConn.BtnConnClick(Sender: TObject);
  41. begin
  42.     //aConStr :='Provider=SQLOLEDB.1;Password=12345;Persist Security Info=False;User ID=sa;Initial Catalog=心理咨询;Data Source=DECAY';//“心理咨询”为数据库名
  43.     aConStr :='Provider=SQLOLEDB.1;Password='+txtPWD.Text+';Persist Security Info=False;User ID='+txtName.Text+';Initial Catalog='+txtDB.Text+';Data Source='+txtDS.Text;//“心理咨询”为数据库名
  44.     //aConStr := PromptDataSource(Application.Handle ,'');
  45.     connDate.connectionString:=aConStr;
  46.     try
  47.        connDate.Connected:=true;
  48.        // memo1.Lines.Text:= aConStr;
  49.        showmessage(aConStr);
  50.     except
  51.     showmessage('数据源连接出错,请重新建立数据库连接.');
  52.     end;
  53. end;
  54. procedure TftmConn.btnSQLOpenClick(Sender: TObject);
  55. begin
  56.  //try
  57.  // connDate.Connected:=true;
  58. try
  59. adoquery.Close;
  60. adoquery.SQL:=memo1.Lines;
  61. //showmessage(memo1.Lines.Text);
  62. adoquery.Prepared;
  63. adoquery.ExecSQL;
  64. adoquery.Active:=true;
  65. //showmessage('ok');
  66. except
  67. showmessage('SQL语法出错,请检查语法.');
  68. end;
  69. //except
  70. //showmessage('数据源连接出错,请先建立数据库连接.');
  71. //end;
  72. end;
  73. procedure TftmConn.btnCloseClick(Sender: TObject);
  74. begin
  75.  application.Terminate;
  76. end;
  77. end.