zoukankan      html  css  js  c++  java
  • Delphi中ADOQuery删除多表中的数据问题

    -------------------Delphi 7----

    -----本次测试没有用到第三方控件----

    ADOquery的多表关联,如果直接ADOquery1.delete;会删除多个表的数据;

    如果要删除资料,请另行写SQL语句,有人说把这个SQL语句放在ADOquery1.BeforeDelete中;感觉太麻烦;

    这在项目中怎么能行!不好控制啊,删除的统一放一起多好;个人感觉放一起好点,哈哈。

    -----------

    本人测试的这个例子,为的是写在基类中,才这么测试的!

    ------------------------

    ADOConnection1.ConnectionString 已清空,请自行配置;

    ----------------------

    ------------MS SQL Server 2008--------中---开始--

    1.表:StudentInfo

    CREATE TABLE [dbo].[StudentInfo](
    [StudentID] [varchar](10) NOT NULL,
    [StudentName] [varchar](20) NOT NULL,
    CONSTRAINT [PK_StudentInfo] PRIMARY KEY CLUSTERED
    (
    [StudentID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    2.表CourseScore:

    CREATE TABLE [dbo].[CourseScore](
    [CourseNo] [varchar](10) NOT NULL,
    [StudentID] [varchar](10) NOT NULL,
    [score] [int] NOT NULL,
    CONSTRAINT [PK_CourseScore] PRIMARY KEY CLUSTERED
    (
    [CourseNo] ASC,
    [StudentID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    ----插入数据:

    insert into dbo.StudentInfo values('A0001','小马'),('A0002','小黑'),('A0003','小白'),('A0004','小二')
    insert into dbo.CourseScore values('C001','A0001',60),('C001','A0002',63),('C001','A0003',96),('C001','A0004',81)
    insert into dbo.CourseScore values('C002','A0001',67),('C002','A0002',65),('C002','A0003',88),('C002','A0004',78)

    ------------------------------MS SQL Server 2008--------中------结束----

    Delphi Code:

    ---------Unit---开始

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids;

    type
    TOperateTable=class(TObject)
    TableName:string;
    UniqueKey:string;//Field之间请用英文状态下的分号隔开
    end;

    TForm1 = class(TForm)
    ADOQuery1: TADOQuery;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    ComboBox1: TComboBox;
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    ADOQuery2: TADOQuery;
    ADOConnection1: TADOConnection;
    ADOQuery2courseNo: TStringField;
    ADOQuery2StudentID: TStringField;
    ADOQuery2Studentname: TStringField;
    ADOQuery2score: TIntegerField;
    Button4: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    private
    FQueryInfo:TStringList;
    { Private declarations }
    public
    function QueryAdd(DBGridName:string):Boolean;
    function QueryDelete(DBGridName:string):Boolean;
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    //
    end;

    function TForm1.QueryAdd(DbgridName: string): Boolean;
    begin
    //
    end;

    procedure TForm1.FormShow(Sender: TObject);
    var
    ss:TOperateTable;
    begin
    ComboBox1.ItemIndex:=0;
    FQueryInfo:=TStringList.Create;

    //////////////DBGrid1的真实操作的表
    ss:=TOperateTable.Create;
    ss.TableName:='StudentInfo';
    ss.UniqueKey:='StudentID';//唯一索引
    FQueryInfo.AddObject('DBGrid1',ss);
    ///////////////////

    ss:=TOperateTable.Create;
    ss.TableName:='CourseScore';//DBGrid2的真实操作的表
    ss.UniqueKey:='courseNo;StudentID';//唯一索引
    FQueryInfo.AddObject('DBGrid2',ss);

    end;

    procedure TForm1.FormDestroy(Sender: TObject);
    var
    i:Integer;
    begin
    if FQueryInfo<>nil then //自己创建的就要自己释放
    begin
    if FQueryInfo.Count>0 then
    begin
    for i:=0 to FQueryInfo.Count-1 do
    begin
    FQueryInfo.Objects[i].Free;
    end;
    end;
    FQueryInfo.Clear;
    FQueryInfo.Free;
    end;
    end;

    procedure TForm1.Button4Click(Sender: TObject);
    begin
    //刷新两个数据集
    (DBGrid1.DataSource.DataSet as TADOQuery).Close;
    (DBGrid1.DataSource.DataSet as TADOQuery).Open;
    (DBGrid2.DataSource.DataSet as TADOQuery).Close;
    (DBGrid2.DataSource.DataSet as TADOQuery).Open;
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    (DBGrid1.DataSource.DataSet as TADOQuery).SQL.Text:='select * from dbo.StudentInfo ';
    (DBGrid2.DataSource.DataSet as TADOQuery).SQL.Text:='select a.courseNo,a.StudentID,b.Studentname,a.score from dbo.CourseScore a inner join dbo.StudentInfo b on a.StudentID=b.StudentID';
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
    //
    end;

    procedure TForm1.Button3Click(Sender: TObject);
    begin
    //只想删除CourseScore中的某笔数据但是会把 StudentInfo表的数据也删除了
    QueryDelete(ComboBox1.Text); //ComboBox1来指定删除那个DBGrid的数据
    end;

    function TForm1.QueryDelete(DBGridName: string): Boolean;
    var
    vQuery:TADOQuery;
    vTableInfo:TOperateTable;
    vTableWhere,vstr:string;
    vstrList:TStringList;
    i:Integer;
    vDBGrid:TDBGrid;
    begin
    Result:=False;
    if FQueryInfo.Count=0 then //用到项目中要判断FQueryInfo
    Exit;
    vDBGrid:=TDBGrid(FindComponent(DBGridName));
    if vDBGrid=nil then
    Exit;
    try
    vstrList:=TStringList.Create;
    vTableInfo:=TOperateTable(FQueryInfo.Objects[FQueryInfo.IndexOf(DBGridName)]);

    vQuery:=TADOQuery.Create(Self);
    vstrList.Delimiter:=';';
    vstrList.DelimitedText:=vTableInfo.UniqueKey;
    if vstrList.Count >0 then
    begin
    for i:=0 to vstrList.Count-1 do
    begin
    vstr:=vstrList.Strings[i];
    //MSSQL数据库会自动的类型转换,为此就算是数字类型的也按字符串类型处理!
    //vTableWhere:=vTableWhere+' and '+vstr+'='''+(vDBGrid.DataSource.DataSet as TADOQuery).fieldbyname(vstr).AsString+'''';//这个不好,最好按下面这句
    vTableWhere:=vTableWhere+' and '+vstr+'='+Quotedstr((vDBGrid.DataSource.DataSet as TADOQuery).fieldbyname(vstr).AsString);
    end;
    Delete(vTableWhere,1,5);
    vQuery.Connection:=ADOConnection1;
    vQuery.SQL.Text:=' Delete from '+vTableInfo.TableName +' Where '+vTableWhere;
    Memo1.Lines.Add(vQuery.SQL.Text);
    vQuery.Prepared:=True;
    vQuery.ExecSQL;

    (vDBGrid.DataSource.DataSet as TADOQuery).Close;
    (vDBGrid.DataSource.DataSet as TADOQuery).Open ;
    end;

    finally
    vstrList.Free;
    vQuery.Free;
    end;
    end;

    end.

    -----------Unit---结束-----------------------------------------------------------

    -------------Form开始---------

    object Form1: TForm1
    Left = 667
    Top = 298
    Width = 443
    Height = 538
    Caption = 'Form1'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    OnCreate = FormCreate
    OnDestroy = FormDestroy
    OnShow = FormShow
    PixelsPerInch = 96
    TextHeight = 13
    object Button1: TButton
    Left = 16
    Top = 328
    Width = 75
    Height = 25
    Caption = '新增'
    TabOrder = 0
    OnClick = Button1Click
    end
    object Button2: TButton
    Left = 16
    Top = 360
    Width = 75
    Height = 25
    Caption = '修改'
    TabOrder = 1
    OnClick = Button2Click
    end
    object Button3: TButton
    Left = 232
    Top = 336
    Width = 75
    Height = 25
    Caption = '删除'
    TabOrder = 2
    OnClick = Button3Click
    end
    object ComboBox1: TComboBox
    Left = 56
    Top = 304
    Width = 129
    Height = 21
    Style = csDropDownList
    ImeName = '中文(简体) - 搜狗拼音输入法'
    ItemHeight = 13
    TabOrder = 3
    Items.Strings = (
    'DBGrid1'
    'DBGrid2')
    end
    object DBGrid1: TDBGrid
    Left = 24
    Top = 16
    Width = 320
    Height = 120
    DataSource = DataSource1
    ImeName = '中文(简体) - 搜狗拼音输入法'
    TabOrder = 4
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
    end
    object DBGrid2: TDBGrid
    Left = 24
    Top = 152
    Width = 320
    Height = 120
    DataSource = DataSource2
    ImeName = '中文(简体) - 搜狗拼音输入法'
    TabOrder = 5
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
    end
    object Button4: TButton
    Left = 352
    Top = 136
    Width = 65
    Height = 25
    Caption = 'Button4'
    TabOrder = 6
    OnClick = Button4Click
    end
    object Memo1: TMemo
    Left = 16
    Top = 400
    Width = 377
    Height = 97
    ImeName = '中文(简体) - 搜狗拼音输入法'
    Lines.Strings = (
    'Memo1')
    ScrollBars = ssBoth
    TabOrder = 7
    end
    object ADOQuery1: TADOQuery
    Connection = ADOConnection1
    Parameters = <>
    SQL.Strings = (
    'select * from dbo.StudentInfo ')
    Left = 224
    Top = 64
    end
    object DataSource1: TDataSource
    DataSet = ADOQuery1
    Left = 152
    Top = 72
    end
    object DataSource2: TDataSource
    DataSet = ADOQuery2
    Left = 112
    Top = 200
    end
    object ADOQuery2: TADOQuery
    Connection = ADOConnection1
    CursorType = ctStatic
    Parameters = <>
    SQL.Strings = (

    'select a.courseNo,a.StudentID,b.Studentname,a.score from dbo.Co' +
    'urseScore a inner join dbo.StudentInfo b on a.StudentID=b.Studen' +
    'tID')
    Left = 184
    Top = 192
    object ADOQuery2courseNo: TStringField
    FieldName = 'courseNo'
    Size = 10
    end
    object ADOQuery2StudentID: TStringField
    FieldName = 'StudentID'
    Size = 10
    end
    object ADOQuery2Studentname: TStringField
    FieldName = 'Studentname'
    end
    object ADOQuery2score: TIntegerField
    FieldName = 'score'
    end
    end
    object ADOConnection1: TADOConnection
    LoginPrompt = False
    Provider = 'SQLOLEDB.1'
    Left = 304
    Top = 176
    end
    end

    ----------------Form结束--------------

  • 相关阅读:
    【初探Spring】------Spring IOC(二):初始化过程---简介
    《Effective java》-----读书笔记
    【初探Spring】------Spring IOC(一)
    Nuxt 项目性能优化调研
    一个 API 设计上的失误
    mpvue 初体验之改写【车标速查】
    微信小程序开发初体验
    pointer-events: none 的两个应用场景
    小议短网址系统的设计(有些时候,需要换个角度思考问题)
    关于 parseInt 的一道有意思的面试题
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/14797174.html
Copyright © 2011-2022 走看看