.net/C# 订阅所有【.net/C#】的日志

sqlite链接数据库 创建数据库 读取数据库

C#代码
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Web;   
  4. using System.Web.UI;   
  5. using System.Web.UI.WebControls;   
  6.   
  7. using System.IO;   
  8. using System.Data.Common;   //DbProviderFactory   
  9. using System.Data.SQLite;   
  10.   
  11. using System.Diagnostics;   //Stopwatch   
  12. public partial class _Default : System.Web.UI.Page   
  13. {   
  14.     protected void Page_Load(object sender, EventArgs e)   
  15.     {   
  16.   
  17.     }   
  18.     protected void Button1_Click(object sender, EventArgs e)   
  19.     {   
  20.   
  21.         File.Delete(Server.MapPath("_Data/sqldb.db"));   
  22.         SQLiteConnection.CreateFile(Server.MapPath("_Data/sqldb.db"));   
  23.            
  24.         DbProviderFactory factory = SQLiteFactory.Instance;   
  25.         using (DbConnection conn = factory.CreateConnection())   
  26.         {   
  27.             // 开始计时   
  28.             Stopwatch watch = new Stopwatch();   
  29.             watch.Start();   
  30.             // 连接数据库   
  31.             conn.ConnectionString = "Data Source=" + Server.MapPath("_Data/sqldb.db");   
  32.             conn.Open();   
  33.   
  34.             // 创建数据表   
  35.             string sql = "create table [test] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE)";   
  36.             DbCommand cmd = conn.CreateCommand();   
  37.             cmd.Connection = conn;   
  38.             cmd.CommandText = sql;   
  39.             cmd.ExecuteNonQuery();   
  40.   
  41.             // 添加参数   
  42.             cmd.Parameters.Add(cmd.CreateParameter());   
  43.   
  44.   
  45.             // 停止计时   
  46.             watch.Stop();   
  47.             Console.WriteLine(watch.Elapsed);   
  48.             Response.Write(watch.Elapsed);   
  49.             watch.Start();   
  50.             DbTransaction trans = conn.BeginTransaction(); // <-------------------   
  51.             try  
  52.             {   
  53.                 // 连续插入1000条记录   
  54.                 for (int i = 0; i < 1000; i++)   
  55.                 {   
  56.                     cmd.CommandText = "insert into [test] ([s]) values (?)";   
  57.                     cmd.Parameters[0].Value = i.ToString();   
  58.   
  59.                     cmd.ExecuteNonQuery();   
  60.                 }   
  61.   
  62.                 trans.Commit(); // <-------------------   
  63.             }   
  64.             catch  
  65.             {   
  66.                 trans.Rollback(); // <-------------------   
  67.                 throw// <-------------------   
  68.             }   
  69.   
  70.             watch.Stop();   
  71.             Console.WriteLine(watch.Elapsed);   
  72.             Response.Write(watch.Elapsed);   
  73.             conn.Close();   
  74.             conn.Dispose();   
  75.         }   
  76.     }   
  77.     protected void Button2_Click(object sender, EventArgs e)   
  78.     {   
  79.   
  80.         DbProviderFactory factory = SQLiteFactory.Instance;   
  81.         using (DbConnection conn = factory.CreateConnection())   
  82.         {   
  83.             conn.ConnectionString = "Data Source=" + Server.MapPath("_Data/" + "sqldb.db");   
  84.             conn.Open();   
  85.   
  86.   
  87.             // 创建数据表   
  88.             string sql = "select * from [test]";   
  89.             DbCommand cmd = conn.CreateCommand();   
  90.             cmd.Connection = conn;   
  91.             cmd.CommandText = sql;   
  92.             DbDataReader dr = cmd.ExecuteReader();   
  93.             while (dr.Read())   
  94.             {   
  95.                 TextBox1.Text += dr["s"] + "\n";   
  96.             }   
  97.             dr.Close();   
  98.             dr.Dispose();   
  99.             conn.Close();   
  100.             conn.Dispose();   
  101.         }   
  102.     }   
  103. }   

 



文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.