前提・実現したいこと
C#初学者です。
windowsformとSQLserver、C#を使ったユーザーマスタメンテの画面を作成しています。
下記③番の条件にて入力された、ユーザーIDとサーバーに登録されたIDを比較するコード書けず止まっています。
①ユーザーID(6桁の整数)を入力し、
②「入力完了(btnFinish_Click)」をクリックすると、
③サーバーにそのIDが登録されていなければ
④ユーザー名、パスワードを入力し登録する。
という流れです。
お力添えいただけますと幸いです。
発生している問題・エラーメッセージ
該当のソースコード
SQLserver
1USER_ID USER_NAME PASSWORD ADMIN_FLAG CRT_DAY CRT_TIME 21 tanaka aaa NULL NULL NULL 32 yamada bbb NULL NULL NULL
C#
1 2using System; 3using System.ComponentModel; 4using System.Data.SqlClient; 5using System.Text; 6using System.Windows.Forms; 7 8namespace user_registration 9{ 10 public partial class Form1 : Form 11 { 12 public Form1() 13 { 14 InitializeComponent(); 15 timer1.Start(); 16 ActiveControl = textBox1; 17 MessageBox.Show("ORG0001 START"); 18 19 20 } 21 22 private void Form1_KeyDown(object sender, KeyEventArgs e) 23 { 24 switch (e.KeyCode) 25 { 26 27 case Keys.F9: 28 btnRevocation.Focus(); 29 btnRevocation.PerformClick(); 30 break; 31 32 case Keys.F10: 33 btnClosing.Focus(); 34 btnClosing.PerformClick(); 35 break; 36 37 case Keys.F12: 38 btnFinish.Focus(); 39 btnFinish.PerformClick(); 40 break; 41 42 default: 43 return; 44 } 45 } 46 47 private void checkBox1_CheckedChanged(object sender, EventArgs e) 48 { 49 50 } 51 52 private void Form1_Load(object sender, EventArgs e) 53 { 54 KeyPreview = true; 55 } 56 57 private void label1_Click(object sender, EventArgs e) 58 { 59 60 } 61 62 private void textBox1_TextChanged(object sender, EventArgs e) 63 { 64 65 66 } 67 68 private void timer1_Tick(object sender, EventArgs e) 69 { 70 DateTime datetime = DateTime.Now; 71 lblDate.Text = datetime.ToString("yyyy/MM/dd HH:mm:ss"); 72 } 73 74 75 76 private void textBox3_TextChanged(object sender, EventArgs e) 77 { 78 79 } 80 81 private void groupBox2_Enter(object sender, EventArgs e) 82 { 83 84 } 85 86 private void groupBox1_Enter(object sender, EventArgs e) 87 { 88 89 } 90 91 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 92 { 93 94 } 95 96 private void btnDeletion_Click(object sender, EventArgs e) 97 { 98 if (body.Enabled == false) 99 { 100 textBox1.Clear(); 101 } 102 103 if (header.Enabled == false) 104 { 105 106 if (!String.IsNullOrEmpty(textBox2.Text)|| !String.IsNullOrEmpty(textBox3.Text)) 107 { 108 textBox2.Clear(); 109 textBox3.Clear(); 110 checkBox1.Checked = false; 111 } 112 else 113 { 114 checkBox1.Checked = false; 115 header.Enabled = true; 116 body.Enabled = false; 117 } 118 } 119 120 121 } 122 123 124 private void btnClosing_Click(object sender, EventArgs e) 125 { 126 if (String.IsNullOrEmpty(textBox1.Text) && String.IsNullOrEmpty(textBox2.Text) && String.IsNullOrEmpty(textBox3.Text)) 127 { 128 Application.Exit(); 129 } 130 else 131 { 132 DialogResult result = MessageBox.Show("入力中の内容は破棄されます。\nよろしいですか?", 133 "確認", 134 MessageBoxButtons.YesNo, 135 MessageBoxIcon.Exclamation); 136 137 if (result == DialogResult.Yes) 138 { 139 MessageBox.Show("ORG0001 END"); 140 Application.Exit(); 141 } 142 } 143 144 } 145 146 private void btnFinish_Click(object sender, EventArgs e) 147 { 148 149 try 150 { 151 if (header.Enabled == true) 152 { 153 if (String.IsNullOrEmpty(textBox1.Text)) 154 { 155 MessageBox.Show("ユーザーコードを入力してください。"); 156 ActiveControl = textBox1; 157 return; 158 } 159 } 160 161 if (textBox1.Text.IndexOf(' ') > -1 && this.ActiveControl != null) 162 { 163 MessageBox.Show("不正な文字列が含まれています"); 164 ActiveControl = textBox1; 165 return; 166 } 167 168 body.Enabled = true; 169 header.Enabled = false; 170 171 // ここから下のコードがSQL文の記載箇所です。 172 173 SqlConnection connection = new SqlConnection(Properties.Settings.Default.sqlServer); 174 connection.Open(); 175 MessageBox.Show("接続に成功しました"); 176 177 178 StringBuilder sql = new StringBuilder(); 179 sql.AppendLine("SELECT USER_ID"); 180 sql.AppendLine("FROM USER_REGISTRATION"); 181 sql.AppendLine("WHERE USER_ID = 1"); 182 183 SqlCommand command = new SqlCommand(sql.ToString(), connection); 184 SqlDataReader dr = command.ExecuteReader(); 185 186 187 188 //while (dr.Read()) 189 //{ 190 // for (int i = 0; i < dr.FieldCount; i++) 191 // { 192 // MessageBox.Show(dr[i].ToString()); 193 // } 194 //} 195 196 //builder.DataSource = "localhost\\SQLEXPRESS"; 197 //builder.UserID = "test"; 198 //builder.Password = "test"; 199 //builder.InitialCatalog = "master"; 200 //builder.IntegratedSecurity = true; 201 202 //using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) 203 //{ 204 // connection.Open(); 205 // MessageBox.Show("接続に成功しました"); 206 207 // SqlCommand command = new SqlCommand(); 208 209 210 //} 211 212 213 214 } 215 catch (SqlException ex) 216 { 217 MessageBox.Show(ex.Message); 218 } 219 220 catch (Exception ex) 221 { 222 MessageBox.Show(ex.Message); 223 } 224 } 225 } 226}
回答3件
あなたの回答
tips
プレビュー