重写mysql类
重新写了mysql类,实现了数据库的增减,以及执行mysql命令。不知道还需要添加什么功能,请感兴趣的网友告知,我再添加。。。
代码如下:
程序代码:
************************************************** *-- 类: mysql (d:\documents\visual foxpro 项目\myclass.vcx) *-- 父类: custom *-- 基类: custom *-- 时间戳: 04/04/24 09:51:01 PM * DEFINE CLASS mysql AS custom Height = 19 Width = 66 *-- 数据源名称。 datasource = "" *-- 用户名。 user = "" *-- 密码。 password = "" *-- 当前数据库。 currentdatabase = "null" *-- 记录连接状态。 connected = .F. *-- 保存连接mysql数据库的句柄。 handle = -1 Name = "mysql" *-- 建立mysql连接。 PROCEDURE connect IF EMPTY(this.dataSource) .or. EMPTY(this.user) .or. EMPTY(this.password) carray='' oform=NEWOBJECT([connector],[myclass],null,carray) oform.show IF ALEN(carray)=1 && 用户取消连接 this.connected=.f. MESSAGEBOX([用户取消连接mysql服务器!]) RETURN this.connected ELSE this.dataSource=ALLTRIM(carray[1]) this.user=ALLTRIM(carray[2]) this.password=ALLTRIM(carray[3]) ENDIF ENDIF this.handle=SQLCONNECT(this.datasource,this.user,this.password) IF this.handle<=0 this.connected=.f. MESSAGEBOX([无法连接mysql服务器!]) this.datasource='' this.user='' this.password='' ELSE this.connected=.t. MESSAGEBOX([已连接上mysql服务器!]+[连接]+TRANSFORM(this.handle)+[。]) ENDIF RETURN this.connected ENDPROC *-- 断开sql连接。 PROCEDURE disconnect IF this.handle>0 SQLDISCONNECT(this.handle) this.handle=-1 this.connected=.f. this.dataSource='' this.user='' this.password='' ENDIF ENDPROC *-- 执行查询命令。 PROCEDURE sqlexe PARAMETERS csqlcmd IF this.handle>0 n=SQLEXEC(this.handle,csqlcmd) IF n>0 MESSAGEBOX([sql语句已执行!]) RETURN .t. ENDIF ENDIF MESSAGEBOX([sql语句未被正确执行!]) RETURN .f. ENDPROC *-- 新建一个mysql数据库。 PROCEDURE create PARAMETERS cname IF this.connected=.t. n=SQLEXEC(this.handle,[create DATABASE if not exists ]+cname) IF n>0 this.currentdatabase=cname MESSAGEBOX([成功创建数据库]+cname+[!]) SQLEXEC(this.handle,[use ]+cname) RETURN .t. ENDIF ENDIF MESSAGEBOX([无法创建数据库]+cname+[!]) RETURN .f. ENDPROC *-- 选择一个mysql数据库为当前数据库。 PROCEDURE select PARAMETERS cname IF this.connected=.t. n=SQLEXEC(this.handle,[use ]+cname) IF n>0 this.currentdatabase=cname MESSAGEBOX([已切换到当前数据库]+cname+[!]) RETURN .t. ENDIF ENDIF MESSAGEBOX([无法切换数据库]+cname+[!]) RETURN .f. ENDPROC *-- 删除一个mysql数据库。 PROCEDURE delete PARAMETERS cname nanswer=MESSAGEBOX('一旦删除将无法恢复!',1+16+256,'警告!') IF this.connected=.t. AND nanswer=1 n=SQLEXEC(this.handle,[drop DATABASE if exists ]+cname) IF n>0 this.currentdatabase=null MESSAGEBOX([当前数据库]+cname+[已删除!]) RETURN .t. ENDIF ENDIF MESSAGEBOX([无法删除数据库]+cname+[!]) RETURN .f. ENDPROC ENDDEFINE * *-- EndDefine: mysql **************************************************
数据库的登录界面的类定义如下:
程序代码:
************************************************** *-- 类: connector (d:\documents\visual foxpro 项目\myclass.vcx) *-- 父类: form *-- 基类: form *-- 时间戳: 04/04/24 09:51:00 PM * DEFINE CLASS connector AS form Height = 250 Width = 375 DoCreate = .T. AutoCenter = .T. Caption = "Connect to mysql..." MaxButton = .F. WindowType = 1 WindowState = 0 Name = "connector" ADD OBJECT shape1 AS shape WITH ; Top = 12, ; Left = 12, ; Height = 229, ; Width = 349, ; Name = "Shape1" ADD OBJECT shape2 AS shape WITH ; Top = 13, ; Left = 13, ; Height = 227, ; Width = 347, ; BorderColor = RGB(255,255,255), ; Name = "Shape2" ADD OBJECT label1 AS label WITH ; AutoSize = .T., ; Caption = "连接mysql数据库", ; Height = 16, ; Left = 28, ; Top = 7, ; Width = 92, ; Name = "Label1" ADD OBJECT label2 AS label WITH ; AutoSize = .T., ; Caption = "数据源名称:", ; Height = 16, ; Left = 26, ; Top = 66, ; Width = 74, ; Name = "Label2" ADD OBJECT text1 AS textbox WITH ; Height = 20, ; Left = 146, ; Top = 64, ; Width = 100, ; Name = "Text1" ADD OBJECT label3 AS label WITH ; AutoSize = .T., ; Caption = "用户名:", ; Height = 16, ; Left = 26, ; Top = 113, ; Width = 50, ; Name = "Label3" ADD OBJECT label4 AS label WITH ; AutoSize = .T., ; Caption = "密码:", ; Height = 16, ; Left = 26, ; Top = 162, ; Width = 38, ; Name = "Label4" ADD OBJECT text2 AS textbox WITH ; Height = 20, ; Left = 146, ; Top = 111, ; Width = 100, ; Name = "Text2" ADD OBJECT text3 AS textbox WITH ; Height = 20, ; Left = 146, ; Top = 160, ; Width = 100, ; PasswordChar = "*", ; Name = "Text3" ADD OBJECT command1 AS commandbutton WITH ; Top = 204, ; Left = 224, ; Height = 25, ; Width = 60, ; Caption = "确定", ; Name = "Command1" ADD OBJECT command2 AS commandbutton WITH ; Top = 204, ; Left = 292, ; Height = 25, ; Width = 60, ; Caption = "取消", ; Name = "Command2" PROCEDURE Init *!* 用法如下: *!* calia='' *!* oform=NEWOBJECT([connector],[myclass],null,calia) *!* oform.Show *!* ?ALEN(carray) &&如果等于1,则表示用户取消连接任务 *!* ?carray[1] PARAMETERS carray IF PARAMETERS()=0 MESSAGEBOX([需要一个数组参数!]) RETURN .f. ENDIF ENDPROC PROCEDURE command1.Click IF EMPTY(thisform.text1.value) .or. EMPTY(thisform.text2.Value) .OR. EMPTY(thisform.text3.Value) MESSAGEBOX([请正确输入数据源,用户名及密码!]) ELSE DIMENSION carray[3] carray[1]=thisform.text1.Value carray[2]=thisform.text2.Value carray[3]=thisform.text3.value thisform.release ENDIF ENDPROC PROCEDURE command2.Click DIMENSION carray[1] thisform.Release() ENDPROC ENDDEFINE * *-- EndDefine: connector **************************************************