Created
December 15, 2012 09:10
-
-
Save cofearabi/4292287 to your computer and use it in GitHub Desktop.
(C#) display the value of field of the table in access mdb.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace cs_access | |
{ | |
public partial class Form1 : Form | |
{ | |
System.Data.OleDb.OleDbConnection conn; | |
DataTable dtUser; | |
private bool conectAcessDB(string strDbPath, string strPasswd, ref System.Data.OleDb.OleDbConnection con) | |
{ | |
// --- 接続文字列 | |
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="; | |
conString += strDbPath + ";Jet OLEDB:Database Password=" + strPasswd; | |
con = new System.Data.OleDb.OleDbConnection(conString); | |
try | |
{ | |
// --- DB Open | |
con.Open(); | |
// --- DB Close | |
con.Close(); | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
/// <summary> | |
// --------------------------------------------------------- | |
// * Function : getDatTableSQL | |
// * Operation : SQL文に従ってデータテーブルに取込む | |
// --------------------------------------------------------- | |
/// </summary> | |
private bool getDbTableSQL(string strSQL, ref System.Data.DataTable dt) | |
{ | |
bool ret = false; | |
if (strSQL != "") | |
{ | |
System.Data.OleDb.OleDbDataAdapter da = | |
new System.Data.OleDb.OleDbDataAdapter(strSQL, conn); | |
// --- DataTableに格納する | |
dt = new System.Data.DataTable(); | |
try | |
{ | |
da.Fill(dt); | |
ret = true; | |
} | |
catch | |
{ | |
string strTitle = "選択クエリー"; | |
string strPrompt = "クエリーに失敗しました。"; | |
MessageBox.Show(strPrompt, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); | |
} | |
} | |
return ret; | |
} | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
string strDBPath = @"C:\mdb\stock_mdb.mdb"; // --- データベースパス | |
string strDBPassword = ""; // --- データベースパスワード | |
// ---------------------------------------------------------------------- | |
string strSQL; // --- SQL文 | |
bool ret = false; | |
int i; | |
// --- DB接続 | |
ret = conectAcessDB(strDBPath, strDBPassword, ref conn); | |
strSQL = "SELECT meigara,namae FROM meigara; "; | |
// --- SQL文のデータテーブルを取得 | |
ret = getDbTableSQL(strSQL, ref dtUser); | |
if (dtUser.Rows.Count > 0) | |
{ | |
for (i = 0; i < dtUser.Rows.Count; i++) | |
{ | |
// --- ユーザー名コンボボックスへアイテム追加 | |
//cmbUserName.Items.Add(dtUser.Rows[i][0].ToString()); | |
MessageBox.Show(dtUser.Rows[i][1].ToString()); | |
} | |
} | |
// --- リソース解放 | |
dtUser.Dispose(); | |
conn.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment