ifixVB連接數(shù)據(jù)庫,數(shù)據(jù)庫數(shù)據(jù)處理附帶代碼參考_第1頁
ifixVB連接數(shù)據(jù)庫,數(shù)據(jù)庫數(shù)據(jù)處理附帶代碼參考_第2頁
ifixVB連接數(shù)據(jù)庫,數(shù)據(jù)庫數(shù)據(jù)處理附帶代碼參考_第3頁
ifixVB連接數(shù)據(jù)庫,數(shù)據(jù)庫數(shù)據(jù)處理附帶代碼參考_第4頁
ifixVB連接數(shù)據(jù)庫,數(shù)據(jù)庫數(shù)據(jù)處理附帶代碼參考_第5頁
已閱讀5頁,還剩77頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、一,在 ifix 幫組手冊(cè)中的參考內(nèi)容 .1二, 在 vb 中需要引用內(nèi)容 .6三,采用 2 中引用的 ADO ext 方式連接用戶數(shù)據(jù)庫參考.6四,具體的參考代碼.84.1 創(chuàng)建新表.84.2 刪除不需要時(shí)間范圍數(shù)據(jù).94.3 刪除某個(gè)表.104.4 清除表中重復(fù)數(shù)據(jù).114.5 將臨時(shí)數(shù)據(jù)表中數(shù)據(jù)倒回主數(shù)據(jù)表.154.6 刪除某個(gè)表中全部數(shù)據(jù).174.7 讀一個(gè)數(shù)據(jù)庫中數(shù)據(jù),將該數(shù)據(jù)寫入另一個(gè)數(shù)據(jù)庫.18附件 1:網(wǎng)絡(luò)下載參考數(shù)據(jù) .25附錄 2:sql 參考 .36附錄 3:附帶一個(gè)小資料 sql 參數(shù) .40SQL 常用命令使用方法.43參考 1:日表代碼 .50參考 2:污水月表代碼

2、 .55參考 3:?jiǎn)文瓯泶a .63參考 4:多年表代碼 .69參考 5:污水設(shè)備運(yùn)行時(shí)間表代碼 .75參考 6:刪除某表指定時(shí)間前數(shù)據(jù) .81一,在 ifix 幫組手冊(cè)中的參考內(nèi)容Driver=Microsoft Access Driver (*.MDB);DBQ=E:報(bào)表gyws_report.mdb;INSERT INTO fixreport + _(datatime, datatag, datavalue) + _VALUES( 2010-7-13 10:11:23, addbc, 124.45)二, 在 vb 中需要引用內(nèi)容三,采用 2 中引用的 ADO ext 方式連接用戶數(shù)據(jù)庫參

3、考四,具體的參考代碼4.1 創(chuàng)建新表Private Sub close_pic16_Click()+再已存在庫中創(chuàng)建表+Dim cat As ADOX.CatalogDim tbl As New ADOX.TableSet cat = New ADOX.Catalog cat.ActiveConnection = Provider=Microsoft.Jet.OLEDB.4.0; & _ Data Source=E:污水報(bào)表gyws_report.mdb; With tbl .Name = fixreport_temp Create a field and append it to t

4、he new Table object. This must be done before appending the Table object to the Tables collection of the Catalog. .Columns.Append datatime, adDate .Columns.Append datatag, adVarWChar .Columns.Append datavalue, adDouble .Columns.Append datatxt, adVarWChar End With Add the new table to the database. c

5、at.Tables.Append tbl Clean up the references Set cat = Nothing Set tbl = Nothing End Sub4.2 刪除不需要時(shí)間范圍數(shù)據(jù)、Private Sub close_pic14_Click()Dim conADO As ADODB.ConnectionDim rsADO As ADODB.Recordset Dim strQuery1 As String Dim StartTime報(bào)表設(shè)計(jì).datestar.RefreshStartTime = Format(報(bào)表設(shè)計(jì).datestar.Value, yyyy-mm-

6、dd) & & Format(報(bào)表設(shè)計(jì).timestar.Value, hh:mm:ss)Me.timestar1.Value = StartTimeDim Data, Data1, Data2, Datasm, Tagsm(1) Set conADO = New ADODB.Connection conADO.ConnectionString = Driver=Microsoft Access Driver (*.MDB);DBQ=E:污水報(bào)表gyws_report.mdb;UID=aa;PWD=; strQuery1 = delete + _FROM fixreport +

