SQL Update Statement, OleDbDataAdapter, and ms access 2007
-
Hi guys, I'm trying to understand an Update query generated from vs2010. I'm new to sql and all of the msdn answers are hard-coded, and I need some help to do this with variable data. I've figured out my insert statement
DataSet dataChanges = new UserData();
dataChanges.DataSetName = "dataChanges";dataChanges = this.\_userDataSet.GetChanges(DataRowState.Modified | DataRowState.Added | DataRowState.Deleted); this.\_dbConnection.ConnectionString = this.\_connectionString; this.\_dbConnection.Open(); //insert new rows OleDbCommand command = new OleDbCommand("INSERT INTO tblUsers(ID\_NAME, ID\_PASSWORD, ID\_RIGHTS, ID\_JOBS)VALUES(?, ?, ?, ?)", this.\_dbConnection); command.Parameters.Add(this.\_userDataSet.tblUsers.ID\_NAMEColumn.ColumnName, OleDbType.VarChar, this.\_userDataSet.tblUsers.ID\_NAMEColumn.MaxLength, "ID\_NAME"); command.Parameters.Add(this.\_userDataSet.tblUsers.ID\_PASSWORDColumn.ColumnName, OleDbType.VarChar, this.\_userDataSet.tblUsers.ID\_PASSWORDColumn.MaxLength, "ID\_PASSWORD"); command.Parameters.Add(this.\_userDataSet.tblUsers.ID\_RIGHTSColumn.ColumnName, OleDbType.Integer, this.\_userDataSet.tblUsers.ID\_RIGHTSColumn.MaxLength, "ID\_RIGHTS"); command.Parameters.Add(this.\_userDataSet.tblUsers.ID\_JOBSColumn.ColumnName, OleDbType.Integer, this.\_userDataSet.tblUsers.ID\_JOBSColumn.MaxLength, "ID\_JOBS"); this.\_dbAdapter.InsertCommand = command;
_userDataSet is a typed dataset. The vs generated UPDATE query looks like this:
//update rows;
command = new OleDbCommand("UPDATE tblUsers SET ID_NAME = ?, ID_PASSWORD = ?," +
"ID_RIGHTS = ?, ID_JOBS = ? WHERE ((ID_NAME = ?) AND" +
"((? = 1 AND ID_PASSWORD IS NULL) OR (ID_PASSWORD = ?))" +
"AND ((? = 1 AND ID_RIGHTS -
Hi guys, I'm trying to understand an Update query generated from vs2010. I'm new to sql and all of the msdn answers are hard-coded, and I need some help to do this with variable data. I've figured out my insert statement
DataSet dataChanges = new UserData();
dataChanges.DataSetName = "dataChanges";dataChanges = this.\_userDataSet.GetChanges(DataRowState.Modified | DataRowState.Added | DataRowState.Deleted); this.\_dbConnection.ConnectionString = this.\_connectionString; this.\_dbConnection.Open(); //insert new rows OleDbCommand command = new OleDbCommand("INSERT INTO tblUsers(ID\_NAME, ID\_PASSWORD, ID\_RIGHTS, ID\_JOBS)VALUES(?, ?, ?, ?)", this.\_dbConnection); command.Parameters.Add(this.\_userDataSet.tblUsers.ID\_NAMEColumn.ColumnName, OleDbType.VarChar, this.\_userDataSet.tblUsers.ID\_NAMEColumn.MaxLength, "ID\_NAME"); command.Parameters.Add(this.\_userDataSet.tblUsers.ID\_PASSWORDColumn.ColumnName, OleDbType.VarChar, this.\_userDataSet.tblUsers.ID\_PASSWORDColumn.MaxLength, "ID\_PASSWORD"); command.Parameters.Add(this.\_userDataSet.tblUsers.ID\_RIGHTSColumn.ColumnName, OleDbType.Integer, this.\_userDataSet.tblUsers.ID\_RIGHTSColumn.MaxLength, "ID\_RIGHTS"); command.Parameters.Add(this.\_userDataSet.tblUsers.ID\_JOBSColumn.ColumnName, OleDbType.Integer, this.\_userDataSet.tblUsers.ID\_JOBSColumn.MaxLength, "ID\_JOBS"); this.\_dbAdapter.InsertCommand = command;
_userDataSet is a typed dataset. The vs generated UPDATE query looks like this:
//update rows;
command = new OleDbCommand("UPDATE tblUsers SET ID_NAME = ?, ID_PASSWORD = ?," +
"ID_RIGHTS = ?, ID_JOBS = ? WHERE ((ID_NAME = ?) AND" +
"((? = 1 AND ID_PASSWORD IS NULL) OR (ID_PASSWORD = ?))" +
"AND ((? = 1 AND ID_RIGHTSIn order to fill in the parameters you would use the same type of code from the INSERT statement generation:
command.Parameters.Add(this._userDataSet.tblUsers.ID_NAMEColumn.ColumnName, OleDbType.VarChar, this._userDataSet.tblUsers.ID_NAMEColumn.MaxLength, "ID_NAME"); command.Parameters.Add(this._userDataSet.tblUsers.ID_PASSWORDColumn.ColumnName, OleDbType.VarChar, this._userDataSet.tblUsers.ID_PASSWORDColumn.MaxLength, "ID_PASSWORD"); // Etc., etc.
You would change the OleDbType to the appropriate type for the parameter. And you change the last parameter to the name of the field. Does that make sense? Hopefully this helps. I answered based on what I could gather from your questions.