Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Created January 15, 2014 12:19
Show Gist options
  • Save KentaYamada/8435223 to your computer and use it in GitHub Desktop.
Save KentaYamada/8435223 to your computer and use it in GitHub Desktop.
SQLite:DBファイル作成→ファイルパスを指定してDB接続する
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