lunedì 4 novembre 2013

How To:SQLite create DB.

Sulla falsa riga del precedente post, ecco che vi allego una breve brano di codice per la creazione di db SQLite.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace SqlLiteTest.CreateDB
{
    class Program
    {
        static void Main(string[] args)
        {
            SQLiteDBHelpers sdH = new SQLiteDBHelpers("DBTest", @"\Data");
            sdH.CreateDBFile();

        }
    }


    public class SQLiteDBHelpers
    {
        private string _fName;
        private string _fPath;

        public SQLiteDBHelpers(string fName, string fPath)
        {
            _fName = fName;
            _fPath = fPath;
        }

        public void CreateDBFile()
        {
            VerifyPath(_fPath);

            if (!CheckName(Path.Combine( _fPath,_fName)))
            {
                SQLiteConnection.CreateFile(Path.Combine(_fPath, _fName) + ".sqlite");

                System.Data.SQLite.SQLiteConnection sc = 
                            new SQLiteConnection("DATA SOURCE ="+
                            Path.Combine(_fName, _fPath)+ ".sqlite;Version=3");

                sc.Open();

                string sqlMaster = "create table master "+ 
                        "(id int, tName varchar(40),tCreation datetime)";


                SQLiteCommand command = new SQLiteCommand(sqlMaster, sc);
                command.ExecuteNonQuery();

                sc.Close();
            }            
        }


        private void VerifyPath(string Path)
        {
            if (!Directory.Exists(Path))
            {
                Directory.CreateDirectory(Path);
            }
        }

        private bool CheckName(string Name)
        {
            return File.Exists(Name);
        }

    }
}


Poca roba, ma comunque utile.

Nessun commento: