How to query two access database(accdb) files that have different schema? - c#

I need your help.
Suppose there are two access database(accdb) files which have some columns with same names but some are not. For example:
start
Then how to make a query to both database at the same time?
OleDbConnection Conn = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=..\\DATABASE_A.accdb;");
OleDbCommand command = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable dt = new DataTable();
Con.Open();
string query = "Select [JOB],[DATE] From TABLE_A WHERE [NAME] = ?";
command.CommandText = query;
command.Parameters.AddWithValue("#NAME", $someDate);
command.Connection = Conn;
adapter.SelectCommand = command;
adapter.Fill(dt);
Conn.Close();
However, how about DATABASE_B.accdb to fill the same DataTable dt but TABLE_B's column has different name?I want to form a datatable like that:
result
Thanks for any suggestion.

In SQL you would use a union query.
In C# you could use LINQ Union.

Related

How to select non-English characters from US7ASCII type DB use C# OLEDB

The character set of the target Oracle DB is US7ASCII, but Korean characters are stored.
When I select the data In my C# program using OLEDB and look at the datagridview, Korean characters are shown as ???.
Environment.SetEnvironmentVariable("NLS_LANG", "AMERICAN_AMERICA.KO16KSC5601");
string connectionString = "Provider=OraOLEDB.Oracle;Data Source=DB;User ID=ID;Password=PW;";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand command = new OleDbCommand();
command.CommandText = "SELECT nm FROM table";
command.Connection = connection;
DataTable dataTable = new DataTable();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command);
dataAdapter.Fill(dataTable);
dataGridView1.DataSource = dataTable;
connection.Close();
The code above is e.g. of my program. I need help to solve this.

C# : display SQL query on a chart using SqlDataReader

This is my query I want to display it like this: sum of commands by command date (each date should have a Bar that shows how many sales (commands) by that particular date (day)
select
date_comm as 'jourdetravail',
sum(TotalCom) as 'recettedujour'
from
Commande_art
group by
date_comm
You can use this script to fill a datatable and then use the datatable as source for the chart and also you will need the chart to be indexed on x axis
SqlConnection con = new SqlConnection("Data Source=<Your Server>;Initial Catalog=<YourDatabase>;Persist Security Info=True;User ID=<yourID>;Password=<yourPassword>");
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select [date_comm] as 'jourdetravail', sum([TotalCom]) as 'recettedujour' from [dbo].[Commande_art] group by [date_comm]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
and then your "dt":
<yourChart>.DataSource = dt;
You will also need to set the 2 series of the chart with the same name of the column of the quesry in order to show them correctly

How to run SQL that has declare statements via SqlCommand?

This is a sample query:
declare #tempTable table
(
Id bigint
)
-- populating with temp Ids
select *
from TargetTable
where Id not in
(
select Id
from #tempTable
)
And this is C# code:
public DataTable Get(string sql)
{
var dataTable = new DataTable();
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand())
{
connection.Open();
command.Connection = connection;
command.CommandText = sql;
var dataReader = command.ExecuteReader();
dataTable.Load(dataReader);
}
return dataTable;
}
Running this code throws exception, complaining that:
Incorrect syntax near the keyword 'declare'.
Incorrect syntax near
')'.
I know it's possible to use join instead of #tempTable, but is there a way to run this query?
A SqlDataAdapter object can be used to populate a DataTable as follows. Instead of calling SqlCommand.ExecuteReader(), the SqlDataAdapter.Fill() method is used. In this example the argument to the Fill() method is the DataTable that will be populated. While this approach will work with the query you posted with the table variable, I'd strongly recommend converting this to a stored procedure and filling the DataTable from this instead. Additionally, unless the amount of data that's being sent into the table variable is very small using a temp table will offer more functionality (more accurate statistics, better DML performance, ROLLBACK participation, etc.) than table variables and I'd suggest using a temp table instead as well.
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dataTable = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand command = new SqlCommand(cmd, connection);
da.SelectCommand = command;
connection.Open();
da.Fill(dataTable);
}
Stored Procedure Call:
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataTable dataTable = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
//use SP name for command text
SqlCommand command = new SqlCommand("usp_ProcedureName", connection);
//stored procedure command type
command.CommandType = CommandType.StoredProcedure;
da.SelectCommand = command;
connection.Open();
da.Fill(dataTable);
}
You can create a Stored Procedure of your query then add this to your code:
command.CommandType = CommandType.StoredProcedure;
Then use exec command
command.CommandText = "exec procedureName"
If your query location is in database itself.

OracleDataAdapter not filling DataTable when querying to a DBLink

I'm trying to execute a query from OracleDataAdapter in C#, this query will go to a DBLink available in the DB, but for some reason when it needs to fill the datatable it throws me an error:
IndexOutOfRangeException - ndex was outside the bounds of the array
the code I'm trying to execute is:
OracleConnection conn = new OracleConnection(connectionString);
conn.Open();
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand("select count(*) alive from dual#MYDBLINK");
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet); //Here is where it fails
}
The query works if I execute in sql developer, and the code works if I remove the DBLink information and let the query as select count(*) alive from dual, so I asume, that the issue here is something with the DBLink or the # character.

Parameter not changing in OleDbCommand

This is a simple search page (search.aspx?title=Hello) and I want to query the db for like matches. According to the microsoft docs (http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx see: Remarks) this is the correct way to do it, but the parameter (?) never gets set to the value of the query string.
string sqlcmd = "SELECT * FROM TableName WHERE Title LIKE ?";
OleDbCommand command = new OleDbCommand(sqlcmd, sqlcon);
OleDbParameter p1 = new OleDbParameter("#p1", OleDbType.WChar);
p1.Value = Request.QueryString["title"];
OleDbDataAdapter da = new OleDbDataAdapter(command);
da.SelectCommand.Parameters.Add(p1);
DataTable dt = new DataTable();
da.Fill(dt);
The parameter never changes to what the query string was, it just executes the query
SELECT * FROM Table WHERE Title LIKE ?
Could you try the following:
"SELECT * FROM Table WHERE Title LIKE #p1"
I think that is the convention when using parameters in ADO.Net command text.
Here is my solution, you need to have single quotes around the question mark for the SQL to work. Complete solution:
sqlcon.Open();
string sqlcmd = "SELECT * FROM TableName WHERE Title LIKE '%?%'";
OleDbCommand command = new OleDbCommand(sqlcmd, sqlcon);
command.Parameters.Add(new OleDbParameter("p1", Request.QueryString["Title"]));
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);

Categories