Recently received a task--migrating the database
The database to be migrated is the SQL2005 database, and two applications are communicating with this database. Because the client application's connection to the database is directly written to the program in absolute, so this migration needs to modify the client application at the same time, considering the company address to change in the near future, to reconfigure the server, must also repair the client code, so I intend to use the template way, Modify the application to a dynamically connected database, then the subsequent migration data will not need to modify the application's code, only the configuration file needs to be modified.
Idea: Add a configuration file setup.ini, fixed Setup.ini data format, write a template to read Setup.ini data, extract the server name, user name, password, database name and other information, by modifying the INI file to achieve the purpose of connecting different servers:
Ini.bas's Code:
'==========================================================================='Usage:'1. Establish Setup.ini in the directory where the program is located'2. Add the following information in the INI file:'[Setup Information]'Server = Servers name'UserName = user name'Password = password'data = Database'3. Project reference Microsoft Axtivex Data Objects 2.6 Library'4. Modify Form.show in Main in Ini.bas'5. Add the following code to the top of the form that needs to connect to the database:'Option Explicit'Dim Conn as New ADODB. Connection'Dim Rs as New ADODB. Recordset'6. Connect to the database:'Conn.Open "Driver={sql server};server=" + trim (Server) + "uid=" + trim (User) + ";p wd=" + trim (Password) + ";d atabase= "+ Trim (Data) +" "'rs.open "select * from Table Min", Conn, adOpenKeyset, adLockOptimistic'7. Exit the database connection:'Rs.close'Conn.close'Set Rs = Nothing'Set Conn = Nothing'==========================================================================='to save the string that executes the SQL statement'Public sqlstmt as String'declaring API functions to write INI files Public Declare FunctionWritePrivateProfileStringLib "kernel32" Alias "Writeprivateprofilestringa"(ByValLpapplicationname as String,ByValLpkeyname as String,ByValLpstring asAny,ByValLpfilenchame as String) as Long Public Declare FunctionWriteprivateprofilesectionLib "kernel32" Alias "Writeprivateprofilesectiona"(ByValLpappname as String,ByValLpstring as String,ByValLpfilenchame as String) as Long'defining Server Parameter Constants PublicServer as String PublicUser as String PublicPassword as String PublicData as String'Program Entry pointSubMain ()'read the name of the server from the Setup.iniServer = GetKey (App.Path +"\setup.ini","Server") User= GetKey (App.Path +"\setup.ini","User") Password= GetKey (App.Path +"\setup.ini","Password") Data= GetKey (App.Path +"\setup.ini","Data") 'if the read is unsuccessful, exit IfServer ="" Then MsgBox "setup.ini file parameter errors! ", ,"Warning" End If 'Display System main interfaceForm1.showEnd Sub'determine if a file existsFunctionFileExist (Fname as String) as Boolean onLocalError Resume NextFileExist= (Dir(Fname) <>"")End Function'reading the data entry value of the INI file Public FunctionGetKey (tmp_file as String, Tmp_key as String) as String DimFile as Long 'Assigning file handlesFile =FreeFile 'Create a default Setup.ini file if the file does not exist IfFileExist (Tmp_file) =False ThenGetKey="" PagerWritePrivateProfileString ("Setup Information","Server","", App.Path +"\setup.ini") PagerWritePrivateProfileString ("Setup Information","UserName"," ", App.Path +"\setup.ini") PagerWritePrivateProfileString ("Setup Information","Password"," ", App.Path +"\setup.ini") PagerWritePrivateProfileString ("Setup Information","Data"," ", App.Path +"\setup.ini") Exit Function End If 'reading data Item valuesOpen Tmp_file for Input asFile Do while not EOF(1) LineInput#File, BufferIf Left(Buffer,Len(Tmp_key)) = Tmp_key ThenPOS=InStr(Buffer,"=") GetKey=Trim(Mid(Buffer, POS +1)) End If LoopClose FileEnd Function
The above code is tested in the WIN7+VB6+SQL2005 environment.
Summary: don't nail the program to the old place-28th in 97 things that programmers should know