2016-07-22 3 views
0

テキストボックスが空であっても挿入が失敗しても、データベースに値を返すようにINSERTステートメントを設定しました。OleDbを介してデータベースにアクセスするためのヌル値を返す

「1つ以上の必須パラメータに値が指定されていません。」というメッセージが表示されます。

どこが間違っていますか?アクセス中

私のフィールドは次のように設定されていない必要な、これはOLEDB接続で動作しますが、あなたはそれを受け入れるだろうと仮定すると、このようにコードを書き換える必要がある場合、私はよく分からないコメントに基づいて

private void NewCustomer_Load(object sender, EventArgs e) 
{ 

} 

private void button2_Click(object sender, EventArgs e) 
{ 
    OleDbConnection Conn = new OleDbConnection(); 
    Conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb"; 
    OleDbCommand command = new OleDbCommand(); 
    command.CommandText = "INSERT INTO Contacts (Title,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Archived) VALUES (@Title,@FirstName,@LastName,@Address1,@Address2,@Address3,@PostTown,@PostCode,@Telephone,Archived = 0)"; 

    if (string.IsNullOrEmpty(FirstName.Text)) 
    { 
     command.Parameters.AddWithValue("@FirstName", DBNull.Value); 
    } 
    else 
    { 
     command.Parameters.AddWithValue("@FirstName", title.Text); 
    } 

    if (string.IsNullOrEmpty(LastName.Text)) 
    { 
     command.Parameters.AddWithValue("@LastName", DBNull.Value); 
    } 
    else 
    { 
     command.Parameters.AddWithValue("@LastName", title.Text); 
    } 

    if (string.IsNullOrEmpty(Address1.Text)) 
    { 
     command.Parameters.AddWithValue("@Address1", DBNull.Value); 
    } 
    else 
    { 
     command.Parameters.AddWithValue("@Address1", title.Text); 
    } 

    if (string.IsNullOrEmpty(Address2.Text)) 
    { 
     command.Parameters.AddWithValue("@Address2", DBNull.Value); 
    } 
    else 
    { 
     command.Parameters.AddWithValue("@Address2", title.Text); 
    } 

    if (string.IsNullOrEmpty(Address3.Text)) 
    { 
     command.Parameters.AddWithValue("@Address3", DBNull.Value); 
    } 
    else 
    { 
     command.Parameters.AddWithValue("@Address3", title.Text); 
    } 

    if (string.IsNullOrEmpty(Postcode.Text)) 
    { 
     command.Parameters.AddWithValue("@PostCode", DBNull.Value); 
    } 
    else 
    { 
     command.Parameters.AddWithValue("@PostCode", title.Text); 
    } 

    if (string.IsNullOrEmpty(TownCity.Text)) 
    { 
     command.Parameters.AddWithValue("@PostTown", DBNull.Value); 
    } 
    else 
    { 
     command.Parameters.AddWithValue("@PostTown", title.Text); 
    } 

    if (string.IsNullOrEmpty(PhnNum.Text)) 
    { 
     command.Parameters.AddWithValue("@Telephone", DBNull.Value); 
    } 
    else 
    { 
     command.Parameters.AddWithValue("@Telephone", title.Text); 
    } 

    if (string.IsNullOrEmpty(Titl.Text)) 
    { 
     command.Parameters.AddWithValue("@Title", DBNull.Value); 
    } 
    else 
    { 
     command.Parameters.AddWithValue("@Title", title.Text); 
    } 

    Conn.Open(); 
    command.Connection = Conn; 
    command.ExecuteNonQuery(); 
    Conn.Close(); 

    FirstName.Text = null; 
    LastName.Text = null; 
    Address1.Text = null; 
    Address2.Text = null; 
    Address2.Text = null; 
    Postcode.Text = null; 
    TownCity.Text = null; 
    Titl.Text = null; 
    PhnNum.Text = null; 
    Address3.Text = null; 

    MessageBox.Show("Customer Added"); 
} 
+0

私はあなたが最後に「アーカイブ= 0」を削除すべきだと思います。そして、ちょうど0を入れてください。 – Danieboy

+0

「OleDbCommand」は、名前付きパラメータをまったくサポートしていないと思います。「OLE DB .NETプロバイダは、SQL文や、で呼び出されるストアドプロシージャにパラメータを渡すための名前付きパラメータをサポートしていません。 CommandTypeがTextに設定されている場合はOleDbCommand。この場合、疑問符(?)プレースホルダを使用する必要があります。 –

