2014-10-30 28 views
1

我與操縱SQLite數據庫C#應用程序的工作,直到昨天這是工作的罰款,這是檢索記錄,SQLite的連接不工作在C#

但是從昨晚起,連接字符串返回數據源= 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(); 
    } 

} 

}

下面是調試監視窗口期間所看到的圖像..

我使用的圖書館是「的SQLite-1.0.66.0-setup.exe」文件

我與其他數據庫創建的,但同樣的結果測試,任何機構可以幫助?

+0

嘗試添加版本。 「Data Source = MyDatabase.sqlite; Version = 3;」 – Mate 2014-10-30 06:35:32

+0

我做了,但沒有運氣 – DareDevil 2014-10-30 06:51:01

+1

你確定該文件存在於「Environment.CurrentDirectory」中,並且你有權限?你有沒有試過_connection.Open()?拋出異常? – Mate 2014-10-30 06:58:09

回答

4

這裏是我做了什麼:

private void button2_Click(object sender, EventArgs e) 
    { 
     String connString = "Data Source=" + Environment.CurrentDirectory + "\\UrduDictionary"; 
     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(dr.GetValue(0) + " " + dr.GetValue(1) + " " + dr.GetValue(2)); 
        } 
       } 
      } 
     } 
     //Console.ReadLine(); 
    } 
+0

太棒了!嘗試使用Path.Combine和String.Format來改善您使用字符串的工作。 – Mate 2014-10-30 07:26:53

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); 
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()); 
      } 

連接如果有人想用低於實體框架使用它的代碼是代碼(用的EntityFramework版本4.1.0.0測試)

模型的DataContext

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()); 
      } 

,這裏是包含DbProviderFactories和defau app.config文件ltConnectionFactory

<?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> 
+0

感謝您的詳細回覆 – DareDevil 2014-10-30 07:39:44