2012-11-19 2 views
1

그래서 DataAdapter 및 Datasets를 사용하여 데이터베이스에 연락처를 추가하려고합니다. 그러나 데이터를 추가하려고 할 때 계속 동일한 오류가 발생합니다. (제목 참조)C# OleDbException 처리되지 않았습니다 : 하나 이상의 필수 매개 변수에 값이 지정되지 않았습니다.

또한 데이터를 업데이트해도 오류는 발생하지 않지만 아무것도 업데이트하지 않습니다.

추가 사용자 코드

public void AddUser(Contact contact) { 
    command.Connection = getConnection(); 
    command.CommandText = "INSERT INTO tblContact VALUES(Id = @contactid, LastName = @lastname, FirstName = @firstname, Address = @address" 
    + "PostCode = @postcode, City = @city, Gender = @gender, Blocked = @blocked)"; 

    command.Parameters.AddWithValue("@contactid", contact.AccountId); 
    command.Parameters.AddWithValue("@lastname", contact.LastName); 
    command.Parameters.AddWithValue("@firstname", contact.FirstName); 
    command.Parameters.AddWithValue("@address", contact.Address); 
    command.Parameters.AddWithValue("@postcode", contact.PostCode); 
    command.Parameters.AddWithValue("@city", contact.City); 
    bool gender = (contact.Gender == Gender.Male ? true : false); 
    command.Parameters.AddWithValue("@gender", gender); 
    command.Parameters.AddWithValue("@blocked", contact.Blocked); 
    try { 
    adapter.InsertCommand = command; 
    adapter.Update(dsCon, "tblContact"); 
    adapter.Fill(dsCon, "tblContact"); 
    } 
    catch (Exception e) { 
    String code = e.Message; 
    } 

} 

사용자에게

public void ModifyUser(Contact contact) 
{ 
    command.Connection = getConnection(); 
    command.CommandText = "UPDATE tblContact SET LastName = @lastname, FirstName = @firstname, Address = @address" 
    + "PostCode = @postcode, City = @city, Gender = @gender, Blocked = @blocked " 
    + "WHERE Id = @contactid"; 

    command.Parameters.AddWithValue("@contactid", contact.AccountId); 
    command.Parameters.AddWithValue("@lastname", contact.LastName); 
    command.Parameters.AddWithValue("@firstname", contact.FirstName); 
    command.Parameters.AddWithValue("@address", contact.Address); 
    command.Parameters.AddWithValue("@postcode", contact.PostCode); 
    command.Parameters.AddWithValue("@city", contact.City); 
    command.Parameters.AddWithValue("@gender", contact.Gender); 
    command.Parameters.AddWithValue("@blocked", contact.Blocked); 

    adapter.UpdateCommand = command; 
    adapter.Update(dsCon, "tblContact"); 
    adapter.Fill(dsCon, "tblContact"); 
} 

당신이 그것을 도울 수 있다면 이러한 프로세스

private void btnSave_Click(object sender, EventArgs e) { 
    Contact currentContact = new Contact(); 
    currentContact.AccountId = Int32.Parse(lblID.Text); 
    currentContact.LastName = txtLastName.Text; 
    currentContact.FirstName = txtName.Text; 
    currentContact.Address = txtStreet.Text; 
    currentContact.PostCode = Int32.Parse(txtPostalCode.Text); 
    currentContact.City = txtCity.Text; 
    currentContact.Gender = (rdbMale.Checked == true ? Gender.Male : Gender.Female); 
    currentContact.Blocked = chkBlocked.Checked; 
    currentContact.Address = txtStreet.Text; 
    if(isNewContact){ 
    currentContact.AccountId = (manager.GetContacts().Last().AccountId + 1); 
    manager.GetOleDbManager().AddUser(currentContact); 
    } else { 
     manager.GetOleDbManager().ModifyUser(currentContact); 
    } 
    currentContact.Categories = new List<Category>(); 
    foreach (Object c in lstCategories.SelectedItems) { 
    currentContact.Categories.Add((Category)c); 
    } 

    isNewContact = false; 

} 

을 시작하는 내 양식의 코드를 업데이트하는 데 사용되는 코드를 환상적 일 것입니다. 사용중인 db의 스크린 샷입니다. http://i50.tinypic.com/2s93klk.png

답변

1

삽입 명령이 유효한 sql INSERT 문이 아닙니다. (Microsoft.ACE.OleDb.xx 같은) 일부 OLEDB 공급자가의 매개 변수를 가지도록 ParameterCollection을 필요로하기 때문에

command.CommandText = "INSERT INTO tblContact VALUES(@contactid, @lastname, @firstname,"+ 
         "@address,@postcode, @city, @gender, @blocked)"; 

업데이트 commad 텍스트 쉼표

그러나
command.CommandText = "UPDATE tblContact SET LastName = @lastname, FirstName = @firstname," + 
         "Address = @address, PostCode = @postcode, City = @city, " + 
         "Gender = @gender, Blocked = @blocked WHERE Id = @contactid"; 

누락 된 명령은 실패 그것들이 SQL 텍스트에 나타나는 정확한 순서. (명명 된 매개 변수에 대한 지원이 없습니다). 업데이트 문에는 @contactid가 마지막 매개 변수로 포함되어있는 동안 첫 번째 매개 변수로 추가됩니다. @contactid를 마지막 매개 변수로 추가하는 AddWithValue 시퀀스를 변경하려고 할 수 있습니다.

+0

+1 나는 같은 대답을 썼다. 40 초 만에 나를 때려 라. –

+0

Faill! o.O 복사 붙여 넣기를 위해 무엇입니까? –

+0

xS로 업데이트 절차가 여전히 작동하지 않습니다 –

관련 문제