7、 _ WHERE (datatime 1)and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )1)+以下內(nèi)容為從 fix 系統(tǒng)數(shù)據(jù)讀出需要數(shù)據(jù)+datatime not in (SELECT DISTINCT count(datatime) FROM fixreport group by datatime having DISTINCT count datatime = 1) and + _Tag = AA1Tag(10) = AA1fixreport.datatime

8、 DISTINCT fixreport. having count(datatime) 1) delete strQueryfix = SELECT fixreport.datatime,fixreport.datatag,fixreport.datavalue,fixreport.datatxt FROM fixreport + _ WHERE fixreport.datatag = AA1 DISTINCT strQuery = SELECT sum(datavalue) from (SELECT DISTINCT datatime,datatag,datavalue,datatxt FR

9、OM fixreport + _ WHERE datatag = AA1 )datatime not in (SELECT datatime FROM fixreport group by datatime having count( DISTINCT datatime) ) and + _datatag = AA1 Set conADO = New ADODB.Connection conADO.ConnectionString = Driver=Microsoft Access Driver (*.MDB);DBQ=E:污水報(bào)表gyws_report.mdb;UID=aa;PWD=; co

10、nADO.Open Set rsADO = New ADODB.Recordset rsADO.Open strQuery, conADO, adOpenDynamic, adLockOptimistic, adCmdText i = rsADO(0) j = i + 1 k = i + 2 End SubPrivate Sub close_pic27_Click()Dim conADO As ADODB.ConnectionDim rsADO As ADODB.RecordsetDim strQuery As String+fix 數(shù)據(jù)連接定義+Dim conADOfix As Connec

11、tionDim rsADOfix As RecordsetDim strQueryfix As String+fix 數(shù)據(jù)處理定義+On Error GoTo ErrorHandler+自建數(shù)據(jù)庫數(shù)據(jù)連接定義+Dim StartTime, StartTime1, EenTime, EenTime1 As StringDim Tag(15) As StringDim i, j, kDim Datat, Datatag, Datav, Datasm, Tagsm(1)Dim smtime, smm As String+以下內(nèi)容為從 fix 系統(tǒng)數(shù)據(jù)讀出需要數(shù)據(jù)+-時(shí)間控件取值-StartTime1

