Created
January 15, 2014 12:19
-
-
Save KentaYamada/8435223 to your computer and use it in GitHub Desktop.
SQLite:DBファイル作成→ファイルパスを指定してDB接続する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data.SQLite; | |
using System.IO; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
private static readonly string FilePath = @"E:\Users\Desktop\SampleDB.db"; | |
static void Main(string[] args) | |
{ | |
//指定されたファイルパスにファイルが存在しない場合は作成 | |
if (!File.Exists(FilePath)) | |
{ | |
using (FileStream stream = File.Create(FilePath)) | |
{ | |
//ここでストリームを確実に閉じないと、接続処理で例外が発生 | |
if(null != stream) | |
{ | |
stream.Close(); | |
} | |
} | |
} | |
using (var conn = new SQLiteConnection("Data Source=" + FilePath)) | |
{ | |
try | |
{ | |
conn.Open(); | |
Console.WriteLine("Success."); | |
} | |
catch | |
{ | |
Console.WriteLine("Fail."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment