在try里写con.open()
con是链接aceess数据库的代码
看看是否执行
ACCESS是独享方式,只能连接一次。出错就是打开了。
根据平常的思维你应该在之前打开,然后判断它的状态。测试代码如下:1 连接数据库try{String commandText="select count(*) from users"SqlConnection conn = new SqlConnection()conn.ConnectionString = "Data Source=(local)InitialCatalog=classmateUser Id=saPassword=sa"SqlCommand cmd = new SqlCommand(commandText,conn)conn.Open()string count = cmd.ExecuteScalar().ToString()if (conn.State == ConnectionState.Open){Response.Write(count)Response.Write("数据库连接成功!")Response.Write("<scriptalert('连接已打开')</script")}conn.Close()if (conn.State == ConnectionState.Closed){Response.Write("连接已关闭")Response.Write("<scriptalert('连接已关闭')</script")}}Response.Write("数据库连接失败"+ex.Message)}}用_access函数判断,再次打开时的情况要看你第一次的打开方式了,如果上次用的是非独占打开,那没问题,如果是独占打开,会打开失败Example
/* ACCESS.C: This example uses _access to check the
* file named "ACCESS.C" to see if it exists and if
* writing is allowed.
*/
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
/* Check for existence */
if( (_access( "ACCESS.C", 0 )) != -1 )
{
printf( "File ACCESS.C exists\n" )
/* Check for write permission */
if( (_access( "ACCESS.C", 2 )) != -1 )
printf( "File ACCESS.C has write permission\n" )
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)