1.如何使用access
兩年前用過一點(diǎn)access,感覺它小問題很多,就沒有再用。這次的程序是放在千兆寬網(wǎng)的虛擬主機(jī)上,這個(gè)虛擬主機(jī)不支持sqlite,讀取數(shù)據(jù)的時(shí)候是好的,只要往數(shù)據(jù)庫(kù)里邊寫東西,就會(huì)報(bào)錯(cuò),some disk i/o error occured。很神奇,換了一個(gè)虛擬主機(jī)試試,沒有問題。說明不是我程序的問題。后來只能換成access。用ado.net讀取數(shù)據(jù)庫(kù)其實(shí)都差不多,主要就是一個(gè)連接字串的問題,還有就是一些數(shù)據(jù)庫(kù)差異要注意。
1.1 數(shù)據(jù)庫(kù)連接串
<add name=connectionstring connectionstring=data source=|datadirectory|\we.mdb;provider=microsoft.jet.oledb.4.0 /> 連接串很簡(jiǎn)單,只需要指定datasource就可以,這里的|datadirectory|是指的app_data目錄。asp.net的這種方式可以使我們很方便的用相對(duì)路徑來指定數(shù)據(jù)庫(kù)文件的位置。這里的provider采用oledb驅(qū)動(dòng)。
1.2 使用
在程序中使用是很簡(jiǎn)單的,只是把connection,command之前的前綴換掉就可以了。舉一個(gè)例子:
代碼如下:
public datatable getall(string num,int min,int startrecord, int pagesize)
{
string sql = string.format(select num ,minprice ,isused from phonenumber where num like '{0}%' and isused=0, num);
if (min != 0)
{
sql += and minprice=@p1;
}
using (oledbconnection conn = new oledbconnection(sqlhelper.connstr))
{
conn.open();
oledbcommand cmd = conn.createcommand();
cmd.commandtext = sql;
if (min != 0)
cmd.parameters.addwithvalue(p1, min);
oledbdataadapter adp = new oledbdataadapter(cmd);
datatable table = new datatable();
adp.fill(startrecord,pagesize,table);
return table;
}
}
當(dāng)然還要添加using:using system.data.oledb;
using system.data;ado.net的處理方式都是很類似的,事實(shí)上,ado.net有一套以db為前綴的connection,command等類,這些具體的類都是繼承自dbconnection,所以看起來都是一樣的。
1.3 差異
上面提到,access是很詭異的。以下列舉一些我遇到的:
1.3.1 user 是關(guān)鍵字,如果有表名或者列名是user而沒有加中括號(hào),是會(huì)出錯(cuò)的。當(dāng)然,一致地在所有的表名和列名外加中括號(hào)是一個(gè)良好的編程習(xí)慣。
1.3.2 直接插datetime型的數(shù)據(jù)是會(huì)報(bào)錯(cuò)的,即使數(shù)據(jù)庫(kù)里的字段類型確實(shí)是date,插入的方式是把c#的datetime型tostring()過后再插入。
1.3.3 沒有bool型,或者bit型,叫yesno……
1.3.4一個(gè)命令里邊不支持多條sql。這個(gè)限制也很討厭,每次執(zhí)行一個(gè)command的時(shí)候里面只能包含一條sql,非常不方便,即使是小巧如sqlite的數(shù)據(jù)庫(kù)也沒有這個(gè)限制。
1.3.5 參數(shù)順序的問題. 聲明的參數(shù)順序必須要和你往command里邊添加參數(shù)的聲明一致.否則很有可能什么錯(cuò)也不報(bào),就是不影響結(jié)果(update的時(shí)候,其他時(shí)候沒試過). access真是極品數(shù)據(jù)庫(kù)?。?!例如
復(fù)制代碼 代碼如下:
string sql = update [user] set workfield=@p1, company=@p3,ic=@p4,contact=@p5,phone=@p6,mobile=@p7,address=@p8,email=@p9,introduction=@p10
+ where username=@p2;
cmd.commandtext = sql;
cmd.parameters.addwithvalue(p1, entity.workfield);
cmd.parameters.addwithvalue(p3, entity.company);
cmd.parameters.addwithvalue(p4, entity.ic);
cmd.parameters.addwithvalue(p5, entity.contact);
cmd.parameters.addwithvalue(p6, entity.phone);
cmd.parameters.addwithvalue(p7, entity.mobile);
cmd.parameters.addwithvalue(p8,);
cmd.parameters.addwithvalue(p9, string.isnullorempty(entity.email)? :entity.email);
cmd.parameters.addwithvalue(p10, string.isnullorempty(entity.introduction)? :entity.introduction);
cmd.parameters.addwithvalue(p2, entity.username);
int i= cmd.executenonquery();這樣是可以的,但是把
cmd.parameters.addwithvalue(p2, entity.username);
提到前面去,就不行了,更新不了. 幸好在csdn上搜到有人也問這個(gè)問題,不然都不知道怎么搞。
2.z-index的問題。
包含在其他html元素內(nèi)部的html元素的z-index只是相對(duì)于位于同一層次的元素的z-index,對(duì)其父元素之外的元素的z-index沒有影響,也就是說如果父元素的z-index很小,比如是0,內(nèi)部元素的z-index很高,1000.父元素相鄰的元素的z-index是2,如果內(nèi)部元素溢出去了,和相鄰元素重合,那么被覆蓋的還是內(nèi)部元素。
更多信息請(qǐng)查看IT技術(shù)專欄