- char cMyNme[250];
- unsigned long i = 250;
- GetComputerName(cMyNme, &i);//本机名
- AnsiString strConnect = "Provider=SQLOLEDB.1;";//设置连接驱动
- strConnect += "Persist Security Info=True;"; //采用集成安全机制
- strConnect += "Password=1;";//设置密码
- strConnect += "User ID=sa;";//设置用户名
- strConnect += "Data Source=" + AnsiString(cMyNme) + ";" ;//设置服务器名字
- ado_CreatQuery->ConnectionString = strConnect + "Initial Catalog=master;";
- ado_CreatQuery->Close();
- ado_CreatQuery->SQL->Clear();
- ado_CreatQuery->SQL->Add("select * from master.dbo.sysdatabases where name = 'MYDATABASE'") ;//这里可以看到所有数据库及其属性列表
- ado_CreatQuery->Open();
- if(ado_CreatQuery->Eof)
- {
- ado_CreatQuery->Close();
- ado_CreatQuery->SQL->Clear();
- ado_CreatQuery->SQL->Add("CREATE DATABASE MYTAB");
- ado_CreatQuery->ExecSQL();
- }
- ado_CreatQuery->Close();
- ado_CreatQuery->SQL->Clear();
- ado_CreatQuery->SQL->Add("select * from sysobjects where xtype='U' and name='TABLE1'");
- ado_CreatQuery->Open();
- if(ado_CreatQuery->RecordCount == 0)//如果表还没有存在
- {
- //TABLE1表设计
- ado_CreatQuery->Close();
- ado_CreatQuery->SQL->Clear();
- ado_CreatQuery->SQL->Add("USE test CREATE TABLE TABLE1(学号 float(8) NULL,姓名 text NULL)");//text和datetime类型不需要制定类型长度
- ado_CreatQuery->ExecSQL();
- }
- ado_Connection->ConnectionString = strConnect + "Initial Catalog=MYDATABASE;";//connection到MYDATABASE数据库,以便其他感应器调用
- ado_Connection->LoginPrompt = false;
- ado_Connection->Connected = true;
动态创建数据库和数据表(BCB环境)
最新推荐文章于 2017-11-02 15:39:11 发布