一 共用函數
1. gf_check_sqlserver
//******************************************************************//
// Function: gf_check_sqlserver
// Description:判斷是否安裝SQL是否已經啟動,如果沒有啟動則做相應處理
//--------------------------------------------------------------------
// Return: integer lo_SqlServer.status:
// 0 未知狀態
// 1 已經啟動
// 2 SQL Server為暫停狀態
// 3 SQL Server為停止狀態
// 4 SQLSERVER正在啟動(關閉-->啟動)
// 5 SQLSERVER正在關閉
// 6 SQLSERVER正在啟動(暫停-->啟動)
// 7 SQLSERVER正在暫停(啟動-->暫停)
//--------------------------------------------------------------------
// Arguments: (無)
//--------------------------------------------------------------------
// Author: Liu xj Date: 2005.12.11
//******************************************************************//
oleobject lo_SqlServer
string ls_servername
string ls_UserName ='sa'
string ls_password=''
integer li_return
ls_servername = ProfileString("user.ini","Profile","ServerName","192.168.1.2")
lo_SqlServer = create oleobject
li_return = lo_SqlServer.connecttonewobject("sqldmo.sqlserver")
if li_return = 0 then //串連成功
lo_SqlServer.name = ls_servername
lo_SqlServer.logintimeout = 10
//lo_SqlServer.loginsecure = true //以nt方式串連
lo_SqlServer.loginsecure = false //以sql server方式串連
Try
li_return = lo_SqlServer.status
Catch(RunTimeError rte)
messagebox('提示',"系統沒有安裝SQLServer",StopSign!)
End try
Choose case li_return
case 1 //已經啟動
//messagebox('提示',ls_ServerName+' 已經啟動SQLSERVER伺服器')
//lo_SqlServer.stop() //停止SQLSERVER
case 2 //SQL Server為暫停狀態
lo_SqlServer.Continue()
messagebox('提示', '伺服器已暫停,正在啟動'+ ls_servername + '上的SQL Server ... ~r~n~r~n 請稍後再試。')
case 3 //SQL Server為停止狀態
lo_SqlServer.Start(false,ls_servername,ls_username,ls_password)
messagebox('提示', '伺服器已停止,正在啟動'+ ls_servername + '上的SQL Server ... ~r~n~r~n 請稍後再試。')
case 4 //SQLSERVER正在啟動(關閉-->啟動)
messagebox('提示', ls_servername + '上的SQL Server 正在啟動 (關閉-->啟動)')
case 5 //SQLSERVER正在關閉
messagebox('提示', ls_servername + '上的SQL Server 正在關閉')
case 6 //SQLSERVER正在啟動(暫停-->啟動)
messagebox('提示', ls_servername + '上的SQL Server 正在啟動 (暫停-->啟動)')
case 7 //SQLSERVER正在暫停(啟動-->暫停)
messagebox('提示', ls_servername + '上的SQL Server 正在暫停')
case 0 //未知狀態
messagebox('提示', '未知'+ls_servername + '上的SQL Server 狀態')
End Choose
else
destroy(lo_SqlServer)
messagebox('系統提示',"系統未安裝Sql Sserver !")
return 0
end if
destroy(lo_SqlServer)
return li_return
二 創 建 數 據 庫
integer li_count
string ls_sql
string ls_dbpath
uno_des luno_des
if gf_check_sqlserver() <> 1 then return
if not wf_check_input() then return
SetPointer(HourGlass!)
timer(1)
// Profile master
itrans_master = create Transaction
itrans_master.DBMS = "MSS Microsoft SQL Server"
itrans_master.Database = 'master'
itrans_master.LogPass = is_logpass
itrans_master.ServerName = is_servername
itrans_master.LogId = is_logid
itrans_master.AutoCommit = True
SQLCA.DBParm = ""
connect using itrans_master ;
if itrans_master.sqlcode <> 0 then
messagebox("系統提示",itrans_master.sqlerrtext)
goto Error
end if
//************ 取伺服器網卡地址
select top 1 net_address into :is_servermacadd from sysprocesses where dbid=db_id('master') order by login_time DESC using itrans_master;
if isnull(is_servermacadd) or (len(is_servermacadd) <> 12) then
messagebox("系統提示",'請確認網路連接正常,然後重試。')
goto Error
else
is_servermacadd = mid(is_servermacadd,3,2) + right(is_servermacadd,6)
is_servermacadd = luno_des.uf_jiami(is_servermacadd)
//messagebox('is_servermacadd',is_servermacadd)
end if
hpb_1.position = 10
////////建立一個空庫
SELECT count(1) into :li_count FROM sysdatabases WHERE name = :is_database using itrans_master ;
if li_count > 0 then
messagebox('系統提示','資料庫已存在 !')
//goto Error
ls_sql = "DROP DATABASE " + is_database
EXECUTE IMMEDIATE :ls_sql USING itrans_master ;
if itrans_master.sqlcode <> 0 then
messagebox("系統提示",itrans_master.sqlerrtext)
goto Error
end if
end if
hpb_1.position = 30
ls_dbpath = is_currentdirectory + '/data/'
//ls_sql = " CREATE DATABASE " + is_database + " ON (NAME = '" + is_database + "_data' , FILENAME = '" + ls_dbpath + is_database + "_data.mdf' , SIZE = 10, FILEGROWTH = 10%) LOG ON (NAME = '" + is_database + "_log' , FILENAME = '" + ls_dbpath + is_database + "_log.ldf' , SIZE = 5, FILEGROWTH = 10%) "
ls_sql = " CREATE DATABASE " + is_database
EXECUTE IMMEDIATE :ls_sql USING itrans_master ;
if itrans_master.sqlcode <> 0 then
messagebox("系統提示",itrans_master.sqlerrtext)
goto Error
end if
hpb_1.position = 60
//************* 檢測串連狀況 ************//
/*select spid into :li_count from sysprocesses where dbid=db_id(:is_database) using itrans_master;
if li_count > 0 then
//messagebox("系統提示",'資料庫有其他使用者串連,無法恢複。~r~n~r~n請在資料庫空閑時再重試。')
//goto Error
end if
*/
////////恢複資料庫
ls_sql = " RESTORE DATABASE " + is_database + " FROM DISK = '" + is_dbbakfile + "' WITH RECOVERY "
EXECUTE IMMEDIATE :ls_sql USING itrans_master ;
if itrans_master.sqlcode <> 0 then
messagebox("系統提示",itrans_master.sqlerrtext)
goto Error
end if
hpb_1.position = 90
ls_sql = "update " + is_database + "..gy_constant set zhi = '" + is_servermacadd + "' where mc = 'servermacadd'"
EXECUTE IMMEDIATE :ls_sql USING itrans_master ;
if itrans_master.sqlcode <> 0 then
messagebox("系統提示",itrans_master.sqlerrtext)
goto Error
end if
disconnect using itrans_master ;
destroy itrans_master
hpb_1.position = 100
timer(0)
SetPointer(Arrow!)
messagebox("系統提示","資料庫建立完成。")
return
Error:
disconnect using itrans_master ;
destroy itrans_master
hpb_1.position = 1
SetPointer(Arrow!)
timer(0)
return
三 備份資料庫
string ls_sql
//string ls_dbpath
if gf_check_sqlserver() <> 1 then return
if not wf_check_input() then return
SetPointer(HourGlass!)
timer(1)
// Profile master
itrans_master = create Transaction
itrans_master.DBMS = "MSS Microsoft SQL Server"
itrans_master.Database = 'master'
itrans_master.LogPass = is_logpass
itrans_master.ServerName = 'liuxj'
itrans_master.LogId = is_logid
itrans_master.AutoCommit = True
SQLCA.DBParm = ""
connect using itrans_master ;
if itrans_master.sqlcode <> 0 then
messagebox("系統提示",itrans_master.sqlerrtext)
goto Error
end if
hpb_1.position = 10
//************* 備份資料庫 *************//
ls_sql="BACKUP DATABASE " + is_database + " to disk='" + is_dbbakfile + "'"
EXECUTE IMMEDIATE :ls_sql USING itrans_master ;
if itrans_master.sqlcode <> 0 then
messagebox("備份資料庫錯誤:",itrans_master.sqlerrtext)
goto Error
end if
hpb_1.position = 90
disconnect using itrans_master ;
destroy itrans_master
is_DBBackPath = left(is_dbbakfile,lastpos(is_dbbakfile,'/'))
hpb_1.position = 100
SetPointer(Arrow!)
timer(0)
messagebox("系統提示",space(20) + "Database Backup完畢。~r~n~r~n備份到下面檔案: ~r~n" + is_dbbakfile)
return
Error:
disconnect using itrans_master ;
destroy itrans_master
hpb_1.position = 1
timer(0)
SetPointer(Arrow!)
return
四 恢複資料庫
integer li_count
string ls_sql
string ls_dbpath
if gf_check_sqlserver() <> 1 then return
if not wf_check_input() then return
SetPointer(HourGlass!)
timer(1)
// Profile master
itrans_master = create Transaction
itrans_master.DBMS = "MSS Microsoft SQL Server"
itrans_master.Database = 'master'
itrans_master.LogPass = is_logpass
itrans_master.ServerName = is_servername
itrans_master.LogId = is_logid
itrans_master.AutoCommit = True
SQLCA.DBParm = ""
connect using itrans_master ;
if itrans_master.sqlcode <> 0 then
messagebox("系統提示",itrans_master.sqlerrtext)
goto Error
end if
hpb_1.position = 10
//************* 檢測串連狀況 ************//
select spid into :li_count from sysprocesses where dbid=db_id(:is_database) using itrans_master;
if li_count > 0 then
messagebox("系統提示",'資料庫有其他使用者串連,無法恢複。~r~n~r~n請在資料庫空閑時再重試。')
goto Error
end if
//************* 恢複資料庫 *************//
ls_sql = " RESTORE DATABASE " + is_database + " FROM DISK = '" + is_dbbakfile + "' WITH RECOVERY "
EXECUTE IMMEDIATE :ls_sql USING itrans_master ;
if itrans_master.sqlcode <> 0 then
messagebox("恢複資料庫錯誤:",itrans_master.sqlerrtext)
goto Error
end if
hpb_1.position = 90
disconnect using itrans_master ;
destroy itrans_master
hpb_1.position = 100
SetPointer(Arrow!)
timer(0)
messagebox("系統提示","資料庫恢複完畢。")
return
Error:
disconnect using itrans_master ;
destroy itrans_master
hpb_1.position = 1
SetPointer(Arrow!)
timer(0)
return