2008年5月27日火曜日

SQL Server データベースの作り方

SQL Server データベースの作り方

■以下のソフトがインストールされていることを確認する。なければインストールする。
スタート -> すべてのプログラム -> Microsoft SQL Server 2005 を確認する。
1) SQL構成マネージャー(SQL Server Configuration Manager)
2) Microsoft SQL Server Management Studio Express

■SQL Serverの認証を許可する
1)SQL構成マネージャー(SQL Server Configuration Manager)を起動する。
2)SQL Server 2005 ネットワークの構成 -> SQLEXPRESS のプロトコル -> 名前付きパイプ を「有効」にする

■データベースを作成する
1)Microsoft SQL Server Management Studio Express を起動する。
2)[新しいクエリ]で 以下を貼り付けて実行する。
------ 貼り付けるテキスト ------
CREATE DATABASE [TestDB] ON (NAME = 'TestDB_Data', FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\TestDB_Data.MDF') LOG ON (NAME = 'TestDB_Log', FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\TestDB_Log.LDF')
GO

use [TestDB]
GO

CREATE TABLE [dbo].[顧客] (
[ID] [int] PRIMARY KEY ,
[姓] [ntext] ,
[名] [ntext] ,
[姓かな] [ntext] ,
[名かな] [ntext] ,
[性別] [int] ,
[自宅パソコンメール] [bit] ,
[携帯メール] [bit] ,
[職場パソコンメール] [bit] ,
[その他メール] [bit] ,
[その他メールの説明] [ntext] ,
[メールアドレス] [ntext] ,
[誕生日] [datetime] ,
[電話番号] [ntext]
)
GO

CREATE TABLE [dbo].[性別] (
[ID] [int] PRIMARY KEY ,
[名称] [ntext]
)
GO
------ 貼り付けるテキスト ------

2008年5月17日土曜日

VC++2005/2008 get() set() を property で実現する

VC++2005/2008 get() set() を property で実現する

クラスのメンバ変数をアクセスするのに get() set() メソッドを用意するのは一般的ですが、property を利用すればすっきり記述できます。

■property を使ったクラス
public ref class pro
{
private: Byte tbl_; // プライベート宣言
public: property Byte tbl // get() set() の tblパブリック宣言
{
void set(Byte val)
{
tbl_ = val;
}
Byte get()
{
return tbl_;
}
}
};
■呼出側
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
pro^ pp = gcnew pro();
pp->tbl = 10;
}

VC++2005/2008 配列を array でマネージ化

VC++2005/2008 配列を array でマネージ化

配列をマネージ化したとき以下のようになります。

■ネイティブ
unsigned char aaa[3];
aaa[0] = 1;
aaa[1] = 2;
aaa[2] = 3;
unsigned char bbb[3];
memcpy( bbb , aaa , sizeof(bbb) );

■マネージ
array<Byte>^ aaa = gcnew array<Byte>(3);
aaa[0] = 1;
aaa[1] = 2;
aaa[2] = 3;
array<Byte>^ bbb = gcnew array<Byte>(3);
aaa->CopyTo(bbb, 0);

■ちなみに文字数を調べるには sizeof() 以下のようにします。

Int32 len = aaa->Length;

2008年5月15日木曜日

VC++2005/2008 マネージ 線形リスト クラス

VC++2005/2008 マネージ 線形リスト クラス

マネージで線形リストが扱えるクラス(List.h)を作りました。マネージでは new の代わりに gcnew を使用するため、関数のスコープが外れたときでも gcnew で確保した内容が消えないようにしなければなりません。
以下は、線形リストで追加と削除ができるクラスです。先入れ先出のみの対応で、リストの真ん中に対して挿入・削除は対応しておりません。

**** List.h ****
#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

///------------------------------------------------
/// ■リストタイプクラスの先頭
///------------------------------------------------
ref class CListType
{
public: Int32 no; // リスト番号
public: array<unsigned char>^ buff; // 格納データ
public: Int32 len; // 文字数
public: CListType^ next; // 次の要素へのポインタ
///------------------------------------------------
/// コンストラクタ
///------------------------------------------------
public: CListType(Int32 length)
{
buff = gcnew array<unsigned char>(length);
len = length;
next = nullptr;
}
///------------------------------------------------
}; // ■クラスの最後
///------------------------------------------------


