欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Access 使用总结一篇

程序员文章站 2022-03-09 08:05:30
1.如何使用access 两年前用过一点access,感觉它小问题很多,就没有再用。这次的程序是放在千兆宽网的虚拟主机上,这个虚拟主机不支持sqlite,读取数据的时候是好的,只要往数据库里边写东西,...
1.如何使用access
两年前用过一点access,感觉它小问题很多,就没有再用。这次的程序是放在千兆宽网的虚拟主机上,这个虚拟主机不支持sqlite,读取数据的时候是好的,只要往数据库里边写东西,就会报错,some disk i/o error occured。很神奇,换了一个虚拟主机试试,没有问题。说明不是我程序的问题。后来只能换成access。用ado.net读取数据库其实都差不多,主要就是一个连接字串的问题,还有就是一些数据库差异要注意。

1.1 数据库连接串

<add name="connectionstring" connectionstring="data source=|datadirectory|\we.mdb;provider=microsoft.jet.oledb.4.0" /> 连接串很简单,只需要指定datasource就可以,这里的|datadirectory|是指的app_data目录。asp.net的这种方式可以使我们很方便的用相对路径来指定数据库文件的位置。这里的provider采用oledb驱动。

1.2 使用

在程序中使用是很简单的,只是把connection,command之前的前缀换掉就可以了。举一个例子:
复制代码 代码如下:

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;
}
}

当然还要添加using:using system.data.oledb;
using system.data;ado.net的处理方式都是很类似的,事实上,ado.net有一套以db为前缀的connection,command等类,这些具体的类都是继承自dbconnection,所以看起来都是一样的。

1.3 差异

上面提到,access是很诡异的。以下列举一些我遇到的:

1.3.1 user 是关键字,如果有表名或者列名是user而没有加中括号,是会出错的。当然,一致地在所有的表名和列名外加中括号是一个良好的编程习惯。

1.3.2 直接插datetime型的数据是会报错的,即使数据库里的字段类型确实是date,插入的方式是把c#的datetime型tostring()过后再插入。

1.3.3 没有bool型,或者bit型,叫yesno……

1.3.4一个命令里边不支持多条sql。这个限制也很讨厌,每次执行一个command的时候里面只能包含一条sql,非常不方便,即使是小巧如sqlite的数据库也没有这个限制。

1.3.5 参数顺序的问题. 声明的参数顺序必须要和你往command里边添加参数的声明一致.否则很有可能什么错也不报,就是不影响结果(update的时候,其他时候没试过). access真是*数据库啊!!例如
复制代码 代码如下:

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上搜到有人也问这个问题,不然都不知道怎么搞。

2.z-index的问题。
包含在其他html元素内部的html元素的z-index只是相对于位于同一层次的元素的z-index,对其父元素之外的元素的z-index没有影响,也就是说如果父元素的z-index很小,比如是0,内部元素的z-index很高,1000.父元素相邻的元素的z-index是2,如果内部元素溢出去了,和相邻元素重合,那么被覆盖的还是内部元素。
相关标签: Access 使用总结