12、 = dateadd(d, -1, Now)StartTime = Format(StartTime1, yyyy-mm-dd) & & Format(00:00:01, hh:mm:ss)EndTime1 = dateadd(h, 23, StartTime)EndTime = Format(EndTime1, yyyy-mm-dd) & & Format(23:00:01, hh:mm:ss) strQuery = SELECT datavalue FROM (SELECT DISTINCT datatime,datatag,datavalue,datatx

13、t FROM fixreport + _ WHERE datatag = COD211 + _ AND (datatime =ts + StartTime + AND + _ datatime =ts + StartTime + AND + _ DATETIME =ts 2010-06-14 00:00:01 AND + _ DATETIME =ts 2010-06-14 23:00:01) + _ ORDER BY DATETIME 2010-06-14 23:00:00 Set conADOfix(i) = New ADODB.Connection conADOfix(i).Connect

14、ionString = DSN=FIX Dynamics Historical Data;UID=sa;PWD=; conADOfix(i).Open Set rsADOfix(i) = New ADODB.Recordset rsADOfix(i).Open strQueryfix(i), conADOfix(i), adOpenForwardOnly, adLockReadOnly, adCmdText Next i+以下內(nèi)容為向數(shù)據(jù)庫中插入數(shù)據(jù)+Datasm = 當(dāng)前插入數(shù)據(jù)為時(shí)間,本數(shù)據(jù)為 & Data2 & 分!Data1 = CStr(Data) Set conAD

15、O = New ADODB.Connection conADO.ConnectionString = Driver=Microsoft Access Driver (*.MDB);DBQ=E:污水報(bào)表gyws_report.mdb;UID=aa;PWD=;conADO.OpenstrQuery = INSERT INTO fixreport + _(datatime, datatag, datavalue,datatxt) + _VALUES( + StartTime + , + Data2 + , + Data1 + , + Datasm + )Set rsADO = New ADODB.R

16、ecordset rsADO.Open strQuery, conADO, adOpenForwardOnly, adLockReadOnly, adCmdText+以下內(nèi)容為將讀出 fix 數(shù)據(jù)寫入數(shù)據(jù)庫 fixreport+Dim Datat, Datatag, Datav, Datasm, Tagsm(1)Dim jDim smtime, smm As StringFor j = 0 To 9Set conADO(j) = New ADODB.Connection conADO(j).ConnectionString = Driver=Microsoft Access Driver (*

17、.MDB);DBQ=22新建文件夾gyws_report.mdb;UID=aa;PWD=;conADO(j).OpenWhile rsADOfix(j).EOF TrueDatasm = 當(dāng)前插入數(shù)據(jù)為時(shí)間,本數(shù)據(jù)為 & Data2 & 分! CStr If rsADO(c) Then smtime = rsADOfix(j)(0) smm = Minute(smtime) Datasm = 當(dāng)前入數(shù)據(jù)為地 & 次 & smm & 分時(shí)的數(shù)值! Datat = CStr(rsADOfix(j)(0) Datatag = CStr(r

18、sADOfix(j)(2) Datav = CStr(rsADOfix(j)(1) strQuery(j) = INSERT INTO fixreport + _(datatime, datatag, datavalue,datatxt) + _VALUES( + Datat + , + Datatag + , + Datav + , + Datasm + ) If rsADO(c) Then .Cells(r, c + l + 1).Value = rsADO(c) ElseIf rsADO(c) = 0 Then Intyexcel.Quit If CStr(rsADO(c) Like !

19、1234567890 Then Intyexcel.Quit _ Else .Cells(r, c + l + 1).Value = rsADO(c) Set rsADO(j) = New ADODB.Recordset rsADO(j).Open strQuery(j), conADO(j), adOpenForwardOnly, adLockReadOnly, adCmdText rsADOfix(j).MoveNext Wend Next j End Sub+s 刪除表+Dim conADO As ADODB.ConnectionDim rsADO As ADODB.RecordsetD

20、im strQuery As StringDim Datat, Datatag, Datav, Datasm, Tagsm(1)Dim jDim smtime, smm As StringSet conADO = New ADODB.Connection conADO.ConnectionString = Driver=Microsoft Access Driver (*.MDB);DBQ=E:污水報(bào)表gyws_report.mdb;UID=aa;PWD=;conADO.Open strQuery = DROP TABLE fixreport_temp Set rsADO = New ADOD

21、B.Recordset rsADO.Open strQuery, conADO, adOpenForwardOnly, adLockOptimistic, adCmdText +再已存在庫中創(chuàng)建表+Dim cat As ADOX.CatalogDim tbl As New ADOX.TableSet cat = New ADOX.Catalog cat.ActiveConnection = Provider=Microsoft.Jet.OLEDB.4.0; & _ Data Source=E:污水報(bào)表gyws_report.mdb; With tbl .Name = fixreport

22、_temp Create a field and append it to the new Table object. This must be done before appending the Table object to the Tables collection of the Catalog. .Columns.Append datatime, adDate .Columns.Append datatag, adVarWChar .Columns.Append datavalue, adDouble .Columns.Append datatxt, adVarWChar End Wi

23、th Add the new table to the database. cat.Tables.Append tbl Clean up the references Set cat = Nothing Set tbl = Nothing 附件 1:網(wǎng)絡(luò)下載參考數(shù)據(jù)刪除 Access 中的重復(fù)記錄,用 sql 如何寫PL-SQL 2007-12-13 13:52:32 閱讀 200 評(píng)論 0 字號(hào):大中小 1、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷select * from peoplewhere peopleId in (select peopleId fr

24、om people group by peopleId having count(peopleId) 1)2、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷,只留有 rowid 最小的記錄delete from peoplewhere peopleId in (select peopleId from people group by peopleId having count(peopleId) 1)and rowid not in (select min(rowid) from people group by peopleId having count(people

25、Id )1)3、查找表中多余的重復(fù)記錄(多個(gè)字段)select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) 1)4、刪除表中多余的重復(fù)記錄(多個(gè)字段) ,只留有 rowid 最小的記錄delete from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) 1

26、)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)1)5、查找表中多余的重復(fù)記錄(多個(gè)字段) ,不包含 rowid 最小的記錄select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) 1)and rowid not in (select min(rowid) from vitae group by

27、peopleId,seq having count(*)1)幾個(gè)刪除重復(fù)記錄的幾個(gè)刪除重復(fù)記錄的 SQL 語句語句來源:2007 年 11 月 29 日 09:53網(wǎng)友評(píng)論:0 條點(diǎn)擊: 5553比如現(xiàn)在有一人員表 (表名:peosons)若想將姓名、身份證號(hào)、住址這三個(gè)字段完全相同的記錄查詢出來 select p1.* from persons p1,persons p2 where p1.idp2.id and p1.cardid = p2.cardid and p1.pname = p2.pname and p1.address = p2.address 可以實(shí)現(xiàn)上述效果 幾個(gè)刪除重復(fù)記

