If you think that this site is helpful, please recommend your friends to visit our site.
How to read, write, delete records in database?
The following is the code to read, write, delete records in db:
1. How to read records from database?
-------------------------------------------------------------------------------- < % dim nameout Set cn = Server.CreateObject("ADODB.Connection") cn.Open "ASP test" Set rs = Server.CreateObject("ADODB.RecordSet") MySQL = "SELECT * FROM Info" rs.CursorType = adOpenStatic 'rs.LockType = adLockOptimistic rs.ActiveConnection = cn rs.Source = MySQL rs.Open Do while not rs.eof Name = rs("Name") response.write Name rs.MoveNext loop rs.Close cn.Close Set cn = Nothing % > -------------------------------------------------------------------------------- 2. How to write / add records to db? -------------------------------------------------------------------------------- < % dim fYes, fNo, MySQL, strCnn, eNo, eCode, eName eNo = "12345678" eCode = "1" eName = "5" Set cn = Server.CreateObject("ADODB.Connection") cn.Open "Provider=sqloledb;" & "Data Source=rayxu; Initial _ Catalog=CWR;User Id=sa;Password=; " MySQL="insert into _ Employer (EmployerAccountNo, EmployerTypeCode, EmployerName)_ values ('" & eNo & "', '" & eCode & "', '" & eName & "')" Set rs = cn.Execute(MySQL) 'rs.Close cn.Close Set cn = Nothing response.write "Log in successful" % > -------------------------------------------------------------------------------- 3. How to delete record in db? -------------------------------------------------------------------------------- < % Dim RS, MyConn, SQL, SQL2 Set MyConn=Server.CreateObject("ADODB.Connection") MyConn.Open "demo" SQL = "DELETE FROM tblCustomer WHERE ID IN("&request.form("toDelete")&")" 'grab the value(s) selected from the checkboxes SQL2 = "DELETE FROM tblUser WHERE ID IN("&request.form("toDelete")&")" Set RS = MyConn.Execute(SQL) Set RS = MyConn.Execute(SQL2) MyConn.Close Set MyConn = Nothing % >
No comments:
Post a Comment