csharp:SQLite and Access using C# code read data

SQLite sql script:

CREATE TABLE BookKindList
(
    BookKindID INTEGER PRIMARY KEY AUTOINCREMENT,
    BookKindName varchar(500) not null,
           BookKindCode varchar(100) null,
    BookKindParent int null
)

--添加
insert into BookKindList(BookKindName,BookKindCode,BookKindParent) values('目录','0003',1);
--查询
select * from BookKindList;

select * from BookKindList where BookKindList.BookKindID=2;
--删除
delete from BookKindList where BookKindList.BookKindID=3;
--更新
update  BookKindList set BookKindName='文学',BookKindCode='0002',BookKindParent=1 where BookKindID=2;

--返回添加自增ID
select last_insert_rowid();

SQLite 数据库连接字符

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>

 <add key="ConnectionAccess2003String"
value="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|geovindu.mdb;Persist Security Info=True"/>

 <add key="ConnectionAccess2007String"
value="Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|geovindu.accdb;Persist Security Info=True"/>

 <add key="ConnectionSQLString" value="Data
Source=LF-WEN\GEOVINDU;Initial Catalog=geovindu;User
ID=sa;Password=geovindu"/>
    <add
key="ConnectionSQLiteString" value="Data
Source=geovindu.db3;Version=3;Password=geovindu;Pooling=true;FailIfMissing=false;"/>

 <add key="ConnectionMySQLString" value="Database='geovindu';Data
Source='127.0.0.1';User
Id='root';Password='geovindu';charset='utf8';pooling=true;Port=3306;Allow
 Zero Datetime=true;"/>
    <add key="WebDAL" value="AccessDAL"/>
    <!--<add key="WebDAL" value="SqlServerDAL"/>-->
    <!--<add key="WebDAL" value="SqlSQLiteDAL"/>-->
    <!--<add key="WebDAL" value="SqlMySQLDAL"/>-->
    <!--<add key="WebDAL" value="SqlPostgreSQLDAL"/>-->
  </appSettings>

</configuration>

Csharp操作SQLite 添加返回值

/// <summary>
       /// 涂聚文 20150212
       /// SQLite 添加返回值ID
       /// </summary>
       /// <param name="SQLString"></param>
       /// <param name="identity"></param>
       /// <param name="cmdParms"></param>
       /// <returns></returns>
       public static int ExecuteSql(string SQLString, out int identity, params SQLiteParameter[] cmdParms)
       {
           string en = "";
           using (SQLiteConnection connection = new SQLiteConnection(connectionString))
           {
               using (SQLiteCommand cmd = new SQLiteCommand())
               {
                   try
                   {
                       PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                       int rows = cmd.ExecuteNonQuery();
                       cmd.Parameters.Clear();
                       cmd.CommandText = "select last_insert_rowid();";
                       en = cmd.ExecuteScalar().ToString();
                       identity = int.Parse(en);
                       cmd.Parameters.Clear();
                       return rows;
                   }
                   catch (System.Data.OleDb.OleDbException E)
                   {
                       throw new Exception(E.Message);
                   }
               }
           }
       }

/// <summary>
       /// 追回返回值
       /// SQLite涂聚文
       /// </summary>
       /// <param name="bookKindList"></param>
       /// <param name="id"></param>
       /// <returns></returns>
       public int InsertBookKindOut(BookKindListInfo bookKindList, out int id)
       {
           int ret = 0;
           int tid = 0;
           try
           {
               StringBuilder str = new StringBuilder();
               str.Append("insert into BookKindList(BookKindName,BookKindCode,BookKindParent) values(");
               str.Append("@BookKindName,@BookKindCode,@BookKindParent);");
               SQLiteParameter[] par = new SQLiteParameter[]{
               new SQLiteParameter("@BookKindName",DbType.String,1000),
               new SQLiteParameter("@BookKindCode",DbType.String,1000),
               new SQLiteParameter("@BookKindParent",DbType.Int32,4),
               //new SQLiteParameter("@BookKindID",DbType.Int32,4),
               };
               par[0].Value = bookKindList.BookKindName;
               par[1].Value = bookKindList.BookKindCode;
               par[2].Value = bookKindList.BookKindParent;
               //par[3].Direction = ParameterDirection.Output;//无效  不支持
               ret = SQLiteHelper.ExecuteSql(str.ToString(), out tid, par);
               if (ret > 0)
               {
                   //tid = ret;// (int)par[3].Value;
               }
           }
           catch (SQLiteException ex)
           {
               throw ex;
           }
           id = tid;

           return ret;
       }

Csharp操作Access添加返回值

/// <summary>
///  Access 添加返迴ID值
///  涂聚文 2014-12-29
///  Geovin Du
/// 參考:  http://www.mikesdotnetting.com/article/54/getting-the-identity-of-the-most-recently-added-record
/// http://stackoverflow.com/questions/186544/identity-after-insert-statement-always-returns-0
/// </summary>
/// <param name="SQLString"></param>
/// <param name="identity"></param>
/// <param name="cmdParms"></param>
/// <returns></returns>
public static int ExecuteSql(string SQLString, out int identity, params OleDbParameter[] cmdParms)
{

    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        using (OleDbCommand cmd = new OleDbCommand())
        {
            try
            {
                PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                int rows = cmd.ExecuteNonQuery();
                cmd.CommandText = "Select @@Identity";
                identity = (int)cmd.ExecuteScalar();
                cmd.Parameters.Clear();
                return rows;
            }
            catch (System.Data.OleDb.OleDbException E)
            {
                throw new Exception(E.Message);
            }
        }
    }
