欢迎来到冰点文库! | 帮助中心 分享价值,成长自我!
冰点文库
全部分类
  • 临时分类>
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • ImageVerifierCode 换一换
    首页 冰点文库 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

    C#操作SQLiteDB.docx

    • 资源ID:12326018       资源大小:19.65KB        全文页数:28页
    • 资源格式: DOCX        下载积分:8金币
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录
    二维码
    微信扫一扫登录
    下载资源需要8金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,免费下载
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    C#操作SQLiteDB.docx

    1、C#操作SQLiteDBC#操作SQLite DatabaseC#下SQLite操作驱动dll下载:System.Data.SQLiteC#使用SQLite步骤:(1)新建一个project(2)添加SQLite操作驱动dll引用(3)使用API操作SQLite DataBaseusing System;using System.Data.SQLite;namespace SQLiteSamples class Program /数据库连接 SQLiteConnection m_dbConnection; static void Main(string args) Program p = ne

    2、w Program(); public Program() createNewDatabase(); connectToDatabase(); createTable(); fillTable(); printHighscores(); /创建一个空的数据库 void createNewDatabase() SQLiteConnection.CreateFile(MyDatabase.sqlite); /创建一个连接到指定数据库 void connectToDatabase() m_dbConnection = new SQLiteConnection(Data Source=MyDataba

    3、se.sqlite;Version=3;); m_dbConnection.Open(); /在指定数据库中创建一个table void createTable() string sql = create table highscores (name varchar(20), score int); SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); /插入一些数据 void fillTable() string sql = insert into highscor

    4、es (name, score) values (Me, 3000); SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); sql = insert into highscores (name, score) values (Myself, 6000); command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); sql = insert into highscores (

    5、name, score) values (And I, 9001); command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); /使用sql查询语句,并显示结果 void printHighscores() string sql = select * from highscores order by score desc; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); SQLiteDataReader reader =

    6、 command.ExecuteReader(); while (reader.Read() Console.WriteLine(Name: + readername + tScore: + readerscore); Console.ReadLine(); 操作SQLite Database的C#帮助类SQLite Helper将一些常用的功能封装一下,封装成SQLite Helper类using System;using System.Data;using System.Text.RegularExpressions;using System.Xml;using System.IO;usi

    7、ng System.Collections;using System.Data.SQLite;namespace DBUtility.SQLite / / SQLiteHelper is a utility class similar to SQLHelper in MS / Data Access Application Block and follows similar pattern. / public class SQLiteHelper / / Creates a new instance. The ctor is marked private since all members a

    8、re static. / private SQLiteHelper() / / Creates the command. / / Connection. / Command text. / Command parameters. / SQLite Command public static SQLiteCommand CreateCommand(SQLiteConnection connection, string commandText, params SQLiteParameter commandParameters) SQLiteCommand cmd = new SQLiteComma

    9、nd(commandText, connection); if (commandParameters.Length 0) foreach (SQLiteParameter parm in commandParameters) cmd.Parameters.Add(parm); return cmd; / / Creates the command. / / Connection string. / Command text. / Command parameters. / SQLite Command public static SQLiteCommand CreateCommand(stri

    10、ng connectionString, string commandText, params SQLiteParameter commandParameters) SQLiteConnection cn = new SQLiteConnection(connectionString); SQLiteCommand cmd = new SQLiteCommand(commandText, cn); if (commandParameters.Length 0) foreach (SQLiteParameter parm in commandParameters) cmd.Parameters.

    11、Add(parm); return cmd; / / Creates the parameter. / / Name of the parameter. / Parameter type. / Parameter value. / SQLiteParameter public static SQLiteParameter CreateParameter(string parameterName, System.Data.DbType parameterType, object parameterValue) SQLiteParameter parameter = new SQLiteParam

    12、eter(); parameter.DbType = parameterType; parameter.ParameterName = parameterName; parameter.Value = parameterValue; return parameter; / / Shortcut method to execute dataset from SQL Statement and object arrray of parameter values / / SQLite Connection string / SQL Statement with embedded param styl

    13、e parameter names / object array of parameter values / public static DataSet ExecuteDataSet(string connectionString, string commandText, object paramList) SQLiteConnection cn = new SQLiteConnection(connectionString); SQLiteCommand cmd = cn.CreateCommand(); cmd.CommandText = commandText; if (paramLis

    14、t != null) AttachParameters(cmd,commandText, paramList); DataSet ds = new DataSet(); if (cn.State = ConnectionState.Closed) cn.Open(); SQLiteDataAdapter da = new SQLiteDataAdapter(cmd); da.Fill(ds); da.Dispose(); cmd.Dispose(); cn.Close(); return ds; / / Shortcut method to execute dataset from SQL S

    15、tatement and object arrray of parameter values / / Connection. / Command text. / Param list. / public static DataSet ExecuteDataSet(SQLiteConnection cn, string commandText, object paramList) SQLiteCommand cmd = cn.CreateCommand(); cmd.CommandText = commandText; if (paramList != null) AttachParameter

    16、s(cmd,commandText, paramList); DataSet ds = new DataSet(); if (cn.State = ConnectionState.Closed) cn.Open(); SQLiteDataAdapter da = new SQLiteDataAdapter(cmd); da.Fill(ds); da.Dispose(); cmd.Dispose(); cn.Close(); return ds; / / Executes the dataset from a populated Command object. / / Fully populat

    17、ed SQLiteCommand / DataSet public static DataSet ExecuteDataset(SQLiteCommand cmd) if (cmd.Connection.State = ConnectionState.Closed) cmd.Connection.Open(); DataSet ds = new DataSet(); SQLiteDataAdapter da = new SQLiteDataAdapter(cmd); da.Fill(ds); da.Dispose(); cmd.Connection.Close(); cmd.Dispose()

    18、; return ds; / / Executes the dataset in a SQLite Transaction / / SQLiteTransaction. Transaction consists of Connection, Transaction, / and Command, all of which must be created prior to making this method call. / Command text. / Sqlite Command parameters. / DataSet / user must examine Transaction O

    19、bject and handle transaction.connection .Close, etc. public static DataSet ExecuteDataset(SQLiteTransaction transaction, string commandText, params SQLiteParameter commandParameters) if (transaction = null) throw new ArgumentNullException(transaction); if (transaction != null & transaction.Connectio

    20、n = null) throw new ArgumentException(The transaction was rolled back or committed, please provide an open transaction., transaction); IDbCommand cmd = transaction.Connection.CreateCommand(); cmd.CommandText = commandText; foreach (SQLiteParameter parm in commandParameters) cmd.Parameters.Add(parm);

    21、 if (transaction.Connection.State = ConnectionState.Closed) transaction.Connection.Open(); DataSet ds = ExecuteDataset(SQLiteCommand)cmd); return ds; / / Executes the dataset with Transaction and object array of parameter values. / / SQLiteTransaction. Transaction consists of Connection, Transaction

    22、, / and Command, all of which must be created prior to making this method call. / Command text. / object array of parameter values. / DataSet / user must examine Transaction Object and handle transaction.connection .Close, etc. public static DataSet ExecuteDataset(SQLiteTransaction transaction, stri

    23、ng commandText, object commandParameters) if (transaction = null) throw new ArgumentNullException(transaction); if (transaction != null & transaction.Connection = null) throw new ArgumentException(The transaction was rolled back or committed, please provide an open transaction., transaction); IDbCom

    24、mand cmd = transaction.Connection.CreateCommand(); cmd.CommandText = commandText; AttachParameters(SQLiteCommand)cmd,cmd.CommandText, commandParameters); if (transaction.Connection.State = ConnectionState.Closed) transaction.Connection.Open(); DataSet ds = ExecuteDataset(SQLiteCommand)cmd); return d

    25、s; #region UpdateDataset / / Executes the respective command for each inserted, updated, or deleted row in the DataSet. / / / e.g.: / UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, Order); / / A valid SQL statement to insert new records into the data source / A valid SQL statement to delete records from the data source / A valid SQL statement used to update records in the data source / The DataSet used to update the data source / The DataTable used to update the data source. p


    注意事项

    本文(C#操作SQLiteDB.docx)为本站会员主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2023 冰点文库 网站版权所有

    经营许可证编号:鄂ICP备19020893号-2


    收起
    展开