2012-04-22 13 views
2

特定の行のデータベースを更新しようとするときに、Must declare scalar variable "@lvl"というエラーが表示されます。私が何をすべきかは不明です。私は自分のSQLステートメント内で値を宣言する必要がありますか?SQLExceptionスカラー変数 "@variableName"を宣言しなければならない

private void button1_Click(object sender, EventArgs e) 
{ 
    GridViewCellInfo grd = (GridViewCellInfo)radGridView1.Rows[0].Cells[0]; 
    string lvl = grd.Value.ToString(); 

    string sqlPatientCmd = 
     @"UPDATE MotorTB 
      SET RightColumn = @RightColumnCB, LeftColumn = @leftColumnCB 
      WHERE (Level = @lvl)"; 
    SqlConnection connString = new SqlConnection(@"Data Source=MERCURY\SQLEXPRESS;Initial Catalog=AsiaDB; Integrated Security=SSPI;User ID=MERCURY\Sophie;"); 
    try { 
     connString.Open(); 

     SqlCommand sqlCmdStatement = new SqlCommand(sqlPatientCmd, connString); 

     GridViewCellInfo grid; 
     grid = (GridViewCellInfo)radGridView1.Rows[0].Cells[1]; 
     string rightColVal = grid.Value.ToString(); 

     grid = (GridViewCellInfo)radGridView1.Rows[0].Cells[2]; 
     string leftColVal = grid.Value.ToString(); 

     sqlCmdStatement.Parameters.AddWithValue("@rightColumnCB", rightColVal); 
     sqlCmdStatement.Parameters.AddWithValue("@leftColumnCB", leftColVal); 
     sqlCmdStatement.ExecuteScalar(); 
    } catch (Exception ex) { 
     Console.WriteLine(ex.ToString()); 
    } 

    // Close the connection 
    try { 
     connString.Close(); 
    } catch (Exception ex) { 
     Console.WriteLine(ex.ToString()); 
    } 
} 
+0

エラーメッセージは、問題の解決方法を正確に示しています... –

答えて

2

あなたは、パラメータとして指定またはその他のクエリで定義する必要が@lvl SqlParamter

sqlCmdStatement.Parameters.AddWithValue("@lvl", lvl); 
+0

ありがとうございます。それは私が前にそれを試したが、同じエラーが発生しました。私は私のSQLステートメントを – user1155383

+1

に変更しました。私は新しいステートメントを見ることができません。 – SimSimY

1

としてLVLのVARを追加する必要があります。

関連する問題