///------------------------------------------------
/// ■リストクラスの先頭
///------------------------------------------------
ref class CList
{
///------------------------------------------------
/// テーブル宣言
///------------------------------------------------
public: CListType^ head; // リストの先頭要素(ダミー)
public: CListType^ p;
public: CListType^ q;
public: CListType^ newcell;
public: CListType^ output;
public: Int32 No;
///------------------------------------------------
/// コンストラクタ
///------------------------------------------------
public: CList()
{
head = gcnew CListType(1); // リストヘッダー宣言
No = 0;
}
///------------------------------------------------
/// 追加
///------------------------------------------------
public: bool addlist(CListType^ input)
{
p = head->next; // 先頭要素の次の要素のアドレス
q = head; // 先頭要素のアドレス
while( p != nullptr )
{
q = p; // 追加位置の直前の要素のnextを後で設定するために、追加位置の直前の要素のアドレスを記憶しておく
p = p->next; // 次の要素へ進む
}

newcell = gcnew CListType(input->len); // 新しく追加する要素のためのメモリ領域を確保する
if( newcell == nullptr ){
return false; // メモリ不足
}

input->no = ++No;
for(Int32 ii=0; ii<(input->len); ii++){
newcell->buff[ii] = input->buff[ii]; // 新しい要素のデータを設定
}
newcell->len = input->len; // 文字数設定
newcell->next = p; // 新しい要素の次の要素へのアドレスを設定
q->next = newcell; // 新しい要素の直前の要素のnextに、新しい要素のアドレスを設定
return true;
}
///------------------------------------------------
/// 削除
///------------------------------------------------
public: CListType^ deletelist(void)
{
p = head->next; // 先頭要素の次の要素のアドレス
q = head; // 先頭要素のアドレス

if( p == nullptr ){ // リストの末尾
return nullptr;
}
else{
output = gcnew CListType(p->len); // 取出領域確保
output->no = --No;
for(Int32 ii=0; ii<(p->len); ii++){
output->buff[ii] = p->buff[ii]; // 新しい要素のデータを設定
}
output->len = p->len;

q->next = p->next; // 削除する要素の直前の要素のnextポインタを再設定
delete p;
}
return output;
}

///------------------------------------------------
}; // ■クラスの最後
///------------------------------------------------

2008年5月7日水曜日

System::String型で mid() に相当する Substring() の使い方

System::String型で mid() に相当する Substring() の使い方

CString型の mid() に相当するのが Substring() です。取得失敗に備えて try - catch で覆ったほうがよいでしょう。

System::String^ textString = "abcde";
System::String^ moji;
System::String^ err = "";

try
{
moji = textString->Substring(1,1);
}
catch(Exception^ e)
{
err = System::String::Format("{0}",e);
}

2008年4月24日木曜日

VC++2005/2008 コンボボックスの使用方法

VC++2005/2008 コンボボックスの使用方法
コンボボックスはテキストボックスとリストボックスを組合わせた便利なコンポーネントです。おもな使用方法を示します。

array<System::String^>^ arr = gcnew array<System::String^>{"bbb","ccc"}; // 配列宣言

comboBox1->Items->Clear(); // コンボボックスのアイテムクリア
comboBox1->Items->Add( "aaa" ); // コンボボックスにアイテム1つ追加
comboBox1->Items->AddRange( arr ); // コンボボックスにアイテムまとめて追加
comboBox1->Text = String::Format("{0} : {1}",comboBox1->Items->Count , comboBox1->Items[0] ); // コンボボックスのテキスト表示

2008年4月23日水曜日

VC++2005/2008 .NET SerialPort を使用する方法

VC++2005/2008 .NET SerialPort を使用する方法

.NET の SerialPort を使用してシリアル送受信する方法を示します。
SerialPort はそのまま使うよりクラス(CUartクラス)にラップするほうがすっきりします。
シリアル受信はスレッドで行い、受信データをデリゲート渡しにしてテキストボックスに表示します。

■■ Uart.h (CUartクラス) ■■■■■■■■■■■■■■■■■■■■■■■
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO::Ports; // シリアルポート

#define BUF_SIZE 10

