サンプル集  >  C#  >  SQLCeB
SQLCeB
2014/04/10

SQLCe で作成したmyTblを条件指定して検索し内容を表示します。

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

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

[コンソール アプリケーション]を選択し、名前に「SQLCeTest3」と入力し「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: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;

namespace SQLCeTest3
{
    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();

            SqlCeCommand cmd = con.CreateCommand();

            cmd.CommandText
            = "SELECT uid"
            +      ", data"
            +  " FROM myTbl"
            + " WHERE uid=@uid"
            ;

            SqlCeParameter param = null;

            param = new SqlCeParameter( "@uid"
                                      , SqlDbType.BigInt
                                      );
            cmd.Parameters.Add( param );

            cmd.Parameters[0].Value = 2;

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

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

            con.Close();

            Console.ReadLine();
        }
    }
}

実行します。

2:DATA

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

▲ PageTop  ■ Home


Copyright (C) 2014 ymlib.com