###前提・実現したいこと
今単語帳アプリを作成しています。
DBクラスでDBへの接続、値の取得をして、別のクラスに引数として値を渡し、最終的にはテキストボックスに表示させたいと考えています。
DBUtilsというDB接続用クラスを作成し、スタートボタンクリックイベントで値(単語)を取得し、RichTextBoxに単語を表示。「解答を見る」ボタンがクリックされたら単語に紐づく意味をもう一つのRichTextBoxに表示させようとしています。
PostgreSQL使用はマストです。
おそらくですが接続はできていると思います。あとは値をどう取得してTextBoxに表示すべきかが問題です。
どのようにすべきかアドバイスをください。
###発生している問題・エラーメッセージ
エラーメッセージ
###該当のソースコード
C#
using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WordCards { class DBUtils { public string DBConnection(string vocab) { string conStr = "Server = localhost; Port = 5432; User Id = postgres; Password = password; Database = vocab"; NpgsqlConnection conn = new NpgsqlConnection(conStr); try { conn.Open(); //接続オープン var cmd = new NpgsqlCommand(@"select * from vocabschem.t_vocab", conn); //SQL生成し、DBへ var dataReader = cmd.ExecuteReader();//結果を取得 var datas = dataReader;//取得したデータをそしてデータを別のクラスに渡したい。 vocab = datas.ToString(); return vocab; } catch (Exception e) { } conn.Close(); } } } 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 WordCards { public partial class VocabForm : Form { public VocabForm() { InitializeComponent(); } private int sec = 10; string word; private void StartBtn_Click(object sender, EventArgs e) { if (!RandomRadioBtn.Checked && !OrderRadioBtn.Checked) { MessageBox.Show("表示形式を選択してください。"); } /*else if(CategoryComboBox.Text == "カテゴリ選択") { MessageBox.Show("カテゴリ選択をしてください"); }*/ else { //スタートボタンクリック時に非表示・表示を設定 StartBtn.Visible = false; RandomRadioBtn.Visible = false; OrderRadioBtn.Visible = false; TimerRadioBtn.Visible = false; CategoryComboBox.Visible = false; CategGroupBox.Visible = false; WordLabel.Visible = true; MeaningLabel.Visible = true; WordsRichTxtBox.Visible = true; MeanRichTxtBox.Visible = true; BackBtn.Visible = true; ShowAnsBtn.Visible = true; NextBtn.Visible = true; CountDownTimerLabel.Visible = true; if (TimerRadioBtn.Checked) { CDTimer.Start(); } public string DBConnection(vocabulary) } } private void ExitBtn_Click(object sender, EventArgs e) { Application.Exit(); } private void AddWordsMenuItem_Click(object sender, EventArgs e) { AddWordsForm AddWordsForm = new AddWordsForm(); AddWordsForm.Show(); } private void CDTimer_Tick(object sender, EventArgs e) { if(sec > 0) { sec = sec - 1; CountDownTimerLabel.Text = "残り" + sec + "秒"; } } } }
###試したこと
いろいろと調べましたが、なかなか進みません
###補足情報(言語/FW/ツール等のバージョンなど)
使用言語 C#
使用ツール Visual Studio2015(Windows FormApplication) and PostgreSQL 9.3
DB接続はNpgsqlです
OSはWindows 10です
ConsoleApplicationで一度DB接続を練習したときは(おそらくですが)動きました。
回答1件