28、錄的幾個(gè)刪除重復(fù)記錄的 SQL 語句語句 1.用 rowid 方法 2.用 group by 方法 3.用 distinct 方法 1。用。用 rowid 方法方法 據(jù)據(jù) oracle 帶的 rowid 屬性,進(jìn)行判斷,是否存在重復(fù),語句如下:查數(shù)據(jù)查數(shù)據(jù): select * from table1 a where rowid !=(select max(rowid) from table1 b where 1=1 and 2=2.)刪數(shù)據(jù):刪數(shù)據(jù): delete from table1 a where rowid !=(select max(r

29、owid) from table1 b where 1=1 and 2=2.) 2.group by 方法方法 查數(shù)據(jù)查數(shù)據(jù):select count(num), max(name) from student -列出重復(fù)的記錄數(shù),并列出他的 name 屬性 group by num having count(num) 1 -按 num 分組后找出表中 num 列重復(fù),即出現(xiàn)次數(shù)大于一次 刪數(shù)據(jù):刪數(shù)據(jù):delete from student group by num having count(num) 1這樣的話就把所有重復(fù)的都刪除了。 3.用

30、distinct 方法 -對(duì)于小的表比較有用 create table table_new as select distinct * from table1 minux truncate table table1;insert into table1 select * from table_new;查詢及刪除重復(fù)記錄的方法大全查詢及刪除重復(fù)記錄的方法大全1、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷select * from peoplewhere peopleId in (select peopleId from people group by peopleId

31、 having count(peopleId) 1)2、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷,只留有 rowid 最小的記錄delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) 1)and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )1)3、查找表中多余的重復(fù)記錄(多個(gè)字段)

32、select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) 1)4、刪除表中多余的重復(fù)記錄(多個(gè)字段),只留有 rowid 最小的記錄 delete from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) 1)and rowid not in (select

33、 min(rowid) from vitae group by peopleId,seq having count(*)1)5、查找表中多余的重復(fù)記錄(多個(gè)字段),不包含 rowid 最小的記錄select * from vitae awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(

34、*)1)(二)比方說在 A 表中存在一個(gè)字段“name”,而且不同記錄之間的“name”值有可能會(huì)相同,現(xiàn)在就是需要查詢出在該表中的各記錄之間,“name”值存在重復(fù)的項(xiàng);Select Name,Count(*) From A Group By Name Having Count(*) 1如果還查性別也相同大則如下:Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) 1 (三)方法一declare max integer,id integerdeclare cur_rows cursor local for sel

35、ect 主字段,count(*) from 表名 group by 主字段 having count(*) ; 1open cur_rowsfetch cur_rows into id,maxwhile fetch_status=0beginselect max = max -1set rowcount maxdelete from 表名 where 主字段 = idfetch cur_rows into id,maxendclose cur_rowsset rowcount 0方法二重復(fù)記錄有兩個(gè)意義上的重復(fù)記錄,一是完全重復(fù)的記錄,也即所有字段均重復(fù)的記錄,二是部分關(guān)鍵字段重復(fù)的記錄,比如

36、 Name 字段重復(fù),而其他字段不一定重復(fù)或都重復(fù)可以忽略。 1、對(duì)于第一種重復(fù),比較容易解決,使用select distinct * from tableName就可以得到無重復(fù)記錄的結(jié)果集。如果該表需要?jiǎng)h除重復(fù)的記錄(重復(fù)記錄保留 1 條),可以按以下方法刪除select distinct * into #Tmp from tableNamedrop table tableNameselect * into tableName from #Tmpdrop table #Tmp發(fā)生這種重復(fù)的原因是表設(shè)計(jì)不周產(chǎn)生的,增加唯一索引列即可解決。2、這類重復(fù)問題通常要求保留重復(fù)記錄中的第一條記錄,操

37、作方法如下假設(shè)有重復(fù)的字段為 Name,Address,要求得到這兩個(gè)字段唯一的結(jié)果集select identity(int,1,1) as autoID, * into #Tmp from tableNameselect min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoIDselect * from #Tmp where autoID in(select autoID from #tmp2)最后一個(gè) select 即得到了 Name,Address 不重復(fù)的結(jié)果集(但多了一個(gè) autoID 字段,實(shí)際寫時(shí)可以寫在sel

