sql = "insert into data (username,usermail,indata) values('"&username&"','"&usermail&"','"&indate&"')"
conn.Execute(sql)
conn.Close
Set conn = Nothing
說明:建立數(shù)據(jù)庫連接;通過表單獲取姓名、E-mail 字符串,Now()獲取當(dāng)前時間日期;使用 insert into 語句添加新記錄;conn.Execute 來執(zhí)行;最后關(guān)閉。
四、選擇數(shù)據(jù)庫里的記錄
1.選擇所有記錄的字段(按記錄倒序排序):sql = "select * from data order by ID desc"
2.選擇所有記錄的姓名和E-mail字段(不排序):sql = "select username,usermail from data"
3.選擇姓名為“風(fēng)云突變”的所有記錄:sql = "select * from data where username='"風(fēng)云突變"'"
4.選擇使用 163 信箱的所有記錄(按查看次數(shù)排序):sql = "select * from data where usermail like '%"@163.com"%' order by view desc"
5.選擇最新的10個記錄:sql = "select top 10 * from data order by ID desc"
sql = "update data set usermail='"&usermail&"' where ID="&CInt(ID)
conn.Execute(sql)
conn.Close
Set conn = Nothing
說明:建立數(shù)據(jù)庫連接;獲取記錄ID、新 E-mail 字符串;使用 update 語句修改記錄;conn.Execute 來執(zhí)行;最后關(guān)閉。
如果使記錄的查看值加1,則:sql = "update data set view=view+1 where ID="&CInt(ID)
六、刪除數(shù)據(jù)庫記錄
刪除某一條記錄:
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq="&Server.MapPath("data.mdb")
ID = 1
sql = "delete from data where ID="&CInt(ID)
conn.Execute(sql)