MasterRacker
Active member
The code below is a snippet from a small utility I'm trying to write to load an Excel spreadsheet int oa DataSet, change the "Password" and "ChangeDay" fields in ALL rows with programmatically generated information, then save the whole sheet back out.
Everything works as I want it to except the save. Being new to ADO.NET I'm suffering brain disintegration trying to come up with the UPDATE command necessary to allow the DataAdapter Update method to function.
I expect the command string to be something along the lines of "OleDbCommand cmdUpdate = new OleDbCommand("UPDATE [Sheet1$] SET Password = @Password, ChangeDay = @ChangeDay WHERE UserID = @UserID");" but I'm struggling with getting the parameters into the command object.
Can anyone suggest an Update command? (I'm sure I'm missing something obvious and I'm too brain damaged from years of beer abuse to intrepret what Dr. Google is showing me, but that's the way it goes some days. )![[cheers] [cheers] [cheers]](/data/assets/smilies/cheers.gif)
Jeff
The future is already here - it's just not widely distributed yet...
Everything works as I want it to except the save. Being new to ADO.NET I'm suffering brain disintegration trying to come up with the UPDATE command necessary to allow the DataAdapter Update method to function.
I expect the command string to be something along the lines of "OleDbCommand cmdUpdate = new OleDbCommand("UPDATE [Sheet1$] SET Password = @Password, ChangeDay = @ChangeDay WHERE UserID = @UserID");" but I'm struggling with getting the parameters into the command object.
Code:
private StringBuilder sbConn = new StringBuilder();
private string workBookName = ""; // filled by an OpenFileDialog
private OleDbConnection cnExcel;
private OleDbDataAdapter daWorkBook;
private void LoadSheet()
{
sbConn.Append(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
sbConn.Append(workBookName);
sbConn.Append(";Extended Properties=");
sbConn.Append(Convert.ToChar(34));
sbConn.Append("Excel 8.0;HDR=Yes;IMEX=2");
sbConn.Append(Convert.ToChar(34));
cnExcel = new OleDbConnection(sbConn.ToString());
cnExcel.Open();
OleDbCommand cmdSelect = new OleDbCommand("SELECT UserID, Password, ChangeDay FROM [Sheet1$]", cnExcel);
daWorkBook = new OleDbDataAdapter();
daWorkBook.SelectCommand = cmdSelect;
daWorkBook.Fill(dsWorkBook);
cnExcel.Close();
}
private void btnSave_Click(object sender, System.EventArgs e)
{
// Update command generated here....
cnExcel.Open();
daWorkBook.Update (dsWorkBook);
cnExcel.Close();
}
Can anyone suggest an Update command? (I'm sure I'm missing something obvious and I'm too brain damaged from years of beer abuse to intrepret what Dr. Google is showing me, but that's the way it goes some days. )
![[cheers] [cheers] [cheers]](/data/assets/smilies/cheers.gif)
Jeff
The future is already here - it's just not widely distributed yet...