38、ect 子句中省去此列) (四)查詢重復(fù)select * from tablename where id in (select id from tablename group by id having count(id) 1)快速刪除重復(fù)記錄,SQL Server 如何實(shí)現(xiàn)?如果一張表的數(shù)據(jù)達(dá)到上百萬條,用游標(biāo)的方法來刪除簡(jiǎn)直是個(gè)噩夢(mèng),因?yàn)樗鼤?huì)執(zhí)行相當(dāng)長(zhǎng)的一段時(shí)間開發(fā)人員的噩夢(mèng)開發(fā)人員的噩夢(mèng)刪除重復(fù)記錄刪除重復(fù)記錄想必每一位開發(fā)人員都有過類似的經(jīng)歷,在對(duì)數(shù)據(jù)庫進(jìn)行查詢或統(tǒng)計(jì)的時(shí)候不時(shí)地會(huì)碰到由于表中存在重復(fù)的記錄而導(dǎo)致查詢和統(tǒng)計(jì)結(jié)果不準(zhǔn)確。解決該問題的辦法就是將這些重復(fù)的記錄刪除,只保留其中的

39、一條。在 SQL Server 中除了對(duì)擁有十幾條記錄的表進(jìn)行人工刪除外,實(shí)現(xiàn)刪除重復(fù)記錄一般都是寫一段代碼,用游標(biāo)的方法一行一行檢查,刪除重復(fù)的記錄。因?yàn)檫@種方法需要對(duì)整個(gè)表進(jìn)行遍歷,所以對(duì)于表中的記錄數(shù)不是很大的時(shí)候還是可行的,如果一張表的數(shù)據(jù)達(dá)到上百萬條,用游標(biāo)的方法來刪除簡(jiǎn)直是個(gè)噩夢(mèng),因?yàn)樗鼤?huì)執(zhí)行相當(dāng)長(zhǎng)的一段時(shí)間。四板斧四板斧輕松消除重復(fù)記錄輕松消除重復(fù)記錄殊不知在 SQL Server 中有一種更為簡(jiǎn)單的方法,它不需要用游標(biāo),只要寫一句簡(jiǎn)單插入語句就能實(shí)現(xiàn)刪除重復(fù)記錄的功能。為了能清楚地表述,我們首先假設(shè)存在一個(gè)產(chǎn)品信息表 Products,其表結(jié)構(gòu)如下:CREATE TABLE P

40、roducts ( ProductID int, ProductName nvarchar (40), Unit char(2), UnitPrice money )表中的數(shù)據(jù)如圖 1:圖表圖 1 中可以看出,產(chǎn)品 Chang 和 Tofu 的記錄在產(chǎn)品信息表中存在重復(fù)?,F(xiàn)在要?jiǎng)h除這些重復(fù)的記錄,只保留其中的一條。步驟如下:第一板斧建立一張具有相同結(jié)構(gòu)的臨時(shí)表CREATE TABLE Products_temp ( ProductID int, ProductName nvarchar (40), Unit char(2), UnitPrice money )第二板斧為該表加上索引,并使其忽略

41、重復(fù)的值方法是在企業(yè)管理器中找到上面建立的臨時(shí)表 Products _temp,單擊鼠標(biāo)右鍵,選擇所有任務(wù),選擇管理索引,選擇新建。如圖 2 所示。按照?qǐng)D 2 中圈出來的地方設(shè)置索引選項(xiàng)。圖 2第三板斧拷貝產(chǎn)品信息到臨時(shí)表insert into Products_temp Select * from Products此時(shí) SQL Server 會(huì)返回如下提示:服務(wù)器: 消息 3604,級(jí)別 16,狀態(tài) 1,行 1已忽略重復(fù)的鍵。它表明在產(chǎn)品信息臨時(shí)表 Products_temp 中不會(huì)有重復(fù)的行出現(xiàn)。第四板斧將新的數(shù)據(jù)導(dǎo)入原表將原產(chǎn)品信息表 Products 清空,并將臨時(shí)表 Products

42、_temp 中數(shù)據(jù)導(dǎo)入,最后刪除臨時(shí)表 Products_temp。delete Products insert into Products select * from Products_temp drop table Products_temp這樣就完成了對(duì)表中重復(fù)記錄的刪除。無論表有多大,它的執(zhí)行速度都是相當(dāng)快的,而且因?yàn)閹缀醪挥脤懻Z句,所以它也是很安全的。小提示:上述方法中刪除重復(fù)記錄取決于創(chuàng)建唯一索引時(shí)選擇的字段,在實(shí)際的操作過程中讀者務(wù)必首先確認(rèn)創(chuàng)建的唯一索引字段是否正確,以免將有用的數(shù)據(jù)刪除。http:/ ADOX 來創(chuàng)建 Access 數(shù)據(jù)庫。 Microsoft ActiveX

