SQLite连接无法在C#中工作

时间:2014-10-30 06:26:34

标签: c# database sqlite connection-string

我正在使用操作SQLite数据库的C#应用​​程序,直到昨天它工作正常,它正在检索记录,

但自昨晚以来,Connection String返回Data Source = null

以下是测试代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace SQLiteTest
{
    public partial class Form1 : Form
    {
        //string connection_string = "Data Source=UrduDictionary";
        string connection_string = "Data Source=" + Environment.CurrentDirectory + "\\Test.sqlite";
        string query = "";
        private SQLiteConnection _connection;
        private SQLiteCommand _command;
        private SQLiteDataAdapter _adapter;
        DataSet local;
        public Form1()
        {
            InitializeComponent();
        }
    void Make_Connection()
    {
        _connection = new SQLiteConnection(connection_string);
    }
    private void button1_Click(object sender, EventArgs e)
    {                 
         Make_Connection();
    }

}

}

下面是在Watch Window中调试期间看到的图像..

我正在使用的库是" SQLite-1.0.66.0-setup.exe"

我已经使用其他数据库进行了测试但结果相同,任何机构都可以提供帮助吗?

4 个答案:

答案 0 :(得分:5)

这是我做的:

private void button2_Click(object sender, EventArgs e)
{
    string dbPath = Path.Combine(Environment.CurrentDirectory, "UrduDictionary");
    string connString = string.Format("Data Source={0}", dbPath);

    using (SQLiteConnection conn = new SQLiteConnection(connString))
    {
        StringBuilder query = new StringBuilder();
        query.Append("SELECT * ");
        query.Append("FROM CATIGORY_TABLE ");

        using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), conn))
        {
            conn.Open();

            using (SQLiteDataReader dr = cmd.ExecuteReader())
            {
                while (dr.Read())
                {
                    Console.WriteLine("{0} {1} {2}",
                        dr.GetValue(0),
                        dr.GetValue(1),
                        dr.GetValue(2));
                }
            }
        }
    }
}

答案 1 :(得分:2)

以防万一...

var pathDB = System.IO.Path.Combine(Environment.CurrentDirectory, "Test.sqlite"); 
if (!System.IO.File.Exists(pathDB)) throw new Exception(); 
var connection_string = String.Format("Data Source={0};Version=3;", pathDB);

答案 2 :(得分:1)

这就是我用C#连接的sqlite:

  • 我从here
  • 下载了Sqlite二进制文件
  • 然后这是与Sqlite连接的代码:
string dbConnectionString = @"Data Source=Sample.s3db;Version=3;";
try
{
    SQLiteConnection sqlite_con = new SQLiteConnection(dbConnectionString);
    sqlite_con.Open();
    string query = "select * from test;";
    SQLiteCommand sqlite_cmd = new SQLiteCommand(query, sqlite_con);
    SQLiteDataReader dr = sqlite_cmd.ExecuteReader();
    while (dr.Read())
    {
        MessageBox.Show(dr.GetString(1));
    }

    sqlite_con.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

如果有人想在下面的Entity Framework中使用它,那么代码(使用EntityFramework version 4.1.0.0进行测试)

模型和数据文字

namespace WpfApplication1.Model
{
    public class Movie
    {
        public Int64 Id { get; set; }
        public string name { get; set; }
    }
    public class MySqliteContext : DbContext
    {
        public DbSet<Movie> Movies { get; set; }
    }
}

List<Model.Movie> movies = new List<Model.Movie>();
using (var context = new MySqliteContext())
    movies = context.Movies.ToList();

foreach (var movie in movies)
    MessageBox.Show(movie.name.ToString());

以下是 app.config 文件,其中包含DbProviderFactoriesDefaultConnectionFactory

<?xml version="1.0"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="true" />
  </configSections>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
           type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.84.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="MySqliteContext" connectionString="Data Source=|DataDirectory|Sample.s3db" providerName="System.Data.SQLite" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.SQLite.SQLiteFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

答案 3 :(得分:0)

始终使用最后一个稳定版本,在这种情况下为1.6.292