search
HomeDatabaseMysql TutorialWinform操作Access

有时候做数据的中转,SQLServer和Oracle这些大型数据库有点杀鸡用牛刀,而且会增加维护成本,这时可以使用Access数据库,尤其是处理Winform的时候。下面简单说一下 Access的数据访问类的使用方法,该类为静态方法,如果是多线程程序,可能会造成数据库连接之

     有时候做数据的中转,SQLServer和Oracle这些大型数据库有点“杀鸡用牛刀”,而且会增加维护成本,这时可以使用“Access”数据库,尤其是处理“Winform”的时候。下面简单说一下
Access的数据访问类的使用方法,该类为静态方法,如果是多线程程序,可能会造成“数据库”连接之间的竞争。比如一个线程打开了数据库连接,还没有处理完,另一个线程就要关闭,这时就不能使用这个类了。

1、类如下。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.OleDb;

 

namespace Model
{
///


/// DataAccess 的摘要说明 C#操作Access实例解析
///

public class DataAccess
{
protected static OleDbConnection conn = new OleDbConnection();
protected static OleDbCommand comm = new OleDbCommand();
public DataAccess()
{
//init C#操作Access实例解析
}
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + ConfigurationManager.AppSettings["myconn"];
//web.config文件里设定。
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception e)
{ throw new Exception(e.Message); }

}

}//打开数据库 C#操作Access实例解析

private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
comm.Dispose();
}
}//关闭数据库 C#操作Access实例解析

public static void excuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{ closeConnection(); }
}//执行sql语句 C#操作Access实例解析

public static OleDbDataReader dataReader(string sqlstr)
{
OleDbDataReader dr = null;
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;

dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
dr.Close();
closeConnection();
}
catch { }
}
return dr;
}
//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
public static void dataReader(string sqlstr, ref OleDbDataReader dr)
{
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
if (dr != null && !dr.IsClosed)
dr.Close();
} //C#操作Access实例解析
catch
{
}
finally
{
closeConnection();
}
}
}
//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭

public static DataSet dataSet(string sqlstr)
{
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);

}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return ds;
}//返回指定sql语句的dataset C#操作Access实例解析

public static void dataSet(string sqlstr, ref DataSet ds)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}//返回指定sql语句的dataset C#操作Access实例解析

public static DataTable dataTable(string sqlstr)
{
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dt;
}//返回指定sql语句的datatable
public static void dataTable(string sqlstr, ref DataTable dt)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}//返回指定sql语句的datatable C#操作Access实例解析

public static DataView dataView(string sqlstr)
{
OleDbDataAdapter da = new OleDbDataAdapter();
DataView dv = new DataView();
DataSet ds = new DataSet();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
dv = ds.Tables[0].DefaultView;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dv;
}
//返回指定sql语句的dataview C#操作Access实例解析

}

}


2、配置文件中连接字符串


3、查询及判断数据存在
string IsExistSQL = " select * from ETLSettings where ETLName='" + name + "'";

if (DataAccess.dataTable(IsExistSQL).Rows.Count == 0)
{}

4、创建表
string ETLCreateSql = "Create TABLE " + name +
" ( DANo VARCHAR NOT NULL, DATime DATETIME NOT NULL, LogTime DATETIME NOT NULL, MeterType VARCHAR NOT NULL, MeterNo VARCHAR NOT NULL, Qty Decimal(18,6) NOT NULL )";

DataAccess.excuteSql(ETLCreateSql);


5、增加及删除记录
string ETLSql = " insert into ETLSettings values ('" + name + "','" + name + "'," + "1,0)";

ETLSql = " delete from ETLSettings where ETLName='" + name + "'";


6、删除表
drop table test

7、Access里插入时间需要“#XXXXXXXX#”这样。

DataAccess.excuteSql("insert into " + etlname + " (DANo,DATime,LogTime,MeterType,MeterNo,Qty,Status) values ('"+model.DANo+"',#"+model.DATime+"#,#"+model.LogTime+"#,'"+model.MeterType+"','"+model.MeterNo+"',"+model.Qty+",0)");

 

8、Access的连接数

Access是允许同时有255个打开的连接,注意是打开,打开并不表示就一定在执行查询。如果要执行查询,那是另外的事,和理论支持“255个并发连接”不冲突。

Access的连接是串行执行,没有并行执行模式。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP编程中有哪些常见的Behat操作?PHP编程中有哪些常见的Behat操作?Jun 12, 2023 am 08:19 AM

