OracleDataAdapter with GlobalTemporaryTable: FillSchema not working - sql

I'm using FillSchema on a OracleDataAdapter in .net. The SelectCommand works on a global temporary table.
For the first run, this works great. I will get the schema of the global temporary table
Then I drop that temporary table and great a new temporary table with a different schema.
After that, for the second run, the FillSchema method will still return the schema from the old dropped temporary table.
Am I missing something? Shouldn't the select command query the schema from the new version of the temp table?
Thanks for any help!

What did not work:
The OracleDataAdapter.SelectCommand that was used by
FillSchema was:
Select * from TableName
What works:
I needed to change that query to the exact schema,
then it works:
Select column1,column2,column3 from TableName
I don't understand exactly why, but that solved my problem.
It will return the schema of the new global temporary table.
Is this a caching problem of the oracle server? Because the
tableName is always the same?

To use procedure ways to solve this Problem
OracleParameter inputParam = new OracleParameter("TABLE_NAME_IN",OracleDbType.Varchar2,"TEST",ParameterDirection.Input); //Query TableName
OracleParameter refParam = new OracleParameter("OUTPUT",OracleDbType.RefCursor,ParameterDirection.Output);//RefCursor
DataTable dt = new DataTable();//Fill DataTable
using (OracleCommand dbCommand = new OracleCommand("PKG_SYS.SELECT_TABLE_DATA",orclConnection))
{
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.Parameters.Add(inputParam);
dbCommand.Parameters.Add(refParam);
using (OracleDataAdapter da = new OracleDataAdapter())
{
da.SelectCommand = dbCommand;
da.Fill(dt);
}
}

Related

How to use variable (datatable) in sql command text to execute

I am new to Vertica and trying this in angular aspx page.
`con.Open();
cmd = con.CreateCommand();
cmd.Connection = con;
cmd.Parameters.Add(new VerticaParameter("#tblCustomers", table));
cmd.CommandText = "insert into customers select * from #tblCustomers";
cmd.ExecuteNonQuery();`
I have established the connection and inserted some fresh records too.
But Now I am trying to insert bulk records in my vertica database's table.
Something Same like SqlServer,
I have loaded my table data into "table" variable. Which is a datatable.
Is it possible to do like this ?? As I am getting some error
"
Incorrect syntax at or near $1"
customers and #tblCustomers both have same columns.
Thanks!
Setting aside whether or not you should do something like this, in the code that you've posted you're passing #tblCustomers as a parameter to the query, so it's going to treat it as a string value, not an object name in the query. You need to build the CommandText in your code without that as a parameter. Something like:
cmd.CommandText = "insert into customers select * from " & tableName
(Sorry if that syntax isn't quite right, but hopefully it gets across the point)
Some additional (and important) notes though:
Always use a column list when doing an INSERT. Use INSERT INTO MyTable (some_column, some_other_column) SELECT... not INSERT INTO MyTable SELECT...
NEVER use SELECT *. List out your column names.

Display Table Records from AS400 to Visual Basic

I'm trying to display the records from PYLEAVE table, but when I use this code, it shows error SQL0204, can someone help me?
Call takeconnectionas400()
conn.Close()
conn.Open()
adapter = New OleDbDataAdapter("select * from PRIMA.PYLEAVE", conn)
ds = New DataSet
adapter.Fill(ds, "PRIMA.PYLEAVE")
DGVAS400.DataSource = ds.Tables("PRIMA.PYLEAVE")
DGVAS400.ReadOnly = True
In DB2 for IBM i, the syntax for the table is not DBNAME.TABLENAME it is SCHEMA.TABLENAME In old AS/400 terminology, that is LIBRARY.FILE Have your IBM i administrator tell you what schema (library) the table is in and change your SELECT statement appropriately.
I don't know VB, but in JDBC you specify the database name in the connection string, not the SELECT statement.

Can I first SELECT and then DELETE records in one t-SQL transaction?

I can't figure out if this is an acceptable operation. I need to select records from the SQL Server 2008 database and then delete them, all as a single transaction from an ASP.NET code. Note that the .NET code must be able to retrieve the data that was first selected.
Something as such:
SELECT * FROM [tbl] WHERE [id] > 6;
DELETE FROM [tbl] WHERE [id] > 6
I'm trying it with the SQL Fiddle but then if I do:
SELECT * FROM [tbl]
I get the full table as if nothing was deleted.
EDIT As requested below here's the full .NET code to retrieve the records:
string strSQLStatement = "SELECT * FROM [tbl] WHERE [id] > 6;" +
"DELETE FROM [tbl] WHERE [id] > 6";
using (SqlCommand cmd = new SqlCommand(strSQLStatement, connectionString))
{
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while(rdr.Read())
{
//Read values
val0 = rdr.GetInt32(0);
val3 = rdr.GetInt32(3);
//etc.
}
}
}
This will do the select and delete simultanious:
delete from [tbl] output deleted.* WHERE [id] > 6
It is possible to select and delete in the same transaction as long as both operations are enlisted in the same transaction.
Look at this post
Transactions in .net
The "easiest" way to achieve transactions with a compatible provider (SQL Server works great!) is to use a TransactionScope. Just make sure the scope is created before the connection is opened so that everything is correctly enlisted.
The content of the SelectStuff and DeleteStuff methods doesn't matter much - just use the same connection, don't manually mess with the connection or with transactions, and perform the SQL operations however is best.
// Notes
// - Create scope OUTSIDE/BEFORE connection for automatic enlisting
// - Create only ONE connection inside to avoid DTC and "advanced behavior"
using (var ts = new TransactionScope())
using (var conn = CreateConnection()) {
// Make sure stuff selected is MATERIALIZED:
// If a LAZY type (Enumerable/Queryable) is returned and used later it
// may cause access to the connection outside of when it is valid!
// Use "ToList" as required to force materialization of such sequences.
var selectedStuff = SelectStuff(conn);
DeleteStuff(conn);
// Commit
ts.Complete();
// Know stuff is deleted here, and access selected stuff.
return selectedStuff;
}
The return value from multiple SQL statements is the result of the last statement run, which in this case is the DELETE. There are no rows returned from a DELETE, so there is nothing to read for val0 and val3.
There are two solutions I can think of here:
Change your code to expressly start a transaction, perform the SELECT, read the values, and then issue the DELETE, or
SELECT into a #temp table, execute the DELETE, and then SELECT from the #temp table, do what you need to with the rows, and then DROP th.

