StackOverflowで質問するのは初めてなので、不適切な質問をしていたら、あらかじめお詫びします。過去数日間、これを調査している間、私を助けるものを見つけることができませんでしたので、助けようとする人に前もって感謝します.
人々が登録してログインできるようにするデータベースを作成しています。VS2012 で C# を使用しています。以下は私のログインコードで、テスト中に問題が発生しています。データベース内のすべての人を反復処理し、適切なユーザーに到達するまでログインが失敗したことを通知します。
private void button1_Click_1(object sender, EventArgs e)
{
try
{
cn.Open();
}
catch (Exception)
{
MessageBox.Show("Did not connect");
}
SqlCommand cmd = new SqlCommand("SELECT * FROM [Users]", cn);
cmd.Connection = cn;
SqlDataReader reader = null;
reader = cmd.ExecuteReader();
while (reader.Read())
{
if (textBox1.Text == (reader["Username"].ToString()) && textBox2.Text == (reader["Password"].ToString()))
{
MessageBox.Show("Logged in");
}
else
{
MessageBox.Show("Login has failed. Please check your Username and Password.");
}
}
cn.Close();
}
私の登録部分に関しては、それがVS2012のものなのかどうかはわかりませんが、デバッグを終了してから再びデバッグに戻ると、情報がデータベースに保存されません。
private void button1_Click_1(object sender, EventArgs e)
{
cn.Open();
SqlCommand cm1 = new SqlCommand("INSERT INTO Users (Username, Password) VALUES (@Username, @Password)", cn);
SqlCommand cm2 = new SqlCommand("INSERT INTO Contact(Name, Address, City, State, PostalCode, Email, PhoneNumber) VALUES(@Name, @Address, @City, @State, @PostalCode, @Email, @PhoneNumber)", cn);
cm1.Parameters.AddWithValue("@Username", textBox1.Text);
cm1.Parameters.AddWithValue("@Password", textBox2.Text);
cm2.Parameters.AddWithValue("@Name", textBox3);
cm2.Parameters.AddWithValue("@Address", textBox4);
cm2.Parameters.AddWithValue("@City", textBox5);
cm2.Parameters.AddWithValue("@State", textBox6);
cm2.Parameters.AddWithValue("@PostalCode", textBox7);
cm2.Parameters.AddWithValue("@Email", textBox8);
cm2.Parameters.AddWithValue("@PhoneNumber", textBox9);
try
{
int affectedRows = cm1.ExecuteNonQuery(); //+cm2.ExecuteNonQuery();
if (affectedRows > 0)
{
MessageBox.Show("Insert Sucsess!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Insert Failed!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
cn.Close();
}