前提・実現したいこと
C#初学者です。
windowsformとSQLserver、C#を使ったユーザーマスタメンテの画面を作成しています。
下記③番の条件にて入力された、ユーザーIDとサーバーに登録されたIDを比較するコード書けず止まっています。
①ユーザーID(6桁の整数)を入力し、
②「入力完了(btnFinish_Click)」をクリックすると、
③サーバーにそのIDが登録されていなければ
④ユーザー名、パスワードを入力し登録する。
という流れです。
お力添えいただけますと幸いです。
発生している問題・エラーメッセージ
該当のソースコード
SQLserver
USER_ID USER_NAME PASSWORD ADMIN_FLAG CRT_DAY CRT_TIME 1 tanaka aaa NULL NULL NULL 2 yamada bbb NULL NULL NULL
C#
using System; using System.ComponentModel; using System.Data.SqlClient; using System.Text; using System.Windows.Forms; namespace user_registration { public partial class Form1 : Form { public Form1() { InitializeComponent(); timer1.Start(); ActiveControl = textBox1; MessageBox.Show("ORG0001 START"); } private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.F9: btnRevocation.Focus(); btnRevocation.PerformClick(); break; case Keys.F10: btnClosing.Focus(); btnClosing.PerformClick(); break; case Keys.F12: btnFinish.Focus(); btnFinish.PerformClick(); break; default: return; } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { KeyPreview = true; } private void label1_Click(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { DateTime datetime = DateTime.Now; lblDate.Text = datetime.ToString("yyyy/MM/dd HH:mm:ss"); } private void textBox3_TextChanged(object sender, EventArgs e) { } private void groupBox2_Enter(object sender, EventArgs e) { } private void groupBox1_Enter(object sender, EventArgs e) { } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { } private void btnDeletion_Click(object sender, EventArgs e) { if (body.Enabled == false) { textBox1.Clear(); } if (header.Enabled == false) { if (!String.IsNullOrEmpty(textBox2.Text)|| !String.IsNullOrEmpty(textBox3.Text)) { textBox2.Clear(); textBox3.Clear(); checkBox1.Checked = false; } else { checkBox1.Checked = false; header.Enabled = true; body.Enabled = false; } } } private void btnClosing_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(textBox1.Text) && String.IsNullOrEmpty(textBox2.Text) && String.IsNullOrEmpty(textBox3.Text)) { Application.Exit(); } else { DialogResult result = MessageBox.Show("入力中の内容は破棄されます。\nよろしいですか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (result == DialogResult.Yes) { MessageBox.Show("ORG0001 END"); Application.Exit(); } } } private void btnFinish_Click(object sender, EventArgs e) { try { if (header.Enabled == true) { if (String.IsNullOrEmpty(textBox1.Text)) { MessageBox.Show("ユーザーコードを入力してください。"); ActiveControl = textBox1; return; } } if (textBox1.Text.IndexOf(' ') > -1 && this.ActiveControl != null) { MessageBox.Show("不正な文字列が含まれています"); ActiveControl = textBox1; return; } body.Enabled = true; header.Enabled = false; // ここから下のコードがSQL文の記載箇所です。 SqlConnection connection = new SqlConnection(Properties.Settings.Default.sqlServer); connection.Open(); MessageBox.Show("接続に成功しました"); StringBuilder sql = new StringBuilder(); sql.AppendLine("SELECT USER_ID"); sql.AppendLine("FROM USER_REGISTRATION"); sql.AppendLine("WHERE USER_ID = 1"); SqlCommand command = new SqlCommand(sql.ToString(), connection); SqlDataReader dr = command.ExecuteReader(); //while (dr.Read()) //{ // for (int i = 0; i < dr.FieldCount; i++) // { // MessageBox.Show(dr[i].ToString()); // } //} //builder.DataSource = "localhost\\SQLEXPRESS"; //builder.UserID = "test"; //builder.Password = "test"; //builder.InitialCatalog = "master"; //builder.IntegratedSecurity = true; //using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) //{ // connection.Open(); // MessageBox.Show("接続に成功しました"); // SqlCommand command = new SqlCommand(); //} } catch (SqlException ex) { MessageBox.Show(ex.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
まだ回答がついていません
会員登録して回答してみよう