动态创建数据库和数据表(C++Builder)
C++Builder虽然有点落了,但是做起MSSQL来还是比较方便的。
下面简单介绍下C++Builder中创建数据库和数据表的,仅供新手参考
用了ADOQuery作为连接感应驱动
1 char cMyNme[250];
2 unsigned long i = 250;
3 GetComputerName(cMyNme, &i);//本机名
4
5 AnsiString strConnect = "Provider=SQLOLEDB.1;";//设置连接驱动
6 strConnect += "Persist Security Info=True;"; //采用集成安全机制
7 strConnect += "Password=1;";//设置密码
8 strConnect += "User ID=sa;";//设置用户名
9 strConnect += "Data Source=" + AnsiString(cMyNme) + ";" ;//设置服务器名字
10 //如果是远程,需要输入IP
11 ado_CreatQuery->ConnectionString = strConnect + "Initial Catalog=master;";
12
13 ado_CreatQuery->Close();
14 ado_CreatQuery->SQL->Clear();
15 ado_CreatQuery->SQL->Add("select * from master.dbo.sysdatabases where name = 'MYDATABASE'") ;//这里可以看到所有数据库及其属性列表
16 ado_CreatQuery->Open();
17 if(ado_CreatQuery->Eof)
18 {
19 ado_CreatQuery->Close();
20 ado_CreatQuery->SQL->Clear();
21 ado_CreatQuery->SQL->Add("CREATE DATABASE MYTAB");
22 ado_CreatQuery->ExecSQL();
23 }
24
25 ado_CreatQuery->Close();
26 ado_CreatQuery->SQL->Clear();
27 ado_CreatQuery->SQL->Add("select * from sysobjects where xtype='U' and name='TABLE1'");
28 ado_CreatQuery->Open();
29 if(ado_CreatQuery->RecordCount == 0)//如果表还没有存在
30 {
31 //////TABLE1表设计
32 ado_CreatQuery->Close();
33 ado_CreatQuery->SQL->Clear();
34 ado_CreatQuery->SQL->Add("USE test CREATE TABLE TABLE1(学号 float(8) NULL,姓名 text NULL)");//text和datetime类型不需要制定类型长度
35 ado_CreatQuery->ExecSQL();
36 }
37 ado_Connection->ConnectionString = strConnect + "Initial Catalog=MYDATABASE;";//connection到MYDATABASE数据库,以便其他感应器调用
38
39 ado_Connection->LoginPrompt = false;
40 ado_Connection->Connected = true;
2 unsigned long i = 250;
3 GetComputerName(cMyNme, &i);//本机名
4
5 AnsiString strConnect = "Provider=SQLOLEDB.1;";//设置连接驱动
6 strConnect += "Persist Security Info=True;"; //采用集成安全机制
7 strConnect += "Password=1;";//设置密码
8 strConnect += "User ID=sa;";//设置用户名
9 strConnect += "Data Source=" + AnsiString(cMyNme) + ";" ;//设置服务器名字
10 //如果是远程,需要输入IP
11 ado_CreatQuery->ConnectionString = strConnect + "Initial Catalog=master;";
12
13 ado_CreatQuery->Close();
14 ado_CreatQuery->SQL->Clear();
15 ado_CreatQuery->SQL->Add("select * from master.dbo.sysdatabases where name = 'MYDATABASE'") ;//这里可以看到所有数据库及其属性列表
16 ado_CreatQuery->Open();
17 if(ado_CreatQuery->Eof)
18 {
19 ado_CreatQuery->Close();
20 ado_CreatQuery->SQL->Clear();
21 ado_CreatQuery->SQL->Add("CREATE DATABASE MYTAB");
22 ado_CreatQuery->ExecSQL();
23 }
24
25 ado_CreatQuery->Close();
26 ado_CreatQuery->SQL->Clear();
27 ado_CreatQuery->SQL->Add("select * from sysobjects where xtype='U' and name='TABLE1'");
28 ado_CreatQuery->Open();
29 if(ado_CreatQuery->RecordCount == 0)//如果表还没有存在
30 {
31 //////TABLE1表设计
32 ado_CreatQuery->Close();
33 ado_CreatQuery->SQL->Clear();
34 ado_CreatQuery->SQL->Add("USE test CREATE TABLE TABLE1(学号 float(8) NULL,姓名 text NULL)");//text和datetime类型不需要制定类型长度
35 ado_CreatQuery->ExecSQL();
36 }
37 ado_Connection->ConnectionString = strConnect + "Initial Catalog=MYDATABASE;";//connection到MYDATABASE数据库,以便其他感应器调用
38
39 ado_Connection->LoginPrompt = false;
40 ado_Connection->Connected = true;