0

以下のコードを使用して、テキスト ボックスに姓と名を入力しようとしています。

using (OleDbConnection connName = new OleDbConnection(strCon))
            {

                String sqlName = "SELECT forename, Surname FROM customer WHERE [customerID]=" + txtCustomerID.Text;

                // Create a command to use to call the database.
                OleDbCommand commandname = new OleDbCommand(sqlName, connName);
                 connName.Open();
                // Create a reader containing the results


                using (OleDbDataReader readerName = commandname.ExecuteReader())
                {
                    readerName.Read(); // Advance to the first row.
                    txtName.Text = readerName[0].ToString();
                }
                connName.Close();                 

             }

ただし、エラーが発生しています:OleDbException未処理でした。

「1 つ以上の必須パラメータに必要な値がありません」

で、ExecuteReaderこれを修正する方法がわかりません。

編集: 以下のこのコードは、クエリ内の情報とほぼ同じバーですが、この例外は発生しません。

  string strCon = Properties.Settings.Default.PID2dbConnectionString;

        using (OleDbConnection conn = new OleDbConnection(strCon))
        {
            String sqlPoints = "SELECT points FROM customer WHERE [customerID]=" + txtCustomerID.Text;
            conn.Open();

            // Create a command to use to call the database.
            OleDbCommand command = new OleDbCommand(sqlPoints, conn);
            // Create a reader containing the results

            using (OleDbDataReader reader = command.ExecuteReader())
            {
                reader.Read(); // Advance to the first row.
                txtPoints.Text = reader[0].ToString(); // Read the contents of the first column
            }
            conn.Close();
        }
4

2 に答える 2

1

これの通常の理由は、null または空の文字列です。つまり、txtCustomerID.Text には値がないため、サーバーに送信されるクエリは次のようになります。

SELECT forename, Surname FROM customer WHERE [customerID]= 

this やSQL Injectionのようなエラーを回避し、厳密に型指定されたパラメーターを使用し、パラメーター化されたクエリを使用してデータの切り捨てを回避できます (顧客 ID は int フィールドであると想定しています)。

    using (OleDbConnection connName = new OleDbConnection(strCon))
    {
        String sqlName = "SELECT forename, Surname FROM customer WHERE customerID = @CustomerID";

        // Create a command to use to call the database.
        using (OleDbCommand commandname = new OleDbCommand(sqlName, connName))
        {

            //Check the input is valid
            int customerID = 0;
            if (!int.TryParse(txtCustomerID.Text, out customerID))
            {
                txtName.Text = "Customer ID Text box is not an integer";
                return;
            }


            connName.Open();

            // Add the parameter to the command
            commandname.Parameters.Add("@CustomerID", OleDbType.Integer).Value = customerID;

            // Create a reader containing the results
            using (OleDbDataReader readerName = commandname.ExecuteReader())
            {
                readerName.Read(); // Advance to the first row.
                txtName.Text = readerName[0].ToString();
            }
            connName.Close();                 
        }
    }
于 2013-01-10T18:19:32.017 に答える
0

文字列クエリで使用されるパラメーターをエンコードする必要があります。

  String sqlName = String.Format("SELECT forname, Surname FROM customer WHERE customerID={0}",txtCustomerID.Text);

ただし、文字列にハードコードされた SQL クエリを使用しないことをお勧めします。SQL インジェクション攻撃の簡単な方法です。代わりにパラメーターを使用する必要があります。

于 2013-01-10T17:08:29.547 に答える