IT Staff

Blog về chuyên ngành IT

Archive for the ‘NuoDB’ Category

Thao tác sql với NuoDB bằng C# .NET Console Application

leave a comment »

– Cách tốt nhất để biết nó hay hoặc dở là sử dụng nó. –

Ở bài trước, mình đã cài đặt thành công NuoDB và đã tương tác với NuoDB ODBC Driver rồi. Do đó, mình đã chắc chắn driver đã được cài đặt và chạy tốt. Nhưng mình sẽ không làm thủ công để tạo DSNs cho NuoDB domain nữa, bạn có thể tham khảo và sử dụng cách thêm DSN-names tự động ở đây.
Mở Visual Studio > Create New Project > trong Project types, chọn Visual C# > trong templates, chọn Console Application > OK.

– Tạo một DSN tên là TestChorusDSN với uid=admin, pwd=admin và database=testchorus@localhost trên máy cục bộ.

– Mở kết nối đến TestChorusDSN vừa tạo, tạo một table có tên ngẫu nhiên là: user.test + DateTime.utcnow.ticks. Có 2 field id và name.

– Insert dữ liệu vào table vừa tạo.

– Select dữ liệu của table vừa tạo.

– Xóa DSN TestChorusDSN.

Source code của chương trình chính như sau:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.Odbc;

using System.Data;

 

namespace ConsoleApplication1

{

    class Program

    {

        [System.Runtime.InteropServices.DllImport(“ODBCCP32.dll”)]

        static extern bool SQLConfigDataSource(IntPtr parent, int request, string driver, string attributes);

        ///<summary>

        /// Tạo và xóa DSNs

        ///</summary>

        ///<param name=”remove”></param>

        ///<returns></returns>

        static bool HandleDSN(bool remove)

        {

            return SQLConfigDataSource(IntPtr.Zero

                , remove ? /* ODBC_REMOVE_DSN */ 3 : /* ODBC_ADD_DSN */ 1

                , “NuoDB ODBC Driver”

                , “DSN=TestChorusDSNUID=adminPWD=adminDatabase=TestChorus@localhost”);

 

        }

 

        static void Main(string[] args)

        {

            const string Separator = “\t|\t”;

 

            Console.WriteLine(“Adding DSN…”);

            HandleDSN(false);

 

            var csb = new OdbcConnectionStringBuilder();

            csb.Dsn = “TestChorusDSN”;

            using (var conn = new OdbcConnection(csb.ToString()))

            {

                Console.WriteLine(“Opening connection…”);

                conn.Open();

                Console.WriteLine(“Starting transaction…”);

                using (var tx = conn.BeginTransaction())

                {

                    string tableName = string.Format(“user.test{0}”, DateTime.UtcNow.Ticks);

                    using (var cmd = conn.CreateCommand())

                    {

                        cmd.Transaction = tx;

                        cmd.CommandText = string.Format(“create table {0} (id int primary key, name string not null)”, tableName);

                        Console.WriteLine(“Creating table…”);

                        cmd.ExecuteNonQuery();

                    }

                    using (var cmd = conn.CreateCommand())

                    {

                        cmd.Transaction = tx;

                        cmd.CommandText = string.Format(“insert into {0}(id, name) values (?, ?)”, tableName);

                        cmd.Prepare();

                        IDbDataParameter parameter;

                        for (var i = 0; i < 10; i++)

                        {

                            cmd.Parameters.Clear();

 

                            parameter = cmd.CreateParameter();

                            parameter.Value = i;

                            cmd.Parameters.Add(parameter);

 

                            parameter = cmd.CreateParameter();

                            parameter.Value = string.Format(“value of {0}”, i);

                            cmd.Parameters.Add(parameter);

 

                            Console.WriteLine(“Inserting…”);

                            cmd.ExecuteNonQuery();

                        }

                    }

 

 

                    using (var cmd = conn.CreateCommand())

                    {

                        cmd.Transaction = tx;

                        cmd.CommandText = string.Format(“select * from {0}”, tableName);

                        Console.WriteLine(“Reading data…”);

                        using (var reader = cmd.ExecuteReader())

                        {

                            Console.WriteLine(string.Join(Separator, Enumerable.Range(0, reader.FieldCount).Select(x => reader.GetName(x)).ToArray<string>()));

                            while (reader.Read())

                            {

                                var values = new object[reader.FieldCount];

                                reader.GetValues(values);

 

                                Console.WriteLine(string.Join(Separator, values.Select(row => row.ToString()).ToArray<string>()));

                            }

                        }

                    }

                    Console.WriteLine(“Committing…”);

                    tx.Commit();

                }

            }

 

            Console.WriteLine(“Removing DSN…”);

            HandleDSN(true);

        }

 

    }

}

