首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

数据库连接,该如何解决

2012-02-16 
数据库连接这个是怎么会事呢?//---------------------------------------#include vcl.h#pragma hdrstop

数据库连接

这个是怎么会事呢?



//---------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "student.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
 TStringList *StrLis = new TStringList;

  Session-> GetAliasNames(StrLis); //获取数据库别名 
  //Session 在构造TTable类时自动构造,通过它可以获得TTable所属Database的信息

  if (StrLis-> IndexOf( "StudentDB") == -1) //判断,如果StudentDB不存在,则动态创建
  { 
  String ExePath = ExtractFilePath(Application-> ExeName); //获得工作路径 
  Session-> AddStandardAlias( "StudentDB", ExePath,"dBase"); //同一目录下,新建数据库StudentDB
  Session-> SaveConfigFile(); //保存信息
  StrLis-> Free();

/*******************下面是TableInfor的创建过程***************************/ 
  TIndexOptions MyIndex; 
  TableInfor-> Active = false; //不打开数据库 
  TableInfor-> DatabaseName = "StudentDB "; 
  TableInfor-> TableName = "Infor ";
  TableInfor-> TableType = ttDBase; 
  //定义字段 
  TableInfor-> FieldDefs-> Clear(); 
  TableInfor-> FieldDefs-> Add( "Num ", ftString, 8, true); //定义字段名、字段类型、长度、能否为空 
  TableInfor-> FieldDefs-> Add( "Name ", ftString, 8, true); 
  TableInfor-> FieldDefs-> Add( "Sex ", ftString, 2, false); 
  TableInfor-> FieldDefs-> Add( "Age ", ftString, 4, false); 
  TableInfor-> FieldDefs-> Add( "Home ", ftString, 10, false); 
  TableInfor-> FieldDefs-> Add( "Depart ", ftString, 40, false); 
  TableInfor-> FieldDefs-> Add( "Addr ", ftString, 40, false); 
  TableInfor-> FieldDefs-> Add( "Phone ", ftString, 10, false); 
  //定义索引 
  TableInfor-> IndexDefs-> Clear(); 
  TableInfor-> IndexDefs-> Add( "IDNum ", "Num ", MyIndex<<ixPrimary); //定义索引名、索引字段、索引选项:首选索引
  MyIndex >> ixPrimary,ixUnique; //取消首选索引、唯一索引
  TableInfor-> IndexDefs-> Add( "IDName ", "Name ", MyIndex);
  TableInfor-> IndexDefs-> Add( "IDSex ", "Sex ", MyIndex); 
  TableInfor-> IndexDefs-> Add( "IDAge ", "Age ", MyIndex); 
  TableInfor-> IndexDefs-> Add( "IDHome ", "Home ", MyIndex); 
  TableInfor-> IndexDefs-> Add( "IDDepart ", "Depart ", MyIndex); 
  TableInfor-> IndexDefs-> Add( "IDAddr ", "Addr ", MyIndex); 
  TableInfor-> IndexDefs-> Add( "IDPhone ", "Phone ", MyIndex); 
  //创建Table,应用前面的定义 
  TableInfor-> CreateTable(); 


/***************************创建完毕***********************************/ 

/*********************下面再创建TableMark,方法类似***************************/ 
  TableMark-> Active = false; 
  TableMark-> DatabaseName = "StudentDB "; 
  TableMark-> TableName = "Mark "; 
  TableMark-> TableType = ttDBase; 
  TableMark-> FieldDefs-> Clear(); 
  TableMark-> FieldDefs-> Add( "Num ", ftString, 8, true); 
  TableMark-> FieldDefs-> Add( "Math ", ftString, 3, false); 
  TableMark-> FieldDefs-> Add( "Phyisics ", ftString, 3, false); 
  TableMark-> FieldDefs-> Add( "Art ", ftString, 3, false); 
  TableMark-> FieldDefs-> Add( "PE ", ftString, 3, false); 
  TableMark-> FieldDefs-> Add( "Comp ", ftString, 3, false); 
  TableMark-> FieldDefs-> Add( "Poli ", ftString, 3, false); 
  TableMark-> IndexDefs-> Clear(); 
  TableMark-> IndexDefs-> Add( "IDNum ", "Num ", MyIndex <<ixPrimary);
  MyIndex >> ixPrimary,ixUnique;
  TableMark-> IndexDefs-> Add( "IDMath ", "Math ", MyIndex); 
  TableMark-> IndexDefs-> Add( "IDPhyisics ", "Phyisics ", MyIndex); 
  TableMark-> IndexDefs-> Add( "IDArt ", "Art ", MyIndex); 
  TableMark-> IndexDefs-> Add( "IDPE ", "PE ", MyIndex); 
  TableMark-> IndexDefs-> Add( "IDComp ", "Comp ", MyIndex); 
  TableMark-> IndexDefs-> Add( "IDPoli ", "Poli ", MyIndex); 
  TableMark-> CreateTable(); 
  } 

  //若StudentDB已存在,则打开Infor,Mark表 
  TableInfor-> TableName = "Infor.DBF "; 
  TableMark-> TableName = "Mark.DBF "; 

  TableInfor-> Active = true; 
  TableMark-> Active = true; 
  PageControlGrid-> ActivePageIndex = 0; //设置当前显示页 

  //ComboBox8 -> Items = Screen -> Imes; 
  StatusBar1-> Panels-> Items[0]-> Text = "打开数据库StudentDB ";

  DBNav-> DataSource = DataSourceInfor; //设置TDBNavigator组件的数据源,从而可以对TableInfor进行操作

  //依次设置TDBEdit组件的关联字段
  DBEdit1-> DataField = "Math ";
  DBEdit2-> DataField = "Phyisics ";
  DBEdit3-> DataField = "Art ";
  DBEdit4-> DataField = "PE ";
  DBEdit5-> DataField = "Comp ";
  DBEdit6-> DataField = "Poli ";
  DBEdit7-> DataField = "Num ";

  DBEdit8-> DataField = "Num ";
  DBEdit9-> DataField = "Name ";
  DBEdit10-> DataField = "Sex ";
  DBEdit11-> DataField = "Age ";
  DBEdit12-> DataField = "Home ";
  DBEdit13-> DataField = "Depart ";
  DBEdit14-> DataField = "Addr ";
  DBEdit15-> DataField = "Phone ";

  //排序方式


  ComboBox3-> Items-> Add( "按学号排序 ");
  ComboBox3-> Items-> Add( "按姓名排序 ");
  ComboBox3-> Items-> Add( "按性别排序 ");
  ComboBox3-> Items-> Add( "按年龄排序 ");
  ComboBox3-> Items-> Add( "按籍贯排序 ");
  ComboBox3-> Items-> Add( "按所在院系排序 ");
  ComboBox3-> Items-> Add( "按住址排序 ");
  ComboBox3-> Items-> Add( "按联系电话排序 ");


}

//---------------------------------------




[解决办法]
TableInfor-> DatabaseName = "StudentDB "; 加上路径

[解决办法]
数据库里没有那个表

热点排行