43、 Data Objects Extensions for Data Definition Language and Security (ADOX) 是對(duì) ADO 對(duì)象和編程模型的擴(kuò)展。ADOX 包括用于模式創(chuàng)建和修改的對(duì)象,以及安全性。由于它是基于對(duì)象實(shí)現(xiàn)模式操作,所以用戶可以編寫對(duì)各種數(shù)據(jù)源都能有效運(yùn)行的代碼,而與它們?cè)颊Z法中的差異無關(guān)。 ADOX 是核心 ADO 對(duì)象的擴(kuò)展庫。它顯露的其他對(duì)象可用于創(chuàng)建、修改和刪除模式對(duì)象,如表格和過程。它還包括安全對(duì)象,可用于維護(hù)用戶和組,以及授予和撤消對(duì)象的權(quán)限。 要通過開發(fā)工具使用 ADOX,需要建立對(duì) ADOX 類型庫的引用。對(duì) ADOX 庫的說

44、明為“Microsoft ADO Ext. for DDL and Security.”。ADOX 庫文件名為“Msadox.dll”,程序 ID (ProgID) 為“ADOX”。有關(guān)建立庫引用的詳細(xì)信息,請(qǐng)參閱開發(fā)工具的文檔。 使用 ADOX 對(duì)象模型中的 Catalog 對(duì)象來創(chuàng)建數(shù)據(jù)庫,創(chuàng)建 Access 數(shù)據(jù)庫的方式非常簡(jiǎn)單,使用的字符串和ADODB.Connection 對(duì)象的連接字符串是一樣的。較為簡(jiǎn)單的示例如下:1、創(chuàng)建數(shù)據(jù)庫聲明 Catalog 對(duì)象Dim cat As New ADOX.Catalog設(shè)置創(chuàng)建數(shù)據(jù)庫用到的字符串,和 ADODB.Connection 對(duì)象的連

45、接字符串相同Dim Workstring As StringWorkstring = Provider=Microsoft.Jet.OLEDB.4.0; & Data Source= & E:test27.mdb創(chuàng)建數(shù)據(jù)庫cat.Create Workstringcat.ActiveConnection = Workstring聲明 Table 對(duì)象Dim tb1 As New ADOX.Tabletb1.Name = 表 1cat.Tables.Append tb12、下面的代碼將字段添加到表中,我們以一個(gè)單精度字段和一個(gè)文本字段為例。tb1.Columns.Append 成

46、績(jī), adSingletb1.Columns(成績(jī)).Properties(default).Value = 0 字段默認(rèn)值為 0要說明的是,設(shè)置默認(rèn)值和充許為空相沖突,所以這句可以和上句互換tb1.Columns(成績(jī)).Attributes = adColNullable tb1.Columns.Append 文本, adVarWChar, 50tb1.Columns(文本).Properties(default).Value = 5362以這種方式添加文本列的時(shí)候不能指定 Attributes 屬性為 adColNullable,希望能有人指點(diǎn)下3、較為完整的添加字段的方法,與第二節(jié)沒什

47、么區(qū)別Dim col1 As New ADOX.ColumnDim col2 As New ADOX.Columncol1.ParentCatalog = catcol1.Name = 成績(jī)col1.Type = adSinglecol1.Properties(default).Value = 0col1.Attributes = adColNullabletb1.Columns.Append col1col2.ParentCatalog = catcol2.Name = 文本col2.Type = adVarWCharcol2.DefinedSize = 50col2.Attributes

48、= adColNullablecol2.Properties(default).Value = 2222tb1.Columns.Append col2如果先添加列,tb1.Columns.Append col1,而后設(shè)置其屬性,可以不用 col1.ParentCatalog = cat 這一句4、在數(shù)據(jù)庫已經(jīng)存在的情況下,可以使用 SQL 語句創(chuàng)建表和字段,仍以添加一個(gè)單精度字段和一個(gè)文本字段為例,代碼如下:Dim conn As New ADODB.ConnectionDim Workstring As StringWorkstring = Provider=Microsoft.Jet.OL

