I am writing a C# program involves of an Oracle database using Oracle.DataAccess.Client -> OracleConnection
Though, I have a question about reusing the same connection to fetch data from the same database. Here's what I've done.
|
Then I use that connection to fetch data from the database into an OracleDataAdapter
|
Then I add a column into the database using the connection
|
Then I try to use the connection to fetch data again using OracleDataReader
|
But when I try to look for the new column that I found, it said the table doesn't have that new column I've just added, though it still has the old table structure. So I did many...many tests and found that:
1) If I use a new connection, I can fetch the newly added column (using select * from test)
2) Or I can fetch the newly added column using the old connection (using select FID from test), (select * from test) will not have the new column 'ID' that I've added
3) Or if I didn't fetch the data at the beginning (select * from test), I can fetch the newly added column after adding the table column (select * from test)
It seems like the memory hold on to the first data structure and giving the same stuff at the 2nd fetch, so I wonder why this is happening, is that a way that I can release the memory of the connection? This is just my guess-imate.
Can anyone explain why this is happening? Thanks!
Suthish NairPosted Feb 28, 2011, 1:35 PM
RichardPosted Feb 28, 2011, 11:08 AM
string OracleConString = "User Id=user;Password=pass;Data Source=source;Statement Cache Size=0;Connection Lifetime=0";
DataSet ds_CvnDataset = new DataSet("ds_CvnDataset");
OdbcDataAdapter CvnAdapter = new OdbcDataAdapter();
DataTable dt_CvnData = new DataTable();
CvnAdapter.Fill(ds_CvnDataset, "ds_CvnDataset");
dt_CvnData = ds_CvnDataset.Tables["ds_CvnDataset"];
DataSet ds_CopyToGISSet = new DataSet("CopyToGISSet");
OracleDataAdapter CopyToGISAdapter = new OracleDataAdapter();
DataTable dt_CopyToGISTable = new DataTable();
OracleCommandBuilder CopyToGISBuilder = new OracleCommandBuilder(CopyToGISAdapter);
CopyToGISAdapter.SelectCommand = new OracleCommand("select * from pole_database_bk", OracleCon);
CopyToGISAdapter.Fill(ds_CopyToGISSet, "CopyToGISSet");
dt_CopyToGISTable = ds_CopyToGISSet.Tables["CopyToGISSet"];
string alterColSQL = "alter table pole_database_bk add FID number(11)";
OracleCommand alterCmd = new OracleCommand(alterColSQL, OracleCon);
alterCmd.ExecuteNonQuery();
string GISReadSQL = "select * from pole_database_bk";
OracleCommand GISReadCmd = new OracleCommand(GISReadSQL, OracleCon);
OracleDataReader GISReader = GISReadCmd.ExecuteReader();
DataTable GISReaderTable = new DataTable("GISReader");
GISReaderTable.Load(GISReader);
MPdataset.Tables.Add(GISReaderTable);
oDataGrid.SetDataBinding(MPdataset, "GISReader");
Suthish NairPosted Feb 28, 2011, 10:08 AM
Its working for me..
New column get added.
Can also fetch the new column.
using System.Data.OracleClient;
string OracleConString = "Password=111;User id=1111;Data Source=hello;Connection Lifetime=0";
OracleConnection OracleCon = new OracleConnection(OracleConString);
OracleCon.Open();
OracleDataAdapter Adapter = new OracleDataAdapter();
Adapter.SelectCommand = new OracleCommand("select * from test1", OracleCon);
DataSet ds = new DataSet();
Adapter.Fill(ds);
string alterColSQL = "alter table test1 add ID number(11)";
OracleCommand alterCmd = new OracleCommand(alterColSQL, OracleCon);
alterCmd.ExecuteNonQuery();
string ReadSQL = "select * from test1";
OracleCommand ReadCmd = new OracleCommand(ReadSQL, OracleCon);
OracleDataReader GISReader = ReadCmd.ExecuteReader();
DataTable ReaderTable = new DataTable();
ReaderTable.Load(GISReader);
ds.Tables.Add(ReaderTable);
OracleCon.Close();
Mahesh ChandPosted Feb 28, 2011, 9:28 AM
RichardPosted Feb 28, 2011, 9:19 AM
Mahesh ChandPosted Feb 25, 2011, 8:46 PM
RichardPosted Feb 25, 2011, 5:04 PM
I did more research and suspected it may be the statement caching in the connection string but give me the same result.
RichardPosted Feb 18, 2011, 1:38 PM
Guest UserPosted Feb 18, 2011, 1:01 PM
RichardPosted Feb 17, 2011, 2:22 PM
plus the 'alter' command should be auto commit during transaction.
Guest UserPosted Feb 17, 2011, 2:17 PM