2011年12月13日 星期二

[.Net] Create SQLite database and datatable with .net framework

This is sample code for creating a new database file and table in SQLite.

            string m_dataSource = "test1.s3db";
            SQLiteConnection.CreateFile(m_dataSource);
            DbProviderFactory factory = SQLiteFactory.Instance;
            using (DbConnection conn = factory.CreateConnection())
            {
                // Create DB
                conn.ConnectionString = "Data Source=" + m_dataSource;
                conn.Open();
                // Create Table
                string sql = "create table [aa] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE)";
                DbCommand cmd = conn.CreateCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }



Reference:
http://hirocat.pixnet.net/blog/post/40514692-%E5%A6%82%E4%BD%95%E9%AB%98%E6%95%88%E4%BD%BF%E7%94%A8sqlite-.net-(c%23)


沒有留言:

張貼留言