49、EDB.4.0; & Data Source= & E:test.mdbconn.Open Workstringstrsql = CREATE TABLE test9(編號(hào) single default 0,姓名 text (50) not null default XXXX)conn.Execute strsqlSet conn = Nothing+網(wǎng)絡(luò)內(nèi)容未經(jīng)驗(yàn)證+Dim Path As StringDim Db As DatabaseDim SQL As StringPath = App.Path & (Time, hhmmss) & .mdb 文件名及文

50、件存放路徑If Dir(Path) = Then 判斷該路徑是否有文件 Set Db = Workspaces(0).CreateDatabase(Path, dbLangGeneral)創(chuàng)建新的文件 SQL = Create Table & TEST& ( & 單號(hào) text,備注 text); Db.Execute SQLEnd if附錄 2:sql 參考 1. ASP 與 Access 數(shù)據(jù)庫連接: 2. ASP 與 SQL 數(shù)據(jù)庫連接: 建立記錄集對(duì)象: set rs=server.createobject( adodb.recordset ) rs.open

51、SQL 語句,conn,3,2 3. SQL 常用命令使用方法: (1) 數(shù)據(jù)記錄篩選: sql= select * from 數(shù)據(jù)表 where 字段名=字段值 order by 字段名 desc sql= select * from 數(shù)據(jù)表 where 字段名 like %字段值% order by 字段名 desc sql= select top 10 * from 數(shù)據(jù)表 where 字段名 order by 字段名 desc sql= select * from 數(shù)據(jù)表 where 字段名 in ( 值 1 , 值 2 , 值 3 ) sql= select * from 數(shù)據(jù)表 w

52、here 字段名 between 值 1 and 值 2 (2) 更新數(shù)據(jù)記錄: sql= update 數(shù)據(jù)表 set 字段名=字段值 where 條件表達(dá)式 sql= update 數(shù)據(jù)表 set 字段 1=值 1,字段 2=值 2 字段 n=值 n where 條件表達(dá)式 (3) 刪除數(shù)據(jù)記錄: sql= delete from 數(shù)據(jù)表 where 條件表達(dá)式 sql= delete from 數(shù)據(jù)表 (將數(shù)據(jù)表所有記錄刪除) (4) 添加數(shù)據(jù)記錄: sql= insert into 數(shù)據(jù)表 (字段 1,字段 2,字段 3 ) values (值 1,值 2,值 3 ) sql= ins

53、ert into 目標(biāo)數(shù)據(jù)表 select * from 源數(shù)據(jù)表 (把源數(shù)據(jù)表的記錄添加到目標(biāo)數(shù)據(jù)表) (5) 數(shù)據(jù)記錄統(tǒng)計(jì)函數(shù): AVG(字段名) 得出一個(gè)表格欄平均值 COUNT(*¦字段名) 對(duì)數(shù)據(jù)行數(shù)的統(tǒng)計(jì)或?qū)δ骋粰谟兄档臄?shù)據(jù)行數(shù)統(tǒng)計(jì) MAX(字段名) 取得一個(gè)表格欄最大的值 MIN(字段名) 取得一個(gè)表格欄最小的值 SUM(字段名) 把數(shù)據(jù)欄的值相加 引用以上函數(shù)的方法: sql= select sum(字段名) as 別名 from 數(shù)據(jù)表 where 條件表達(dá)式 set rs=conn.excute(sql) 用 rs( 別名 ) 獲取統(tǒng)的計(jì)值,其它函數(shù)運(yùn)