/// <summary>
/// Access 追加返回值
/// 涂聚文
/// 20141205
/// </summary>
/// <param name="bookKindList"></param>
/// <param name="id"></param>
/// <returns></returns>
public int InsertBookKindOut(BookKindListInfo bookKindList, out int id)
{
    int ret = 0;
    int tid = 0;
    try
    {
        StringBuilder str = new StringBuilder();
        str.Append("insert into BookKindList(BookKindName,BookKindCode,BookKindParent) values(");
        str.Append("@BookKindName,@BookKindCode,@BookKindParent);");
        OleDbParameter[] par = new OleDbParameter[]{
        new OleDbParameter("@BookKindName",OleDbType.VarChar,1000),
        new OleDbParameter("@BookKindCode",OleDbType.VarChar,1000),
        new OleDbParameter("@BookKindParent",OleDbType.Integer,4),
        //new OleDbParameter("@BookKindID",OleDbType.Integer,4),
        };
        par[0].Value = bookKindList.BookKindName;
        par[1].Value = bookKindList.BookKindCode;
        par[2].Value = bookKindList.BookKindParent;
        //par[3].Direction = ParameterDirection.Output;//无效  不支持
        ret = DbHelperOleDb.ExecuteSql(str.ToString(), out tid, par);
        if (ret > 0)
        {
            //tid = ret;// (int)par[3].Value;
        }
    }
    catch (OleDbException ex)
    {
        throw ex;
    }
    id = tid;

    return ret;
}
时间: 2024-08-02 16:15:11

csharp:SQLite and Access using C# code read data的相关文章

sqlserver,sqlite,access数据库链接字符串整理_C 语言

SqlServer:string connection = "server=32.1.1.48;database=数据库名;user=sa;password=sa2008"; access 2007: //无密码的连接字符串 string conStr = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=E://111.accdb;Persist Security Info=False"; //有密码的连接字符串 str

java D:\code\cpp\data\frames_Src\001.jpg

问题描述 java D:codecppdataframes_Src01.jpg 得到一个字符串,如:strFileFullName = "D:codecppdataframes_Src01.jpg" 怎样得到: strFilePath = "D:codecppdataframes_Src" strFileName = "001.jpg" 解决方案 String strFileFullName = "D:\code\cpp\data\fr

powerDesigner如何同时显示name,code和data type

问题描述 我在word上看到pd的截图是:显示了 name和code和data type可是在我本地安装的pd显示是:值显示name和data type.应该在pd中如何设置?谢谢 解决方案 http://blog.csdn.net/spt110/article/details/8640849参考一下

LinqToSQL: Comprehensive Support for SQLite, MS Access, SQServer2000/2005

问题描述 http://www.codeproject.com/KB/linq/linqToSql_7.aspx一起研究下. 解决方案 解决方案二:可惜这3中数据库我现在都不用.不过我都用过,而且也都相当了解.我设置使用jet(access的数据库引擎库)开发过一般人认为access绝对不可能开发的大型应用.2.3年前我为了取代msde和jet,我使用过sqlite.使用一个linq产品同时兼容多种数据库,而且语法也与其它linq产品(例如linqtooracle)类似,是很有长远意义的.当然如

使用SQLite数据库和Access数据库的一些经验总结

在我的<Winform开发框架>中,可使用多种数据库作为程序的数据源,除了常规的Oracle数据库.SqlServer.MySql数据库,其中还包括了SQLite数据库.Access数据库,后两个数据库都是在单机版程序中常用到的数据库,各自有着自己的特点,在我的<Winform开发框架>的提炼和多个项目的反复使用过程中,对SQLite数据库.Access数据库的一些特点进行了一些总结,以期达到常用常新,避免走弯路的目的. 1.简化数据库的地址,使用|DataDirectory|代替

C#操作SQLite数据库

SQLite介绍 SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页:http://www.sqlite.o

C#操作SQLite数据库方法小结(创建,连接,插入,查询,删除等)_C#教程

本文实例讲述了C#操作SQLite数据库方法.分享给大家供大家参考,具体如下: SQLite介绍 SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQ

ADO.NET Code Samples Collection【转载】

原文链接:http://blogs.msdn.com/b/jmeier/archive/2010/10/31/ado-net-code-samples-collection.aspx 版权声明:本文全部版权归原作者所有. ADO.NET Code Samples Collection  J.D. Meier   31 Oct 2010 3:17 PM    The ADO.NET Code Samples Collection is a roundup and map of some of th

OEA 2.11 支持单机版数据库 - SQLite与SQLCE对比

在 OEA 平台 设计中,需要支持多种数据库,这至少包括了 SqlServer.Oracle.一个单机文件数据库.而之前对于这一块,我们一直没有实现,只是预留了许多接口.昨天花费了一天的时间,让 OEA 支持了本地数据库 SQLCE.   为什么要支持本地数据库呢? 支持开发简单的本地软件.  偶尔需要一些比较小的应用程序,例如"个人计帐管理"."个人联系方式管理"等.这些简单的软件,如果还让客户装个 SQLServer2008,客户觉得都围绕着.选择使用单机数据库