-

2011年8月3日星期三

SQL Update Statement and ADO.Net?

-What's wrong in that SQL update statement?



using c# and ADO.Net

database : Access





--------------------------------------鈥?br>
I am using an access data base called HotelDataBase.MDB with a table called Register in which 14-columns that are respectively

First Name [Text], Sure Name [Text], Age [Int], ID [Int], Tel [Int], Gender [Text], Country [Text], Room Number [Int], Room Type [Text], Room Class [Text], Number of Nights [Int], Start Date Month [Text], Start Date Day [Int], Start Date Year [Int]



and I've a Form in which a text box called "updateIDTextBox" and Search Button to search for the specified id in that Text Box.



if the id exists, all data will be displayed in their editable places in the form as

First Name : Name------------Last Name : MyName

Age : 22 ------------ID : 1320065 and So On...





Then any of these data can be changed then the user clicks on the Save Button to Update data of the specified id in the search text box



the update statement is





Collapse



string updateQuery = "Update Register Set First Name = '" + firstNameTextBox.Text + "',Sure Name = '" + sureNameTextBox.Text + "',Age = " + ageTextBox.Text + ",ID = " + idTextBox.Text + ",Tel = " + telTextBox.Text + ",Gender = '" + genderComboBox.Text + "',Country = '" + countryComboBox.Text + "',Room Number = " + roomNumberTextBox.Text + ",Room Type = '" + roomTypeComboBox.Text + "',Room Class = '" + roomClassComboBox.Text + "',Number of Nights = " + noOfNightsTextBox.Text + ",Sart Date Month = '" + monthComboBox.Text) + "',Start Date Day = " + dayComboBox.Text + ",Start Date Year = " + yearComboBox.Text + " Where ID = " + updateIDTextBox.Text + ";";





Unfortunately i received a syntax error in the update statement



this is the code of Save Button

Collapse



/*

* all recored are checked

* all inputs are valid

* now update data to the database

* open connection to database

* build commands, send it to database

* save updated data to database

* close connection of database

*/

//the connection

DataBaseOperations.CON = new OleDbConnection(DataBaseOperations.CONNE鈥?br>


string updateQuery = "Update Register Set FirstName = '" + firstNameTextBox.Text + "',SureName = '" + sureNameTextBox.Text + "',Age = " + ageTextBox.Text + ",ID = " + idTextBox.Text + ",Tel = " + telTextBox.Text + ",Gender = '" + genderComboBox.Text + "',Country = '" + countryComboBox.Text + "',RoomNumber = " + roomNumberTextBox.Text + ",RoomType = '" + roomTypeComboBox.Text + "',RoomClass = '" + roomClassComboBox.Text + "',NumberofNights = " + noOfNightsTextBox.Text + ",SartDateMonth = '" + monthComboBox.Text + "',StartDateDay = " + dayComboBox.Text + ",StartDateYear = " + yearComboBox.Text + ", Where ID = " + updateIDTextBox.Text + ";";





// MessageBox.Show(updateQuery, "Update Query");



DataBaseOperations.COM = new OleDbCommand(updateQuery, DataBaseOperations.CON);



DataBaseOperations.CON.Open();

DataBaseOperations.COM.ExecuteNonQuery()鈥?br>
DataBaseOperations.CON.Close();

clearAllFieldsButton.Enabled = true;

MessageBox.Show("Update Process of Habitant : " + firstNameTextBox.Text.ToString() + " " + sureNameTextBox.Text.ToString() + "\nHas Been Done Successfully", "Successful Update");Are you sure about the field name: SartDateMonth? Shouldn't it be StartDateMonth?

没有评论:

发表评论