54、用同上。 (5) 數(shù)據(jù)表的建立和刪除: CREATE TABLE 數(shù)據(jù)表名稱(字段 1 類型 1(長(zhǎng)度),字段 2 類型 2(長(zhǎng)度) ) 例:CREATE TABLE tab01(name varchar(50),datetime default now() DROP TABLE 數(shù)據(jù)表名稱 (永久性刪除一個(gè)數(shù)據(jù)表) 4. 記錄集對(duì)象的方法: rs.movenext 將記錄指針從當(dāng)前的位置向下移一行 rs.moveprevious 將記錄指針從當(dāng)前的位置向上移一行 rs.movefirst 將記錄指針移到數(shù)據(jù)表第一行 rs.movelast 將記錄指針移到數(shù)據(jù)表最后一行 rs.absolute

55、position=N 將記錄指針移到數(shù)據(jù)表第 N 行 rs.absolutepage=N 將記錄指針移到第 N 頁的第一行 rs.pagesize=N 設(shè)置每頁為 N 條記錄 rs.pagecount 根據(jù) pagesize 的設(shè)置返回總頁數(shù) rs.recordcount 返回記錄總數(shù) rs.bof 返回記錄指針是否超出數(shù)據(jù)表首端,true 表示是,false 為否 rs.eof 返回記錄指針是否超出數(shù)據(jù)表末端,true 表示是,false 為否 rs.delete 刪除當(dāng)前記錄,但記錄指針不會(huì)向下移動(dòng) rs.addnew 添加記錄到數(shù)據(jù)表末端 rs.update 更新數(shù)據(jù)表記錄 Record

56、set 對(duì)象方法 Open 方法 recordset.Open Source,ActiveConnection,CursorType,LockType,Options Source Recordset 對(duì)象可以通過 Source 屬性來連接 Command 對(duì)象。Source 參數(shù)可以是一個(gè) Command 對(duì)象名稱、一段 SQL 命令、一個(gè)指定的數(shù)據(jù)表名稱或是一個(gè) Stored Procedure。假如省略這個(gè)參數(shù),系統(tǒng)則采用 Recordset 對(duì)象的 Source 屬性。 ActiveConnection Recordset 對(duì)象可以通過 ActiveConnection 屬性來連接 C

57、onnection 對(duì)象。這里的 ActiveConnection 可以是一個(gè) Connection 對(duì)象或是一串包含數(shù)據(jù)庫連接信息(ConnectionString)的字符串參數(shù)。 CursorType Recordset 對(duì)象 Open 方法的 CursorType 參數(shù)表示將以什么樣的游標(biāo)類型啟動(dòng)數(shù)據(jù),包括 adOpenForwardOnly、adOpenKeyset、adOpenDynamic 及 adOpenStatic,分述如下: - 常數(shù) 常數(shù)值 說明 - adOpenForwardOnly 0 缺省值,啟動(dòng)一個(gè)只能向前移動(dòng)的游標(biāo)(Forward Only)。 adOpenKey

58、set 1 啟動(dòng)一個(gè) Keyset 類型的游標(biāo)。 adOpenDynamic 2 啟動(dòng)一個(gè) Dynamic 類型的游標(biāo)。 adOpenStatic 3 啟動(dòng)一個(gè) Static 類型的游標(biāo)。 - 以上幾個(gè)游標(biāo)類型將直接影響到 Recordset 對(duì)象所有的屬性和方法,以下列表說明他們之間的區(qū)別。 - Recordset 屬性 adOpenForwardOnly adOpenKeyset adOpenDynamic adOpenStatic - AbsolutePage 不支持 不支持 可讀寫 可讀寫 AbsolutePosition 不支持 不支持 可讀寫 可讀寫 ActiveConnectio

59、n 可讀寫 可讀寫 可讀寫 可讀寫 BOF 只讀 只讀 只讀 只讀 Bookmark 不支持 不支持 可讀寫 可讀寫 CacheSize 可讀寫 可讀寫 可讀寫 可讀寫 CursorLocation 可讀寫 可讀寫 可讀寫 可讀寫 CursorType 可讀寫 可讀寫 可讀寫 可讀寫 EditMode 只讀 只讀 只讀 只讀 EOF 只讀 只讀 只讀 只讀 Filter 可讀寫 可讀寫 可讀寫 可讀寫 LockType 可讀寫 可讀寫 可讀寫 可讀寫 MarshalOptions 可讀寫 可讀寫 可讀寫 可讀寫 MaxRecords 可讀寫 可讀寫 可讀寫 可讀寫 PageCount 不支持

60、不支持 只讀 只讀 PageSize 可讀寫 可讀寫 可讀寫 可讀寫 RecordCount 不支持 不支持 只讀 只讀 Source 可讀寫 可讀寫 可讀寫 可讀寫 State 只讀 只讀 只讀 只讀 Status 只讀 只讀 只讀 只讀 AddNew 支持 支持 支持 支持 CancelBatch 支持 支持 支持 支持 CancelUpdate 支持 支持 支持 支持 Clone 不支持 不支持 Close 支持 支持 支持 支持 Delete 支持 支持 支持 支持 GetRows 支持 支持 支持 支持 Move 不支持 支持 支持 支持 MoveFirst 支持 支持 支持 支持 MoveLast 不支持 支持 支持 支持 MoveNext 支持 支持 支持 支持 MovePrevious 不支持 支持 支持 支持 NextRecordset 支持 支持 支

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論