int lev;
SqlCommand getlev = new SqlCommand("SELECT level FROM [User] WHERE Username = @username");
getlev.Parameters.AddWithValue("@username", txtuser.Text);
c.Open();
lev = (int)getlev.ExecuteScalar();
c.Close();
lev++;
SqlCommand lvup= new SqlCommand("UPDATE [User] SET level = @lev WHERE Username = @username", c);
lvup.Parameters.AddWithValue("@lev",lev);
lvup.Parameters.AddWithValue("@username", txtuser.Text);
c.Open();
lvup.ExecuteNonQuery();
c.Close();
エラー:
ExecuteScalar: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: ExecuteScalar: Connection property has not been initialized.
は、「c」の定義を示します。 – fubo
エラーメッセージは自明です。 getlev SqlCommandオブジェクトにConnectionプロパティを設定します。 –
Secondeのように最初の 'SqlCommand'に接続パラメータを追加することができます。また、' SqlCommande'を実行したい場合には接続を閉じてはいけません。 –