SQLite INNER JOIN with ATTACHed databases

I have two SQLite databases with identical table structures that I need to query using VB.Net, but I can figure out pretty much any syntax. I am trying to figure out how to build the query string? This query works correctly:
sb = New StringBuilder("SELECT Master.Name, Master.ID, Master.StartDate, Master.Supervisor, Log.LogType, Log.LogComment FROM Master INNER JOIN Log ON Master.ID = Log.ID WHERE date(Log.LogDate) = '")
sb.Append(calendarDate.ToString("yyyy-MM-dd"))
sb.Append("' ORDER BY Master.Name;")
c = New SQLiteCommand(sb.ToString, _Conn)
Using dr As SQLiteDataReader = c.ExecuteReader
...
I can also "attach" the second database with the following:
sb = New StringBuilder("ATTACH DATABASE '")
sb.Append(outDBPath)
sb.Append("' AS db2;")
c = New SQLiteCommand(sb.ToString, _Conn)
c.ExecuteNonQuery()
c.Dispose()
My question is, what is the syntax to query both databases? Is this even possible? In the past I used a For... Next loop and just changed the connection from the first database to the second.
http://www.sqlite.org/lang_attach.html:
Tables in an attached database can be referred to using the syntax
database-name.table-name. If the name of the table is unique across
all attached databases and the main and temp databases, then the
database-name prefix is not required. If two or more tables in
different databases have the same name and the database-name prefix is
not used on a table reference, then the table chosen is the one in the
database that was least recently attached.
So you use db2.Master to refer to the Master table in the attached database.

How to create database and add 2 table in SQLite

I am using SQLite ADO.NET Provider.
I want to create a database with 2 tables via code in vb.net .
Please provide code for the same.
I am using VS 2010 Winforms, working under XP SP3 Pro
Use the SQLiteConnection's CreateFile() method.
SQLiteConnection.CreateFile("c:\\mydatabasefile.db3")
More info on the System.Data.SQLite forums
You can then send ad-hoc CREATE TABLE statements to the engine:
dim myTableCreate as String =
"CREATE TABLE MyTable(CustomerID INTEGER PRIMARY KEY ASC,
FirstName VARCHAR(25));"
cmd.CommandText = myTableCreate
cmd.ExecuteNonQuery()
More on SQLite CREATE TABLE.
For those who need this, here is an updated working code...
SQLiteConnection.CreateFile("c:\mydatabasefile.db3")
Using Query As New SQLiteCommand()
Connection.ConnectionString ="DataSource=c:\mydatabasefile.db3;Version=3;New=False;Compress=True;"
Connection.Open()
With Query
.Connection = Connection
.CommandText = "CREATE TABLE MyTable(CustomerID INTEGER PRIMARY KEY ASC, FirstName VARCHAR(25))"
End With
Query.ExecuteNonQuery()
Connection.Close()
End Using