小型数据库中推荐大家使用SQLite,在中小型软件、网站开发时,如果不需要对数据管理有太高的要求都可以采用SQLite,其最关键的一点是免费而且开源。像Adobe Reader,Firefox中都采用了SQLite。官方网站 http://www.sqlite.org/ ,当前版本是3.6.17。
一、SQLite基本特性 
- 支持事务处理。
- 零配置。
- 支持大部分SQL92标准。 
- 数据库存储在单个文件中。 
- 默认支持十亿字节长的字符串。 
- SQLite解释器小于300kb。
- 比C/S型的数据库更快。
- 简单便捷的API。 
- 采用ANSI-C 编译,同时支持多种语言。 
- 完全开源,完全免费。 
- 跨平台。
- 自包含,不需要任何第三方组件。 
     SQLite 不支持以下SQL特性:
     FOREIGN KEY constraints,Complete trigger support,Complete ALTER TABLE support, RIGHT and FULL OUTER JOIN,
     Writing to ViEWs,GRANT and REVOKE。
二、GUI管理工具
     在SQLite的Wiki 上列出了很多管理工具,参见http://www.sqlite.org/cvstrac/wiki?p=ManagementTools ,推荐大家使用SQLite2009 Pro,
下载见http://osenxpsuite.net/?xp=3 ,也是免费的,最新版本的SQLite引擎是3.6.16。使用起来也很方便。
     主界面如下:
三、.NET的Wrapper
        很多语言都提供了SQLite 的Wrapper,见http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers 。
        推荐使用SQLite.NET,官方网站http://sqlite.phxsoftware.com/ ,SourceForge项目主页 http://sourceforge.net/projects/sqlite-dotnet2/ 。
SQLite.NET 还支持LINQ方式调用。同时提供VS插件,可以在VS中直接编辑数据库。
        最后,附上包装SQLite 的C#代码用于举例怎么使用。
  1 using System;
using System;
  2 using System.Collections.Generic;
using System.Collections.Generic;
  3 using System.Linq;
using System.Linq;
  4 using System.Text;
using System.Text;
  5 using System.Data;
using System.Data;
  6 using System.Data.SQLite;
using System.Data.SQLite;
  7
  8 namespace AirLibrary