+0

1.あなたの 'AddWithValue'には、そのアイテムの特定のテキストを追加することを意味するどこにでも' title.Text'があります。 2. 'Archived = 0'は挿入時に' Archived'の値が分からないので動作しません。値を指定する必要があります(これが主な原因です)。 3. Connectionオブジェクトを 'using'ステートメントで囲み、常に閉じます。 4.パラメータにはうまくいきますが、パラメータの型を指定するのを忘れないでください。これを 'AddWithValue'に連鎖させることができます。最後に、パラメータはOleDbの位置に基づいているため、クエリのパラメータの順序はコレクションのパラメータと正確に一致する必要があります。 – Igor

答えて

0

指定されたパラメータあなたが質問を使用する必要がOLEDBパラメータのために述べたように

private void button2_Click(object sender, EventArgs e) 
{ 
    using(OleDbConnection Conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb")) 
    { 
     OleDbCommand command = new OleDbCommand(); 

     command.CommandText = "INSERT INTO Contacts (Title,Initial,Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Archived) VALUES (@Title,@FirstName,@LastName,@Address1,@Address2,@Address3,@PostTown,@PostCode,@Telephone,0)"; 

     command.Parameters.AddWithValue("@FirstName", string.IsNullOrEmpty(FirstName.Text) ? DBNull.Value : title.Text); 
     command.Parameters.AddWithValue("@LastName", string.IsNullOrEmpty(LastName.Text) ? DBNull.Value : title.Text); 
     command.Parameters.AddWithValue("@Address1", string.IsNullOrEmpty(Address1.Text) ? DBNull.Value : title.Text); 
     command.Parameters.AddWithValue("@Address2", string.IsNullOrEmpty(Address2.Text) ? DBNull.Value : title.Text); 
     command.Parameters.AddWithValue("@Address3", string.IsNullOrEmpty(Address3.Text) ? DBNull.Value : title.Text); 
     command.Parameters.AddWithValue("@PostCode", string.IsNullOrEmpty(Postcode.Text) ? DBNull.Value : title.Text); 
     command.Parameters.AddWithValue("@PostTown", string.IsNullOrEmpty(TownCity.Text) ? DBNull.Value : title.Text); 
     command.Parameters.AddWithValue("@Telephone", string.IsNullOrEmpty(PhnNum.Text) ? DBNull.Value : title.Text); 
     command.Parameters.AddWithValue("@Title", string.IsNullOrEmpty(Titl.Text) ? DBNull.Value : title.Text); 

     Conn.Open(); 
     command.Connection = Conn; 
     command.ExecuteNonQuery(); 
    } 

    FirstName.Text = null; 
    LastName.Text = null; 
    Address1.Text = null; 
    Address2.Text = null; 
    Address2.Text = null; 
    Postcode.Text = null; 
    TownCity.Text = null; 
    Title.Text = null; 
    PhnNum.Text = null; 
    Address3.Text = null; 

    MessageBox.Show("Customer Added"); 
} 
+0

1私はコネクションの周りに 'using'ブロックを追加しました。このブロックは常に行われるべきです。そうすれば、例外が発生したときにコネクションが開いたままになることはありません。 – Igor

+0

ありがとう@Igor – Danieboy

0

は、代わりにマーク:

private void button2_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     using (var Conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=BoilerSvc_be.mdb")) 
     { 
      var command = new OleDbCommand("INSERT INTO Contacts (" + 
           "Title, Initial, Surname,[Address 1],[Address 2],[Address 3],[Post Town],[Post Code],Telephone,Archived)" + 
         " VALUES ( ?,   ?,  ?,   ?,   ?,   ?,   ?,   ?,  ?,  0)", Conn); 
      Control[] controls = { Titl, FirstName, LastName, Address1, Address2, Address3, TownCity, Postcode, PhnNum }; 

      foreach (var control in controls) 
       command.Parameters.AddWithValue("@" + control.Name, 
        string.IsNullOrEmpty(control.Text) ? DBNull.Value : control.Text as object); 

      Conn.Open(); 
      if (command.ExecuteNonQuery() == 1) 
      { 
       MessageBox.Show("Customer Added"); 
       foreach (var control in controls) 
        control.Text = ""; 
      } 
      else 
       MessageBox.Show("Customer was not Added"); 

     } // Conn is closed and disposed at the end of the using block 
    } 
    catch (Exception ex) { 
     MessageBox.Show("Exception : " + ex.Message); 
    } 
} 
関連する問題