PHP编程中有哪些常见的Behat操作?Behat是一个行为驱动开发(BDD)工具,允许测试人员和开发人员在自然语言中撰写测试用例,并将这些用例转化为可执行的代码。它支持PHP语言,并提供了丰富的库和功能,可以实现多种常见的测试操作。下面列举了PHP编程中常见的Behat操作。前置条件(Background)在编写测试用例时,有时候会有一些公共的前置条件需要

ThinkPHP6如何进行表单验证操作?ThinkPHP6如何进行表单验证操作?Jun 12, 2023 am 09:36 AM

ThinkPHP6是一款基于PHP的MVC框架,极大地简化了Web应用程序的开发。其中表单验证是一个非常基础和重要的功能。在这篇文章中,我们将介绍ThinkPHP6中如何进行表单验证操作。一、验证规则定义在ThinkPHP6中,验证规则都需要定义在控制器中,我们可以通过在控制器中定义一个$validate属性来实现规则的定义,如下所示:usethinkVa

PHP编程中有哪些常见的jQuery操作?PHP编程中有哪些常见的jQuery操作?Jun 12, 2023 am 10:38 AM

PHP编程中有哪些常见的jQuery操作?在PHP编程中,使用jQuery进行网页开发是一种非常方便和高效的方式。jQuery是一个简单而强大的JavaScript库,包含了许多实用的方法和函数。在PHP编程中,我们常常使用jQuery来操纵HTML和DOM元素,使网页具有更好的交互性和高度的可视化效果。在本文中,我们将介绍一些常见的PHP编程中使用jQue

PHP编程中有哪些常见的OAuth操作?PHP编程中有哪些常见的OAuth操作?Jun 12, 2023 am 08:48 AM

OAuth(开放授权)是一种用于授权访问控制的标准化协议。在Web开发中,使用OAuth可以帮助应用程序安全地从第三方平台中获取用户数据或资源。而在PHP编程中,OAuth操作也被广泛应用。本文将介绍PHP编程中的常见OAuth操作。OAuth1.0a授权OAuth1.0a授权是OAuth中最早出现的授权方式,也是最复杂的一种授权方式。在PHP编程中,O

怎样使用ThinkPHP6进行多语言翻译操作?怎样使用ThinkPHP6进行多语言翻译操作?Jun 12, 2023 am 08:49 AM

随着全球化的发展,越来越多的网站和应用程序需要提供多语言支持。而对于使用ThinkPHP6框架的开发者来说,如何实现多语言翻译操作是一个重要的需求。本文将介绍怎样使用ThinkPHP6进行多语言翻译操作。配置语言包在ThinkPHP6中,语言包是一个包含键值对的数组。可以将其存储在app/lang/目录下的各种子目录中。例如:/app/lang/zh-cn/

PHP编程中有哪些常见的Ajax操作?PHP编程中有哪些常见的Ajax操作?Jun 12, 2023 am 08:26 AM

随着Web应用程序的发展,Ajax成为了一种重要的技术,在PHP编程中也得到了广泛的应用。通过Ajax技术,Web应用程序可以实现异步操作,从而提高了用户体验和应用程序性能。在本文中,我们将探讨PHP编程中常见的Ajax操作。一、Ajax基础知识在介绍常见的Ajax操作之前,我们先来了解一下Ajax技术的基础知识。Ajax全称为"AsynchronousJ

怎样在ThinkPHP6中进行captcha图形验证码操作?怎样在ThinkPHP6中进行captcha图形验证码操作?Jun 12, 2023 am 11:45 AM

随着互联网的快速发展,基于图形的验证码已经成为了网站安全保障的一个重要环节。验证码可以有效地防止机器人或恶意程序对网站进行自动化操作,同时也可以保障用户信息的安全性。而在基于ThinkPHP6的网站开发中,如何实现captcha图形验证码的操作呢?本文将为您介绍具体的操作流程。一、生成Captcha图形验证码1、使用captcha库进行安装在ThinkPHP

ThinkPHP6中如何进行分词搜索操作?ThinkPHP6中如何进行分词搜索操作?Jun 12, 2023 am 09:39 AM

随着互联网应用的不断发展,搜索引擎也成为了日常生活中必不可少的工具,而分词搜索是搜索引擎中非常重要的一种搜索方式。在使用ThinkPHP6框架开发项目时,我们也需要对分词搜索进行深入了解和应用。本文将介绍ThinkPHP6中如何进行分词搜索操作。一、分词搜索简介分词搜索是将用户输入的关键词进行分割,然后在数据库中进行模糊搜索,找到相符合的记录。相较于传统的搜

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),