//using System.Data.OleDb;
OleDbConnection myConn;
DataView dbRec;
private void gridtst_Load(object sender, EventArgs e)
{ // my document path
string MDB = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\db.mdb";
try
{ //OleDbConnection
myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + MDB);
myConn.Open();
//DB Set
DataSet dSet = new DataSet("DataBase");
OleDbDataAdapter dAdapt = new OleDbDataAdapter("select * from TBL", myConn);
dAdapt.Fill(dSet);
DataTable dTab = dSet.Tables[0];
//Data Source
dbRec = new DataView(dTab);
//grid
GD.DataSource = dbRec;
GD.Columns[0].Width = 40;
GD.Columns[1].HeaderText = "BB";
//close
myConn.Close();
}
catch (Exception ex)
{ //Error Message
MessageBox.Show(ex.ToString());
}
}
月別アーカイブ: 2008年6月
MDB接続
using System.Data.OleDb;
//利用範囲が広い場合、グローバル変数で定義する
OleDbConnection myConn;
DataView dbRec;
// 1) SQL生成 strSQL(実行SQL)
// 2) MDB接続 myConn(接続情報)
// 3) SQL実行 dbRecへ格納
// 4) 取得結果利用
// 5) MDB終了
// —————————————– main
public void GetData(String KEY)
{
//1) SQL生成 strSQL(実行SQL)
//SQL Query
String strSQL = “SELECT * FROM table WHERE index=’” + KEY + “‘”;
//2) MDB接続 myConn(接続情報)
//DB OPen
this.dbConn();
//3) SQL実行 dbRecへ格納
//Data Source
this.dbSet(strSQL);
//4) 取得結果利用
lblxxx.Text = dbRec[0].Row["field1"].ToString();
txtxxx.Text = dbRec[0].Row[2].ToString();
//フィールド番号指定時は最初を0として数える
//eof
if ( dbRec.Count == 0)
{
}
//ex:Loop処理
int intCNT= 0;
while (intCNT < dbRec.Count) {
lblxxx.Text = dbRec[ intCNT ].Row["field1"].ToString();
txtxxx.Text = dbRec[ intCNT ].Row["field2"].ToString();
intCNT++;
}
//5) MDB終了
//DB Close
this.dbClose();
}
// UPDATE,INSERT,DELETE実行
// —————————————– DB execute
public void dbExec(String strSQL)
{
//Execute SQL
OleDbCommand EXEC = new OleDbCommand(strSQL, myConn);
try {
EXEC.ExecuteNonQuery();
}
catch(OleDbException Ex) {
Console.Write(Ex.ToString());
}
catch(Exception Ea) {
throw Ea;
}
}
// —————————————– DB connect
public void dbConn()
{
try {
String sConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0; Data Source=xvax.mdb”;
//OleDbConnection
myConn = new OleDbConnection(sConnectionString);
//Connection Open
myConn.Open();
}
catch (Exception ex) {
//Error Message
MessageBox.Show(ex.ToString());
}
}
// —————————————– Get data from MDB
public void dbSet(String strSQL)
{
//DB Set
DataSet dSet = new DataSet(“DataBase”);
DataTable dTab = null;
OleDbDataAdapter dAdapt = new OleDbDataAdapter(strSQL, myConn);
dAdapt.Fill(dSet);
dTab = dSet.Tables[0];
//Data Source
dbRec = new DataView(dTab);
}
// —————————————– DB Close
public void dbClose()
{
myConn.Close();
}
指定ファイルを該当アプリで開く
//引数のパスを該当アプリで開く(xls,docなど)
private void OpenDirectFile(String FullPath) {
//ファイル存在チェック
if (File.Exists(FullPath) != true) {
MessageBox.Show(FullPath + ” ファイルが見つかりません”);
} else {
try {
//拡張子に関連付いたアプリで開く
System.Diagnostics.Process.Start(FullPath);
}
catch (Exception eX) {
MessageBox.Show(FullPath + “\n” + eX.ToString());
}
}
}
ファイル操作
//必須
using System.IO;
//exeファイルをネットワークからMyDocument内へコピーする
string copyFrom = @“\\server\\work\\file.exe";
string copyTO = “c:\\file.exe";
File.Copy(copyFrom, copyTO);
//ファイル削除
File.Delete(copyTO);
//ディレクトリ削除
Directory.Delete(dirMyDoc);
//ファイル存在チェック
String FNAME = “xvax.ini";
if (File.Exists(FNAME) != true) {
MessageBox.Show(“File not Found “, “File not Found");
return;
}
//ファイル読み込み
String line;
StreamReader sr = new StreamReader(FNAME);
//1行だけ読み込む
line = sr.ReadLine();
//ファイル書き込み
using (StreamWriter sw = File.CreateText(“書き込み対象ファイル+パス")) {
sw.WriteLine(“test,test");
//File Close
sw.Close();
}
メッセージボックス
//ノーマル
MessageBox.Show(“メッセージ", “タイトル(省略可)");
//Yes,No判定
if (MessageBox.Show(“よろしいですか?", “判定", MessageBoxButtons.YesNo) != DialogResult.No) {
textBox1.Text = “Yes";
else
textBox1.Text = “No!";
}
環境情報取得
//MyDocument内のパス取得
string dirMyDoc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + “\\xvax";
//パス上にディレクトリ存在しない場合
if ( Directory.Exists(dirMyDoc) == false ) {
//ディレクトリ作成
Directory.CreateDirectory( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + “\\xvax" );
}
//NT LOGIN Name
using System.Runtime.InteropServices;
[DllImport("Advapi32.dll", EntryPoint = "GetUserName", ExactSpelling = false, SetLastError = true)]
static extern bool GetUserName([MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer, [MarshalAs(UnmanagedType.LPArray)] Int32[] nSize);
byte[] str = new byte[256];
Int32[] len = new Int32[1];
len[0] = 256;
GetUserName(str, len);
//ex
lblLogName.Text = System.Text.Encoding.ASCII.GetString(str);
指定セル内の指定文字列を赤色に変更
Sub SEARCH_WORD() Dim chkVAL As String, FX As Integer chkVAL = InputBox(“Keyword(該当文字列を赤色にします)", “SEARCH") If chkVAL = “" Then Exit Sub For Each c In Selection FX = InStr(1, c.Value, chkVAL) If FX > 0 Then c.Characters(Start:=FX, Length:=Len(chkVAL)).Font.ColorIndex = 3 Next End Sub