namespace AirLibrary
  9

 {
{
 10
 /**//// <summary>
    /**//// <summary>
 11 /// 数据库接口类
    /// 数据库接口类
 12 /// </summary>
    /// </summary>
 13 public class SQLite : IDisposable
    public class SQLite : IDisposable
 14
 
     {
{
 15
 Attribute#region Attribute
        Attribute#region Attribute
 16 private string datasource = @"db\data.db3";
        private string datasource = @"db\data.db3";
 17 private bool isOpen;
        private bool isOpen;
 18 private bool disposed = false;
        private bool disposed = false;
 19
 20 private SQLiteConnection connection;
        private SQLiteConnection connection;
 21 private Dictionary<string, string> parameters;
        private Dictionary<string, string> parameters;
 22 #endregion //Attribute
        #endregion //Attribute
 23
 24
 Constructor#region Constructor
        Constructor#region Constructor
 25 public SQLite()
        public SQLite()
 26
 
         {
{
 27 init("");
            init("");
 28 }
        }
 29
 30
 /**//// <summary>
        /**//// <summary>
 31 /// 连接指定的数据库
        /// 连接指定的数据库
 32 /// </summary>
        /// </summary>
 33 /// <param name="datasource">连接字符串</param>
        /// <param name="datasource">连接字符串</param>
 34 public SQLite(string datasource)
        public SQLite(string datasource)
 35
 
         {
{
 36 init(datasource);
            init(datasource);
 37 }
        }
 38
 39
 /**//// <summary>
        /**//// <summary>
 40 /// 清理托管资源
        /// 清理托管资源
 41 /// </summary>
        /// </summary>
 42 public void Dispose()
        public void Dispose()
 43
 
         {
{
 44 Dispose(true);
            Dispose(true);
 45 GC.SuppressFinalize(this);
            GC.SuppressFinalize(this);
 46 }
        }
 47
 48
 /**//// <summary>
        /**//// <summary>
 49 /// 清理所有使用资源
        /// 清理所有使用资源
 50 /// </summary>
        /// </summary>
 51 /// <param name="disposing">如果为true则清理托管资源</param>
        /// <param name="disposing">如果为true则清理托管资源</param>
 52 protected void Dispose(bool disposing)
        protected void Dispose(bool disposing)
 53
 
         {
{
 54 if (!this.disposed)
            if (!this.disposed)
 55
 
             {
{
 56 // dispose all managed resources.
                // dispose all managed resources.
 57 if (disposing)
                if (disposing)
 58
 
                 {
{
 59 this.isOpen = false;
                    this.isOpen = false;
 60 connection.Dispose();
                    connection.Dispose();
 61 }
                }
 62
 63 // dispose all unmanaged resources
                // dispose all unmanaged resources
 64 this.close();
                this.close();
 65
 66 disposed = true;
                disposed = true;
 67 }
            }
 68 }
        }
 69
 70 ~SQLite()
        ~SQLite()
 71
 
         {
{
 72 Dispose(false);
            Dispose(false);
 73 }
        }
 74 #endregion //Constructor
        #endregion //Constructor
 75
 76
 Function#region Function
        Function#region Function
 77 private void init(string datasource)
        private void init(string datasource)
 78
 
         {
{
 79 if (datasource != "")
            if (datasource != "")
 80 this.datasource = datasource;
                this.datasource = datasource;
 81 this.connection = new SQLiteConnection("data source = " + this.datasource);
            this.connection = new SQLiteConnection("data source = " + this.datasource);
 82 this.parameters = new Dictionary<string, string>();
            this.parameters = new Dictionary<string, string>();
 83 this.isOpen = false;
            this.isOpen = false;
 84 }
        }
 85
 86 private bool checkDbExist()
        private bool checkDbExist()
 87
 
         {
{
 88 if (System.IO.File.Exists(datasource))
            if (System.IO.File.Exists(datasource))
 89 return true;
                return true;
 90 else
            else
 91 return false;
                return false;
 92 }
        }
 93
 94 private void open()
        private void open()
 95
 
         {
{
 96 if (!checkDbExist())
            if (!checkDbExist())
 97 throw new AirException("1001");
                throw new AirException("1001");
 98
 99 if (!isOpen)
            if (!isOpen)
100 connection.Open();
                connection.Open();
101
102 this.isOpen = true;
            this.isOpen = true;
103 }
        }
104
105 private void close()
        private void close()
106
 
         {
{
107 if (isOpen)
            if (isOpen)
108 connection.Close();
                connection.Close();
109
110 this.isOpen = false;
            this.isOpen = false;
111 }
        }
112 #endregion //Function
        #endregion //Function
113
114
 Method#region Method
        Method#region Method
115
 /**//// <summary>
        /**//// <summary>
116 /// 添加参数
        /// 添加参数
117 /// </summary>
        /// </summary>
118 /// <param name="key">参数名</param>
        /// <param name="key">参数名</param>
119 /// <param name="value">参数值</param>
        /// <param name="value">参数值</param>
120 public void AddParameter(string key, string value)
        public void AddParameter(string key, string value)
121
 
         {
{
122 parameters.Add(key, value);
            parameters.Add(key, value);
123 }
        }
124
125
 /**//// <summary>
        /**//// <summary>
126 /// 执行SQL语句
        /// 执行SQL语句
127 /// </summary>
        /// </summary>
128 /// <param name="queryStr">SQL语句</param>
        /// <param name="queryStr">SQL语句</param>
129 public void ExecuteNonQuery(string queryStr)
        public void ExecuteNonQuery(string queryStr)
130
 
         {
{
131 this.open();
            this.open();
132 using (SQLiteTransaction transaction = connection.BeginTransaction())
            using (SQLiteTransaction transaction = connection.BeginTransaction())
133
 
             {
{
134 using (SQLiteCommand command = new SQLiteCommand(connection))
                using (SQLiteCommand command = new SQLiteCommand(connection))
135
 
                 {
{
136 command.CommandText = queryStr;
                    command.CommandText = queryStr;
137 foreach (KeyValuePair<string, string> kvp in this.parameters)
                    foreach (KeyValuePair<string, string> kvp in this.parameters)
138
 
                     {
{
139 command.Parameters.Add(new SQLiteParameter(kvp.Key, kvp.Value));
                        command.Parameters.Add(new SQLiteParameter(kvp.Key, kvp.Value));
140 }
                    }
141
142 command.ExecuteNonQuery();
                    command.ExecuteNonQuery();
143 }
                }
144 transaction.Commit();
                transaction.Commit();
145 }
            }
146 this.close();
            this.close();
147 this.parameters.Clear();
            this.parameters.Clear();            
148 }
        }
149
150
 /**//// <summary>
        /**//// <summary>
151 /// 执行SQL语句并返回所有结果
        /// 执行SQL语句并返回所有结果
152 /// </summary>
        /// </summary>
153 /// <param name="queryStr">SQL语句</param>
        /// <param name="queryStr">SQL语句</param>
154 /// <returns>返回DataTable</returns>
        /// <returns>返回DataTable</returns>
155 public DataTable ExecuteQuery(string queryStr)
        public DataTable ExecuteQuery(string queryStr)
156
 
         {
{
157 DataTable dt = new DataTable();
            DataTable dt = new DataTable();
158 this.open();
            this.open();
159 try
            try
160
 
             {
{
161 using (SQLiteCommand command = new SQLiteCommand(connection))
                using (SQLiteCommand command = new SQLiteCommand(connection))
162
 
                 {
{
163 SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
164 command.CommandText = queryStr;
                    command.CommandText = queryStr;
165 foreach (KeyValuePair<string, string> kvp in this.parameters)
                    foreach (KeyValuePair<string, string> kvp in this.parameters)
166
 
                     {
{
167 command.Parameters.Add(new SQLiteParameter(kvp.Key, kvp.Value));
                        command.Parameters.Add(new SQLiteParameter(kvp.Key, kvp.Value));
168 }
                    }
169
170 adapter.Fill(dt);
                    adapter.Fill(dt);
171 }
                }
172 }
            }
173 catch (SQLiteException e)
            catch (SQLiteException e)
174
 
             {
{
175 throw new AirException("1002", e.Message);
                throw new AirException("1002", e.Message);
176 }
            }
177 finally
            finally
178
 
             {
{
179 this.close();
                this.close();
180 this.parameters.Clear();
                this.parameters.Clear();
181 }
            }
182 return dt;
            return dt;
183 }
        }
184
185
 /**//// <summary>
        /**//// <summary>
186 /// 执行SQL语句并返回第一行
        /// 执行SQL语句并返回第一行
187 /// </summary>
        /// </summary>
188 /// <param name="queryStr">SQL语句</param>
        /// <param name="queryStr">SQL语句</param>
189 /// <returns>返回DataRow</returns>
        /// <returns>返回DataRow</returns>
190 public DataRow ExecuteRow(string queryStr)
        public DataRow ExecuteRow(string queryStr)
191
 
         {
{
192 DataRow row;
            DataRow row;
193 this.open();
            this.open();
194 try
            try
195
 
             {
{
196 using (SQLiteCommand command = new SQLiteCommand(connection))
                using (SQLiteCommand command = new SQLiteCommand(connection))
197
 
                 {
{
198 SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
199 command.CommandText = queryStr;
                    command.CommandText = queryStr;
200 foreach (KeyValuePair<string, string> kvp in this.parameters)
                    foreach (KeyValuePair<string, string> kvp in this.parameters)
201
 
                     {
{
202 command.Parameters.Add(new SQLiteParameter(kvp.Key, kvp.Value));
                        command.Parameters.Add(new SQLiteParameter(kvp.Key, kvp.Value));
203 }
                    }
204
205 DataTable dt = new DataTable();
                    DataTable dt = new DataTable();
206 adapter.Fill(dt);
                    adapter.Fill(dt);
207 if (dt.Rows.Count == 0)
                    if (dt.Rows.Count == 0)
208 row = null;
                        row = null;
209 else
                    else
210 row = dt.Rows[0];
                        row = dt.Rows[0];
211 }
                }
212 }
            }
213 catch (SQLiteException e)
            catch (SQLiteException e)
214
 
             {
{
215 throw new AirException("1002", e.Message);
                throw new AirException("1002", e.Message);
216 }
            }
217 finally
            finally
218
 
             {
{
219 this.close();
                this.close();
220 this.parameters.Clear();
                this.parameters.Clear();
221 }
            }
222 return row;
            return row;
223 }
        }
224
225
 /**//// <summary>
        /**//// <summary>
226 /// 执行SQL语句并返回结果第一行的第一列
        /// 执行SQL语句并返回结果第一行的第一列
227 /// </summary>
        /// </summary>
228 /// <param name="queryStr">SQL语句</param>
        /// <param name="queryStr">SQL语句</param>
229 /// <returns>返回值</returns>
        /// <returns>返回值</returns>
230 public Object ExecuteScalar(string queryStr)
        public Object ExecuteScalar(string queryStr)
231
 
         {
{
232 Object obj;
            Object obj;
233 this.open();
            this.open();
234 using (SQLiteCommand command = new SQLiteCommand(connection))
            using (SQLiteCommand command = new SQLiteCommand(connection))
235
 
             {
{
236 command.CommandText = queryStr;
                command.CommandText = queryStr;
237 foreach (KeyValuePair<string, string> kvp in this.parameters)
                foreach (KeyValuePair<string, string> kvp in this.parameters)
238
 
                 {
{
239 command.Parameters.Add(new SQLiteParameter(kvp.Key, kvp.Value));
                    command.Parameters.Add(new SQLiteParameter(kvp.Key, kvp.Value));
240 }
                }
241
242 obj = command.ExecuteScalar();
                obj = command.ExecuteScalar();
243 }
            }
244 this.close();
            this.close();
245 this.parameters.Clear();
            this.parameters.Clear();
246 return obj;
            return obj;
247 }
        }
248 #endregion //Method
        #endregion //Method
249 }
    }
250 }
}
251
 
         这里的异常类是我自己项目中用的,大家实际使用时换成自己的异常类就是了。这里只是SQLite的一些简单应用,欢迎大家讨论指点。