添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
乐观的火锅  ·  matlab ...·  4 周前    · 
刀枪不入的感冒药  ·  孙鑫 ...·  1 年前    · 
1  使用config配置文件

按照Sqlite官方教程,需要使用config(web项目叫做web.config,其它项目叫做app.config)配置文件存放连接Sqlite的各个参数,如下所示:

1.1 DbProviderFactories and You

One of the great new features of ADO.NET 2.0 is the use of reflection as a means of instantiating database providers programmatically. The information .NET uses to enumerate the available data providers in the system is relatively simple. It merely looks in the machine.config and in your own app.config file for some XML data to tell it what providers are installed and what assemblies those providers are in.

Scenario 1:  Version Independent (does not use the Global Assembly Cache)

This method allows you to drop any new version of the System.Data.SQLite.DLL into your application's folder and use it without any code modifications or recompiling.  Add the following code to your app.config file:

                        

Scenario 2:  Version Dependent, using either the DLL located in the same folder as the application or the Global Assembly Cache

This method expands on the above XML to provide the version number and key token of the SQLite DLL so it can be found either in the same folder as the application or looked up in the GAC.  The downside to this method is that DbProviderFactories will use this version information to only load the version specified.  This means if you update the DLL, you must also update this XML.

                        

The following C# code demonstrates instantiating SQLite through DbProviderFactories:

      DbProviderFactory fact = DbProviderFactories.GetFactory("System.Data.SQLite");
      using (DbConnection cnn = fact.CreateConnection())
        cnn.ConnectionString = "Data Source=test.db3";
        cnn.Open();
      }

上面的意思是,有两种配置方式,一种是不指定具体的Sqlite版本,另一种是需要指定具体的Sqlite版本,但是都是在config文件中指定的。

2  不使用config配置文件

2.1 创建控制台程序,在nuget中安装System.Data.SQLite

安装成功后,会自动在app.config文件中添加类似于上面的配置项目,为了证明不需要app.config文件,删除app.config。还会自动添加一些Sqlite类库,如下图:

C#不用config配置文件使用Sqlite数据库_SQLite

2.2  在代码中配置连接Sqlite的各个参数

static private DataTable MakeNamesTable()
            // Create a new DataTable titled 'Names.'
            DataTable namesTable = new DataTable("DbFactory");
            // Add three column objects to the table.
            DataColumn nameColumn = new DataColumn();
            nameColumn.DataType = System.Type.GetType("System.String");
            nameColumn.ColumnName = "Name";
            namesTable.Columns.Add(nameColumn);
            DataColumn descriptionColumn = new DataColumn();
            descriptionColumn.DataType = System.Type.GetType("System.String");
            descriptionColumn.ColumnName = "Description";
            namesTable.Columns.Add(descriptionColumn);
            DataColumn invariantColumn = new DataColumn();
            invariantColumn.DataType = System.Type.GetType("System.String");
            invariantColumn.ColumnName = "InvariantName";
            namesTable.Columns.Add(invariantColumn);
            DataColumn typeColumn = new DataColumn();
            typeColumn.DataType = System.Type.GetType("System.String");
            typeColumn.ColumnName = "AssemblyQualifiedName";
            namesTable.Columns.Add(typeColumn);
            // Create an array for DataColumn objects.
            DataColumn[] keys = new DataColumn[1];
            keys[0] = nameColumn;
            namesTable.PrimaryKey = keys;
            // Return the new DataTable.
            return namesTable;
static private DataRow CreateNewDataRow()
            DataTable table;
            table = MakeNamesTable();
            DataRow row;
            row = table.NewRow();
            row["Name"] = "SQLite Data Provider";
            row["Description"] = ".NET Framework Data Provider for SQLite";
            row["InvariantName"] = "System.Data.SQLite";
            row["AssemblyQualifiedName"] = "System.Data.SQLite.SQLiteFactory, System.Data.SQLite";
            table.Rows.Add(row);
            foreach (DataColumn column in table.Columns)
                Console.WriteLine(column.ColumnName);
            return row;
 static void Main(string[] args)
            var dt= DbProviderFactories.GetFactoryClasses();
            foreach(DataColumn c in dt.Columns)
                Console.WriteLine(c.Caption);
            DbProviderFactory fact = DbProviderFactories.GetFactory(CreateNewDataRow());
            using (DbConnection cnn = fact.CreateConnection())
                cnn.ConnectionString = "Data Source=test.db";
                cnn.Open();
                var cmd= cnn.CreateCommand();
                cmd.CommandText = "create table test(id int)";
                cmd.ExecuteScalar();
        }

运行,然后在debug或者release目录下,应该有test.db数据库文件了