Bạn có thể comment để trao đổi kinh nghiệm. Cám ơn bạn đã quan tâm đến bài viết.

Tài liệu tham khảo:

http://www.cs.brown.edu/courses/cs227/slides/dtxn/nuodb.pdf [ NuoDB introduction ]

http://ww1.prweb.com/prfiles/2011/08/22/8738428/NuoDB%20White%20Paper%20v310.pdf [ NuoDB White paper v310 ]

http://blog.cincura.net/232841-using-nuodb-from-net [ using NuoDB from net ]

http://blog.sqlauthority.com/2012/07/31/sql-server-beginning-nuodb-who-will-benefit-and-how-to-start/#comment-325934 [ begining NuoDB at sqlautority blog ]

http://sqlandsiva.blogspot.com/2011/11/nuodb-acid-compliant-scalable-cloud_02.html [ NuoDB – ACID Compliant, Scalable Cloud Database ]

nuodb-acid-compliant-scalable-cloud

Written by Xavier

July 11, 2014 at 3:08 am

Posted in C#, Database, Lap trinh, NuoDB, Staff

Tagged with , , , ,

NUODB :: New DB solution

leave a comment »

* Giới thiệu

Hiện tại NuoDB đang ở bản beta 7 và đã thu hút nhiều cộng đồng tải về dùng thử. Nếu bạn thường làm việc trên các dự án có quy mô đòi hỏi database hỗ trợ co giãn lớn thì bạn nên thử phần mềm này hoặc bạn cần migrate một ứng dụng lên cloud thì NuoDB sẽ rất có ích cho bạn.

NuoDB được giới thiệu là:

– 100% SQL

– 100% ACID

– 100% Elastically Scalable => bạn có thể thêm vào nhiều máy để chạy một database, tức khắc nó sẽ nhanh hơn, nếu bạn gở bỏ chúng hoặc khi chúng bị lỗi thì bạn cũng không hề mất dữ liệu của mình. Do đó bạn không cần phải  sharding, caches hoặc replication…

Hiện tại bản beta là free for test, còn sau  này thì vẫn chưa biết cơ chế terms of price của nó như thế nào, được biết là nó free forever cho những hệ thống nhỏ và chỉ tính phí cơ bản trên các hệ thống lớn. Nhưng nếu nó quá mắc thì có lẽ tôi sẽ chọn giải pháp DynamoDB NoSQL.

* Download

Bạn có thể tải về bản beta7 của NuoDB dành cho từng platform ở đây:

Fill out a short form  figure

Fill out a short form

 

 

 

 

 

 

 

 

 

Sau khi điền đầy đủ thông tin cần thiết, bạn sẽ nhận được một email chứa link download.

Bạn Accept Test Agreement, kế đến tải về bản cài đặt dành cho platform của bạn.

Accept Test Agrreement Figure

Accept Test Agrreement

 

 

 

 

 

 

 

 

 

 

Download beta 7 figure

Download beta 7

 

 

 

 

 

 

 

 

 

Tôi dùng Win7 32bit, cho nên tôi chọn NuoDB Version 1.0-Beta7-73-Windows-x86 (Beta) Setup

NuoDB cần phải có máy ảo java để chạy. Bạn có thể tải về bản cài đặt java ở đây: jre-7u5-windows-i586.exe

Sau khi cài đặt, bạn sẽ có cấu trúc thư mục NuoDB như sau:

NuoDB installed folder

NuoDB installed folder

 

 

 

 

 

 

 

NuoDB có 2 tài liệu dạng HTML và PDF. Bạn có thể tham khảo.

Bravo, nào chúng ta cùng trải nghiệm NuoDB.

PS:

– Cảm ơn bạn đã quan tâm.

– Bạn có thể comment để chia sẻ kinh nghiệm của mình.

Written by Xavier

July 11, 2014 at 3:04 am

Posted in Database, NuoDB, Staff