' The following is a SQL server2000 user function added to the ASP, and to create a database, give him the dbo permissions ' **************** NOTE: SQL Server is not validated in a Windows-only way, ' *********** Allow remote SQL Server connection ' **************** This function has passed the test **************************** ' What's the problem? Welcome to communicate with me, and then introduce some management procedures for SQL Server
' Parameters: Strloginname: New login name, strpwd: login password, strdbname: New database name ' function local variable description: strserver: Server's machine name (local locally), Struid:sql administrator, ' Strsapwd:sql the administrator password. The above three variables should be set according to your situation
' This function primarily invokes the system stored procedure to implement the
' Note: This function does not have fault-tolerant processing, if there is an error, you can determine that there is a problem with your SQL Server settings, or that the login account or the database ' call Addusertomssql ' ("Testlogin", "Iamhere", "Db_test") )
Sub addusertomssql (strloginname,strpwd,strdbname) ' defines server variables and system administrator login information, modified as appropriate
Dim strserver,struid,strsapwd Strserver= "(local)" struid= "sa" strsapwd= "Dim Conn" Database Connection
Dim strdsn ' Database connection string
Dim strcmd ' command string
strdsn= "Driver={sql server};server=" &StrServer& "uid=" &StrUid& ";p wd=" &StrSaPwd& "; Database=master "' establishes the connection to the database master set Conn = Server.CreateObject (" ADODB. Connection ") Conn.Open strDSN
' New database strcmd= ' Create DB ' &strdbname Conn.execute (strcmd) ' Create a new login account strcmd= ' sp_addlogin ' "&strloginname" & "', '" &StrPwd& "', '" &StrDBName& "'" Conn.execute (strcmd) conn.close
' Establish a connection to the new database and assign the new login account the right to access the new database strdsn= ' Driver={sql server}; Server= "&StrServer&"; uid= "&StrUid&";
Pwd= "&StSarPwd&";d atabase= "&strdbname strcmd=" sp_grantdbaccess "" &StrLoginName& "" Conn.Open strDSN Conn.execute (Strcmd)
' Make the new login account the owner of a new database strcmd= ' sp_addrolemember ' db_owner ', ' &StrLoginName& ' ' Conn.execute (strcmd) ' Turn off release connection Conn.Close Set Conn=Nothing Response.Write ' user "&StrLoginName&" successfully established!, and has set up a database for him "&StrDBName&"! end sub