サンプル集  >  C#  >  SQLCeA
SQLCeA
2014/04/08

SQLCe で作成したmyTblを検索し内容を表示します。

◆環境
OS Windows 7 Professional Service Pack 1 64bit
C# Microsoft Visual C# 2010 01018-587-4054044-70237

[ファイル]-[新規作成]-[プロジェクト]を選択。

[コンソール アプリケーション]を選択し、名前に「SQLCeTest2」と入力し「OK」。

プロジェクトを右クリックし「参照の追加」を選択。

System.Data.SqlServerCe を選択し「OK」ボタンを押下。

Program.cs
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;

namespace SQLCeTest2
{
    class Program
    {
        static void Main( string[] args )
        {
            string constr
              = @"Data Source='C:\\Temp\\SQLCeTestDB.sdf';"
              + " Persist Security Info=True;"
              + " Password=test"
              ;

            SqlCeConnection con = new SqlCeConnection( constr );

            con.Open();

            string sql
            = "SELECT uid, data FROM myTbl ORDER BY uid";

            SqlCeCommand cmd = new SqlCeCommand( sql, con );

            SqlCeDataReader rec = cmd.ExecuteReader();
            while ( rec.Read() == true )
            {
                long   uid  = ( long )rec["uid"];
                string data = ( string )rec["data"];

                Console.WriteLine( uid + ":" + data );
            }

            con.Close();

            Console.ReadLine();
        }
    }
}

実行します。

1:DATA
2:DATA
3:DATA

取得した内容が表示されました。

▲ PageTop  ■ Home


Copyright (C) 2014 ymlib.com