サンプル集  >  VC  >  SQLServerの検索
SQLServerのレコード検索
2012/11/22

SQLServerのレコードを検索し表示します。

◆環境
OS Windows XP Professional Version 2002 Service Pack 3
VC Microsoft Visual C++ 2005 77972-235-2482122-41077
DB SQLServer 2000 - 8.00.760 (Intel X86)

SQLServerのバージョンを確認します。 バージョンの確認は、osqlで行います。

>osql -Usa
パスワード :
1> select @@version
2> go



 ----------------------------------------------------------------------
------------------

        ---------------------------------------------------------------
------------------
 Microsoft SQL Server  2000 - 8.00.760 (Intel X86)
        Dec 17 2002 14:22:05
        Copyright (c) 1988-2003 Microsoft Corporation
        Desktop Engine on Windows NT 5.1 (Bui
        ld 2600: Service Pack 3)


(1 件処理されました)
1>

Visual Studioを起動し「Win32 コンソールアプリケーション」を作成します。

プロジェクトのプロパティを開き[全般]の[共通言語ランタイム サポート]を 「共通言語ランタイム サポート、古い構文(/clr:oldSyntax)」を選択します。

プログラムは以下になります。

SQLSvSelect.cpp
 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: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
88: 
89: 
90: 
91: 
92: 
93: 
94: 
95: 
96: 
97: 
98: 
// SQLSvSelect.cpp : コンソール アプリケーションのエントリ
// ポイントを定義します。
//

#include "stdafx.h"

#using <mscorlib.dll>
#include <tchar.h>
#using <System.Dll>
#using <System.Data.Dll>
#using <System.Xml.Dll>

using namespace System;
using namespace System::Data;
using namespace System::Xml;
using namespace System::Collections;
using namespace System::Data::SqlClient;

int _tmain( int argc, _TCHAR* argv[] )
{
    SqlConnection* conn;

    try
    {
        // データベースへ接続
        String* sConnStr;
        sConnStr = "Password=pass;"          \
                   "User ID=user;"           \
                   "Initial Catalog=dbname;" \
                   "Data Source=(local)";

        conn = new SqlConnection( sConnStr );
        conn->Open();
        Console::WriteLine( "Open OK!!" );

        // 検索
        SqlDataAdapter* adpt
        = new SqlDataAdapter(
            "SELECT CONVERT(varchar,door_nmbr,120)" \
                       " AS door_nmbr"              \
                 ", CONVERT(varchar,set_time,120)"  \
                       " AS set_time"               \
                 ", door_type"                      \
             " FROM door_mst"                       \
            " ORDER BY set_time"
          , conn
          );
        DataSet* ds = new DataSet( "DS" );
        adpt->FillSchema( ds, SchemaType::Source, "Data" );

        adpt->MissingSchemaAction
        = MissingSchemaAction::AddWithKey;
        adpt->Fill( ds, "Data" );

        // 検索結果を表示
        DataTable* tbl = ds->Tables->Item["Data"];
        IEnumerator* iEnum = tbl->Rows->GetEnumerator();
        while( iEnum->MoveNext() )
        {
            Console::WriteLine(
                "[{0}/{1}/{2}]"
              , dynamic_cast<String*>
                  ( dynamic_cast<DataRow *>
                      ( iEnum->Current )
                          ->get_Item( "door_nmbr" )
                  )
              , dynamic_cast<String*>
                  ( dynamic_cast<DataRow *>
                      ( iEnum->Current )
                          ->get_Item( "set_time" )
                  )
              , dynamic_cast<String*>
                  ( dynamic_cast<DataRow *>
                      ( iEnum->Current )
                          ->get_Item( "door_type" )
                  )
            );
            // get_Itemの引数に存在しないカラム名を指定すると
            // 以下のエラーが出る。
            //   列 'door_type2' はテーブル Authors に属して
            //   いません。
        }
    }
    catch ( Exception* ex )
    {
        Console::WriteLine( ex->Message );
    }
    __finally
    {
        conn->Close();
        Console::WriteLine( "Close." );
    }

    // ウィンドが消えるのを防止
    getchar();

    return 0;
}

文字列以外の値を表示しようとしても何も表示されなかったので、SQL文のCONVERTで文字列に変換しました。

Open OK!!
[1001/2012-11-21 17:33:42/R]
[1002/2012-11-22 14:45:12/L]
[2001/2012-11-22 14:46:01/R]
Close.

上手くいきました。

▲ PageTop  ■ Home


Copyright (C) 2012 ymlib.com