D
Dan V.
Why does the insert command not work in a transaction? I am using OleDb
with MS Access and ADO.Net and VS 2003.net
I create the connection and dataadapter initially in a function and return
them so I can reuse them
And in one case I would like a transaction. But when I fill the datarow it
fails for some reason.
I followed: http://support.microsoft.com/kb/310351
Do I need to create another dataadapter for this to work?
Thanks for any help.
The setup is like this:
DataSet dsTrans = dsVerify.GetChanges();
OleDbTransaction trVerify;
trVerify = vConnVerify.BeginTransaction(IsolationLevel.ReadCommitted);
vDA.SelectCommand.Transaction = trVerify;
try
{
FillDataRow( sDB, sOrig, psc, Form_ID, tif, tifNonForm, tct, res, img, "",
"", tblVerify, vDA, dsTrans, tifFound, tifNonFormFound, tctFound, resFound,
imgFound, false, false);
etc.
----------------------------------------------------------------------------
-------
Function that creates connection and dataadpter and update commands:
// Connect to a MS Access Database with standard security
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
sDataSource + ";User Id=admin;Password=");
conn.Open();
// Create an updateable DataTable in a DataSet
da = CreateVerifyDataAdapter( sTableName, conn );
ds = new DataSet();
da.Fill(ds, sTableName);
dt = new DataTable();
dt = ds.Tables[sTableName];
-------------------------------------------------------
private OleDbDataAdapter CreateVerifyDataAdapter(string sTableName,
OleDbConnection conn)
{
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand cmd;
// Create the SelectCommand.
cmd = new OleDbCommand("SELECT * FROM " + sTableName, conn);
da.SelectCommand = cmd;
// Create the InsertCommand.
cmd = new OleDbCommand("INSERT INTO " + sTableName + " (fld1, fld2 ) " +
"VALUES (@fld1, @fld2)", conn);
cmd.Parameters.Add("@fld1", OleDbType.VarWChar, 35, "fld1");
cmd.Parameters.Add("@fld2", OleDbType.VarWChar, 35, "fld2");
da.InsertCommand = cmd;
return da;
}
private void FillDataRow(string dn, string tn, Int32 psc, Int32 Form_ID,
string tif, string tifNonForm, string tct, string res, string img, string
extraSus, string extraImg,
DataTable tbl, OleDbDataAdapter bda, DataSet ds, Boolean tifFound, Boolean
tifNonFormFound, Boolean tctFound, Boolean resFound,
Boolean imgFound, Boolean extraSusFound, Boolean extraImgFound)
{
DataRow workrow;
try
{
workrow = tbl.NewRow();
workrow["DatabaseName"] = dn;
workrow["TableName"] = tn;
if ( psc > 0 )
workrow["PageSeq_cod"] = psc;
workrow["Form_ID"] = Form_ID;
if ( tif != "" )
workrow["tif"] = tif;
if ( tifNonForm != "" )
workrow["tifNonForm"] = tifNonForm;
workrow["tct"] = tct;
workrow["res"] = res;
if ( img != "" )
workrow["img"] = img;
workrow["extraSus"] = extraSus;
workrow["extraImg"] = extraImg;
workrow["tifFound"] = tifFound;
workrow["tifNonFormFound"] = tifNonFormFound;
workrow["tctFound"] = tctFound;
workrow["resFound"] = resFound;
workrow["imgFound"] = imgFound;
workrow["extraSusFound"] = extraSusFound;
workrow["extraImgFound"] = extraImgFound;
tbl.Rows.Add(workrow);
bda.Update(ds, tbl.TableName); // run SQL: insert statement
}
catch
{
MessageBox.Show("Post Error using DataRow.\n" );
throw;
}
}
with MS Access and ADO.Net and VS 2003.net
I create the connection and dataadapter initially in a function and return
them so I can reuse them
And in one case I would like a transaction. But when I fill the datarow it
fails for some reason.
I followed: http://support.microsoft.com/kb/310351
Do I need to create another dataadapter for this to work?
Thanks for any help.
The setup is like this:
DataSet dsTrans = dsVerify.GetChanges();
OleDbTransaction trVerify;
trVerify = vConnVerify.BeginTransaction(IsolationLevel.ReadCommitted);
vDA.SelectCommand.Transaction = trVerify;
try
{
FillDataRow( sDB, sOrig, psc, Form_ID, tif, tifNonForm, tct, res, img, "",
"", tblVerify, vDA, dsTrans, tifFound, tifNonFormFound, tctFound, resFound,
imgFound, false, false);
etc.
----------------------------------------------------------------------------
-------
Function that creates connection and dataadpter and update commands:
// Connect to a MS Access Database with standard security
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
sDataSource + ";User Id=admin;Password=");
conn.Open();
// Create an updateable DataTable in a DataSet
da = CreateVerifyDataAdapter( sTableName, conn );
ds = new DataSet();
da.Fill(ds, sTableName);
dt = new DataTable();
dt = ds.Tables[sTableName];
-------------------------------------------------------
private OleDbDataAdapter CreateVerifyDataAdapter(string sTableName,
OleDbConnection conn)
{
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand cmd;
// Create the SelectCommand.
cmd = new OleDbCommand("SELECT * FROM " + sTableName, conn);
da.SelectCommand = cmd;
// Create the InsertCommand.
cmd = new OleDbCommand("INSERT INTO " + sTableName + " (fld1, fld2 ) " +
"VALUES (@fld1, @fld2)", conn);
cmd.Parameters.Add("@fld1", OleDbType.VarWChar, 35, "fld1");
cmd.Parameters.Add("@fld2", OleDbType.VarWChar, 35, "fld2");
da.InsertCommand = cmd;
return da;
}
private void FillDataRow(string dn, string tn, Int32 psc, Int32 Form_ID,
string tif, string tifNonForm, string tct, string res, string img, string
extraSus, string extraImg,
DataTable tbl, OleDbDataAdapter bda, DataSet ds, Boolean tifFound, Boolean
tifNonFormFound, Boolean tctFound, Boolean resFound,
Boolean imgFound, Boolean extraSusFound, Boolean extraImgFound)
{
DataRow workrow;
try
{
workrow = tbl.NewRow();
workrow["DatabaseName"] = dn;
workrow["TableName"] = tn;
if ( psc > 0 )
workrow["PageSeq_cod"] = psc;
workrow["Form_ID"] = Form_ID;
if ( tif != "" )
workrow["tif"] = tif;
if ( tifNonForm != "" )
workrow["tifNonForm"] = tifNonForm;
workrow["tct"] = tct;
workrow["res"] = res;
if ( img != "" )
workrow["img"] = img;
workrow["extraSus"] = extraSus;
workrow["extraImg"] = extraImg;
workrow["tifFound"] = tifFound;
workrow["tifNonFormFound"] = tifNonFormFound;
workrow["tctFound"] = tctFound;
workrow["resFound"] = resFound;
workrow["imgFound"] = imgFound;
workrow["extraSusFound"] = extraSusFound;
workrow["extraImgFound"] = extraImgFound;
tbl.Rows.Add(workrow);
bda.Update(ds, tbl.TableName); // run SQL: insert statement
}
catch
{
MessageBox.Show("Post Error using DataRow.\n" );
throw;
}
}