ref class CUart
{
///------------------------------------------------
/// テーブル宣言
///------------------------------------------------
public: SerialPort^ port;
public: System::String^ nowPort;

///------------------------------------------------
/// コンストラクタ
///------------------------------------------------
public:CUart()
{
nowPort = "----"; // 現在ポート名クリア
port = gcnew SerialPort(); // シリアルポートインスタンス化
}
///------------------------------------------------
/// オープン可能ポート調査
///------------------------------------------------
public: System::String^ portInvestigation()
{
String^ na = nowPort;
if( na != "----" ){ // 現在ポートオープン中のとき
port->Close(); // ポートクローズ
}
array<String^>^ ports = SerialPort::GetPortNames(); // 全ポート名取得
ports->Sort( ports ); // 並び替え
char ct = ports->Length::get();

array<String^>^ names = gcnew array<String^>(ct); // 利用可能なポート名取得
char cnt = 0;
for(char ii=0; ii<ct; ii++)
{
if( portOpenTest( ports[ii] ) == true ){ // オープンに成功したとき
names[cnt++] = ports[ii]; // ポート名取得
}
}

char flg = 0;
if( nowPort == "----" ){
flg = 1;
}
for(char ii=0; ii<cnt; ii++)
{
if( names[ii] == nowPort ){ // 現在のポートと同じ時
flg = 1;
}
else{
if( flg == 1 ){
nowPort = names[ii];
break;
}
}
}

if( na == nowPort ){
nowPort = "----";
}
return nowPort;
}
///------------------------------------------------
/// ポートオープンテスト
///------------------------------------------------
private: bool portOpenTest( System::String^ name )
{
port->Close();
bool result = portOpen( name );
port->Close();
return result;
}
///------------------------------------------------
/// ポートオープン
/// SerialPort port = new SerialPort(COM4, 9600, Parity.None, 8, StopBits.One);
///------------------------------------------------
public: bool portOpen( System::String^ name )
{
System::String^ err = "";
port->PortName = name; // ポート名設定
port->BaudRate = 9600;
port->Parity = Parity::None;
port->StopBits = StopBits::One;
try
{
port->Open(); // ポートオープン
}
catch(Exception^ e)
{
err = System::String::Format("{0}",e);
}
if( err == "" ){
return true;
}
else{
return false;
}
}
///------------------------------------------------
/// 送信
///------------------------------------------------
public: void write( array<unsigned char>^ buffer, int offset, int count )
{
port->Write( buffer , offset , count );
}
///------------------------------------------------
/// 受信
///------------------------------------------------
public: void read( array<unsigned char>^ buffer, int offset, int count )
{
System::String^ err = "";
try
{
port->Read( buffer , offset , count );
}
catch(Exception^ e)
{
err = System::String::Format("{0}",e);
}
}
///------------------------------------------------
/// オープンしているかどうか
///------------------------------------------------
public: bool isOpen()
{
return port->IsOpen::get();
}
///------------------------------------------------
};
■■ Form1.h ■■■■■■■■■■■■■■■■■■■■■■■
※必要な部分だけを記載しています。
///------------------------------------------
/// ■インクリュード宣言
///------------------------------------------
#include "Uart.h"
///------------------------------------------
/// ■変数宣言
///------------------------------------------
private: CUart^ uart;
private: array<unsigned char>^ buff;
private: bool RxLoopFlg;
///------------------------------------------
/// ■ボタン1
/// シリアルポート設定(変更)
///------------------------------------------
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ com = uart->portInvestigation();
if( com == "----" ){
textBox1->Text = com;
RxLoopFlg = false;
}
else{
if( uart->portOpen(com) == true ){
textBox1->Text = com;
RxLoopFlg = true;
}
else{
textBox1->Text = "失敗";
RxLoopFlg = false;
}
}
}
///------------------------------------------
/// ■ボタン2
/// シリアル送信(適当な値を送信)
///------------------------------------------
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
array<unsigned char>^ buff = gcnew array<unsigned char>(BUF_SIZE);
for(char ii=0 ; ii<BUF_SIZE; ii++){
buff[ii] = 0x30 + ii;
}
uart->write( buff , 0 , BUF_SIZE);
}
///------------------------------------------
/// ■フォームロード
///------------------------------------------
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
RxLoopFlg = false;
buff = gcnew array<unsigned char>(BUF_SIZE); // シリアル受信バッファ領域宣言
buff->Clear(buff,0,BUF_SIZE); // シリアル受信バッファクリア
uart = gcnew CUart(); // シリアルポートインスタンス
button1_Click(sender,e); // シリアルポート設定(変更)
backgroundWorker1->RunWorkerAsync(); // スレッド開始(シリアル受信)
}
///------------------------------------------
/// ■デリゲート
/// ■テキストボックスセット
///------------------------------------------
delegate void SetTextCallback(array<unsigned char>^ buff);
private: void SetText_A(array<unsigned char>^ buff){
if( this->textBox2->InvokeRequired ){
SetTextCallback^ d = gcnew SetTextCallback(this, &at3200::Form1::SetText_A);
this->BeginInvoke( d, buff );
}
else{
unsigned char bf[BUF_SIZE];
for(char ii=0; ii<BUF_SIZE; ii++){
bf[ii] = buff[ii];
}
buff->Clear(buff,0,BUF_SIZE); // シリアル受信バッファクリア
// this->textBox2->Text += System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)bf);
this->textBox2->AppendText(System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)bf));
}
}
///------------------------------------------
/// ■スレッド1
///------------------------------------------
private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
while(1){
if( RxLoopFlg == true ){
if( uart->isOpen() == true ){
uart->read( buff , 0 , BUF_SIZE ); // シリアル受信
SetText_A( buff );
}
else{
}
}
else{
}
}
}
■■ Form1.h の終わり ■■■■■■■■■■■■■■■■■■