使用AD0组件,TADOConnection, TADOTable,TDataSource,TDBGrid,TButton;
这几个组件连接sql2005.我的数据库名字是jianqiu,有个表student。里面数据是学号,姓名,性别,年龄,学院。
怎么设置和测试连接我已经明白了。但是现在我需要显示我的数据库的数据,不管数据怎么样,我只想知道这个流程。就用上面的几个组件。就显示在TDBGrid里面。
谢谢了。数据库sql2005

解决方案 »

  1.   

    用一个TDBGrid,一个TDataSource,一个TADOQuery就够了
      

  2.   

    如果你的ADOConnection1已经连接成功的话,组件名称如下
        ADOConnection1: TADOConnection;
        ADOTable1: TADOTable;
        DataSource1: TDataSource;
        DBGrid1: TDBGrid;
     你把ADOTable1 的 Connection 属性设为 ADOConnection1, TableName属性设为数据库已经存在的表名,Active设为true;
    然后DataSource1的DataSet属性设为ADOTable1;
    在把  DBGrid1的DataSource属性设为DataSource1
    这样你数据表里的内容就在 DBGrid1里面显示出来了
      

  3.   

    object Form1: TForm1
      Left = 322
      Top = 172
      Width = 409
      Height = 393
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object DBGrid1: TDBGrid
        Left = 8
        Top = 8
        Width = 377
        Height = 297
        DataSource = DataSource1
        TabOrder = 0
        TitleFont.Charset = DEFAULT_CHARSET
        TitleFont.Color = clWindowText
        TitleFont.Height = -11
        TitleFont.Name = 'MS Sans Serif'
        TitleFont.Style = []
      end
      object ADOConnection1: TADOConnection
        Connected = True
        ConnectionString =
          'Provider=SQLOLEDB.1;Password=fjjl;Persist Security Info=True;Use' +
          'r ID=sa;Initial Catalog=TmpDB;Data Source=TANGQS'
        LoginPrompt = False
        Provider = 'SQLOLEDB.1'
        Left = 272
        Top = 16
      end
      object ADOTable1: TADOTable
        Active = True
        Connection = ADOConnection1
        CursorType = ctStatic
        TableName = 'ht'
        Left = 304
        Top = 16
      end
      object DataSource1: TDataSource
        DataSet = ADOTable1
        Left = 344
        Top = 16
      end
    end
    这个设置后的窗体文件
      

  4.   

    把你的窗体文件 (  .dfm) 贴出来看看
      

  5.   

    把你的窗体文件 (  .dfm) 贴出来看看
      

  6.   

    哦,现在我在sql2005里面新加了一组数据,运行程序,也新加了以行,所以,连接啊那些都没有问题,关键是,没有显示出来
      

  7.   

    不是截图,你在窗体空白的地方点击右键,选择 View as Text,然后把里面的代码复制出来,类似我5楼回复的内容。
      

  8.   

    object Form1: TForm1
      Left = 396
      Top = 287
      Width = 447
      Height = 403
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object Panel1: TPanel
        Left = 0
        Top = 0
        Width = 431
        Height = 365
        Align = alBottom
        Color = clMenuBar
        TabOrder = 0
        object Button1: TButton
          Left = 144
          Top = 264
          Width = 75
          Height = 25
          Caption = '连接数据库'
          TabOrder = 0
          OnClick = Button1Click
        end
        object DBGrid1: TDBGrid
          Left = 24
          Top = 24
          Width = 345
          Height = 225
          Color = clScrollBar
          DataSource = DataSource1
          FixedColor = clCream
          ReadOnly = True
          TabOrder = 1
          TitleFont.Charset = DEFAULT_CHARSET
          TitleFont.Color = clWindowText
          TitleFont.Height = -11
          TitleFont.Name = 'MS Sans Serif'
          TitleFont.Style = []
          Columns = <
            item
              Expanded = False
              Font.Charset = DEFAULT_CHARSET
              Font.Color = clWindowText
              Font.Height = -13
              Font.Name = 'MS Sans Serif'
              Font.Style = []
              Title.Caption = '学生学号'
              Title.Font.Charset = DEFAULT_CHARSET
              Title.Font.Color = clWindowText
              Title.Font.Height = -13
              Title.Font.Name = 'MS Sans Serif'
              Title.Font.Style = []
              Visible = True
            end
            item
              Expanded = False
              Title.Caption = '姓名'
              Visible = True
            end
            item
              Expanded = False
              Title.Caption = '性别'
              Visible = True
            end
            item
              Expanded = False
              Title.Caption = '年龄'
              Visible = True
            end
            item
              Expanded = False
              Title.Caption = '学院'
              Visible = True
            end>
        end
      end
      object DataSource1: TDataSource
        DataSet = ADOTable1
        Left = 8
        Top = 264
      end
      object ADOTable1: TADOTable
        Connection = ADOConnection1
        CursorType = ctStatic
        TableName = 'student'
        Left = 48
        Top = 264
      end
      object ADOConnection1: TADOConnection
        Connected = True
        ConnectionString = 
          'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initi' +
          'al Catalog=jianqiu;Data Source=JIANQIU'
        Provider = 'SQLOLEDB'
        Left = 88
        Top = 264
      end
    end
    麻烦了
      

  9.   

    你的ADOTable1的Active属性没有设为true,设为True试试。
      

  10.   

    你把DBGrid1删除掉,重新放一个,除了设置DataSource属性设为DataSource1,其余都不要设置,他会自动添加的,然后再将ADOTable1的Active属性没有设为true,程序不需要运行,在设计状态下就能显示数据了
      

  11.   

    建议你将ADOTable1改为ADOQuery1,它能执行不同Sql的查询语句,能灵活显示你想要的数据