誰かがなぜこの値をデータベースに追加していないのか教えてください。フォームは正常に実行され、エラーは返されません。C#データベースの問題
private void button1_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand();
connection.ConnectionString = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\John\Documents\Setup.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
command.Parameters.AddWithValue("@userName", textBox1.Text);
command.Parameters.AddWithValue("@passWord", textBox2.Text);
command.CommandText = "INSERT INTO Setup (userName, password) VALUES(@userName, @passWord)";
try
{
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
}
catch (Exception ex)
{
// handle exception
}
finally
{
connection.Close();
}
}
FYI:私は "初心者"です。私のデータベースはセットアップと呼ばれています。私は手動で2つのカラムのuserNameともう1つのパスワードをnchar(50)に設定したテーブルを手動で追加しました。
変更は ' – MilkyWayJoe
ヒントmyTable'に挿入'にSetup'に挿入する - それは良いでしょう(短い)へたとえば 'using(){}'を使用します。 - 'using(SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); //ここで作業してください。接続は次の行で閉じます。 } ' – sinelaw