MySQL Parameterized Select Query joining tables issue - c#

I am using parameterized select queries in conjunction with my program and as a whole, it works except when I try to join tables.
Here is a small excerpt what I have done:
using (MySqlCommand cmd = new MySqlCommand(paramQuery.ToSql(), connection)) {
/*
paramQuery.ToSql() equals:
"Select tableOne.ID, tableOne.Department, tableTwo.Name
From tableOne, tableTwo
Where tableOne.ID = #param0"
*/
for (int index = 0; index < paramQuery.Parameters().Count; index++)
cmd.Parameters.AddWithValue(paramQuery.Parameters().ElementAt(index).Key,
paramQuery.Parameters().ElementAt(index).Value);
/*
paramQuery.Parameters().ElementAt(index).Key = "#param0"
paramQuery.Parameters().ElementAt(index).Value = "tableTwo.ID"
*/
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) {
// Do stuff
}
}
One of the tables that I am try to join will retrieve everyone in tableTwo with the same ID for all. Is there something that I am doing wrong here?

try this:
Select tableOne.ID, tableOne.Department, tableTwo.Name
From tableOne join tableTwo
on tableOne.ID=tableTwo.ID
Where tableOne.ID = #param0
as you understand tableOne.ID=tableTwo.ID is the name of the columns which you joining tables on. At your current example I do not see how it can work.
There is also Left join and right join which serves each to a different purposes.
Here is some example to look how it works: http://dev.mysql.com/doc/refman/5.0/en/join.html

Related

C#: How to get data from the last row in database? I'm using in visual studio 2017

Need help in C# ( Visual Studio 2017 )
I want to get a data from my database from the last row or last inputted data.
My code is here..
String query = "SELECT TOP(1) MessageNumber FROM ncslbpHighWay";
SqlCommand SDA = new SqlCommand(query, AR);
SqlDataReader data = SDA.ExecuteReader();
if (data.Read())
{
textBox2.Text = data.GetValue(0).ToString();
}
AR.Close();
I already got the data but from the specific column at the top only. I don't know how to get the bottom value.
Also i tried the DESC but it doesn't work.
String query = "SELECT TOP(1) MessageNumber FROM ncslbpHighWay ORDER BY COLUMN DESC";
This is my first question here in Stackoverflow. I hope someone would help me on this.
Considering your question below snippet can help you.
-- Method 01--
SELECT * FROM TestData where ID =(Select Max(ID) from TestData)
-- Method 02--
SELECT top 1 * FROM TestData order by ID Desc;
Here I have consider ID columns as Auto Increment.
Your specified query below works for SQL Server for getting last value in table:
SELECT TOP(1) MessageNumber FROM ncslbpHighWay ORDER BY [ColumnName] DESC
However since you're mentioning MySQL tag (implying you're using MySQL instead of SQL Server), you need to use LIMIT n after ORDER BY instead, where n is the number of returned results, i.e. 1 for single result:
SELECT MessageNumber FROM ncslbpHighWay ORDER BY [ColumnName] DESC LIMIT 1
-- or using offset:
SELECT MessageNumber FROM ncslbpHighWay ORDER BY [ColumnName] DESC LIMIT 0, 1
If you're using MySql.Data.MySqlClient.MySqlConnection instead using standard System.Data.SqlClient.SqlConnection, you can write data retrieval like this:
using (MySqlConnection AR = new MySqlConnection())
{
AR.Open();
String query = "SELECT MessageNumber FROM ncslbpHighWay ORDER BY [ColumnName] DESC LIMIT 1";
using (MySqlCommand SDA = new MySqlCommand(query, AR))
{
MySqlDataReader data = SDA.ExecuteReader();
if (data.Read())
{
textBox2.Text = data.GetValue(0).ToString();
}
}
AR.Close();
}
NB: I recommend you using ORM (e.g. Entity Framework) with MySQL Connector .NET library to enable LINQ functionality when managing database queries.
Similar issues:
How to select the last record from MySQL table using SQL syntax
Select last row in MySQL
you can use orm. Things will become easy.

Database table not showing data after inner join

I have a C# windows forms application and I'm working with a database too.
I have 3 tables: Players, Tournaments and TournamentsPlayers.
I will only show 2 tables in the form, the Tournaments and the Players tables. The Tournaments table has a column which is a checkbox and I want to show in the Players table only the players from the tournaments which I checked the box.
When the application starts I have a foreach statement which check all the tournaments, so at first all the players should show in the Players table, but no player is shown.
My code is:
private void ShowPlayersForSelectedTournaments()
{
string query =
"SELECT a.Name, a.Id FROM Players a,TournamentPlayers b WHERE a.Id=b.TournamentId AND b.TournamentId=#TournamentId";
using (Connection = new SqlConnection(ConnectionString))
using (SqlCommand command = new SqlCommand(query, Connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
foreach (DataGridViewRow row in TournamentsTable.Rows)
{
if (row.Cells.Count > 0 && bool.Parse(row.Cells[0].Value.ToString()))
{
command.Parameters.AddWithValue("#TournamentId", row.Cells[1].Value);
DataTable tournamentPlayersList = new DataTable();
adapter.Fill(tournamentPlayersList);
PlayersTable.DataSource = tournamentPlayersList;
break;
}
}
}
}
I have tried a simple select and all is working, but with this sql syntax it doesn't seems to work.
I have tried with another similar mysql syntax, like:
string query = "SELECT a.Name FROM Players a INNER JOIN TournamentPlayers b ON a.Id=b.TournamentId WHERE b.TournamentId=#TournamentId";
But still nothing shows in the Players table.
What am I doing wrong? Is there something wrong with my mysql syntax or my code?
This is your query:
SELECT a.Name, a.Id
FROM Players a,TournamentPlayers b
WHERE a.Id=b.TournamentId AND b.TournamentId=#TournamentId;
It has multiple problems. This should work:
SELECT p.Name, p.Id
FROM Players p JOIN
TournamentPlayers tp
ON tp.playerId = p.id
WHERE tp.TournamentId = #TournamentId;
Notes:
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
Use abbreviations for the table names, so the query makes sense when read.
FIx the join condition between the tables, to match on the player. It is highly unlikely that a player has the same id as a tournament and also played in that tournament. Possible, but unlikely.

How to Get Extra Column Value From Stored Procedure using Entity Framework 6?

Basically i'm using NopCommerce 3.2. I have modified the stored procedure so it could return CategoryName associated with Prodcut details such as;
--return products
SELECT TOP (#RowsToReturn)
p.*, c.Name as CategoryName
FROM
#PageIndex [pi]
INNER JOIN Product p with (NOLOCK) on p.Id = [pi].[ProductId]
-- Custom INNER JOIN To Get CategoryName
INNER JOIN Product_Category_Mapping pc ON pc.ProductId = p.Id
INNER JOIN Category c ON c.Id = pc.CategoryId
WHERE
[pi].IndexId > #PageLowerBound AND
[pi].IndexId < #PageUpperBound
ORDER BY
[pi].IndexId
As you can see ResultSet the CategoryName is retrieved;
In my Code, i have created a custom property with ignor mapping;
// Product.cs
public string CategoryName { get; set; }
// another mapping class i.e ProductMapp.cs
this.Ignore(p => p.CategoryName);
The actual ResultSet is retrieved using this code (minimezed);
// some parameters sent by nopcommerce
var pTotalRecords = _dataProvider.GetParameter();
pTotalRecords.ParameterName = "TotalRecords";
pTotalRecords.Direction = ParameterDirection.Output;
pTotalRecords.DbType = DbType.Int32;
.........
//invoke stored procedure
var products = _dbContext.ExecuteStoredProcedureList<Product>(
"ProductLoadAllPaged",
.............
.............
pTotalRecords);
The Method (ExecuteStoredProcedureList) definition is here;
Note: The method code is too long to paste, so i have tried to paste the execution and mapping code only
//var connection = context.Connection;
var connection = this.Database.Connection;
//Don't close the connection after command execution
//open the connection for use
if (connection.State == ConnectionState.Closed)
connection.Open();
//create a command object
using (var cmd = connection.CreateCommand())
{
//command to execute
cmd.CommandText = commandText;
cmd.CommandType = CommandType.StoredProcedure;
// move parameters to command object
if (parameters != null)
foreach (var p in parameters)
cmd.Parameters.Add(p);
//database call
var reader = cmd.ExecuteReader();
//return reader.DataReaderToObjectList<TEntity>();
var result = context.Translate<TEntity>(reader).ToList();
for (int i = 0; i < result.Count; i++)
result[i] = AttachEntityToContext(result[i]);
//close up the reader, we're done saving results
reader.Close();
return result;
}
As you can see it is mapping to a Product entity. However, in my result set the coloumn can't be mapped automatically, it is always null. I would like to get CategoryName coloumn value mapped to CategoryName property. How can i accomplish this ?
You can create another Class in Nop.Core with required field and rather that returning product table return that table. This class can be temporary by creating table in stores procedure itself and dropping at the end.

dynamic sql generation is not supported against multiple base tables

I tried to add a new row to a Table in an SQL DB, but I had a problem :
dynamic sql generation is not supported against multiple base tables
this is the code I tried :
private MyClass myClass = new MyClass();
private SqlDataAdapter adapter;
private SqlDataAdapter adapter2;
private void GestionCollections_Load(object sender, EventArgs e)
{
adapter = new SqlDataAdapter("select Id_Collection ID, Libelle_Collection Collection,Libelle_Editeur Editeur from Collection_ left join Editeur on Id_Editeur = Collection_.Id_Editeur_Editeur", myClass.cnx);
adapter.Fill(myClass.ds, "Collection_");
adapter2 = new SqlDataAdapter("Select Id_Editeur ID,Libelle_Editeur Editeur from Editeur", myClass.cnx);
adapter2.Fill(myClass.ds, "Editeur");
}
private void AjouterBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
String newKeyWordCollectionName = ajoutCollection.KeyWordCollectionName;
String newKeyWordAEditeurName = ajoutCollection.KeyWordEditeurName;
DataRow row = myClass.ds.Tables["Collection_"].NewRow();
row[1] = newKeyWordCollectionName;
foreach(var myRow in myClass.ds.Tables["Editeur"].AsEnumerable())
{
if (newKeyWordAEditeurName == myRow[1] as String)
row[2] = (int)myRow[0];
}
myClass.ds.Tables["Collection_"].Rows.Add(row);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(myClass.ds, "Collection_");
}
Change your select query and add distinct with inner join.
For example there are two query from which you can understand that what I want to tell you
Wrong query
select iop.pob_id, iop.pob_product_id, iop.pob_qty, iop.pob_unit_id
, iop.pob_rate, iop.pob_value, iop.pob_fiscalyear_id
, **p.product_desc** as orderBy from inv_product_open_balc iop
left join inv_product p on iop.pob_product_id = p.product_id
where p.product_desc like 'Air Freshner%' and iop.pob_fiscalyear_id = 3
Correct query
select distinct iop.pob_id, iop.pob_product_id, iop.pob_qty
, iop.pob_unit_id, iop.pob_rate, iop.pob_value, iop.pob_fiscalyear_id
, **(select Product_desc from** inv_product p where p.product_id = iop.pob_product_id )as orderBy
from inv_product_open_balc iop
inner join inv_product p on iop.pob_product_id = p.product_id
where p.product_desc like 'Air Freshner%' and iop.pob_fiscalyear_id = 3
You can't use an SqlCommandBuilder here:
Automatically generates single-table commands that are used to reconcile changes made to a DataSet with...
The key words here being "single-table". It has no way to reverse engineer from the SELECT statement how a specific update should be applied (e.g. if you NULL all of the columns from the right side of a left join, should it delete the row, or set each column to null.
You need to author appropriate Insert, Update and Delete commands on the SqlDataAdapter.
With SqlCommandBuilder you can generate CRUD operation on entity
Requirement of use is to define Select command before inserting , and include in the select command your primary Key.
Link : http://msdn.microsoft.com/fr-fr/library/system.data.sqlclient.sqlcommandbuilder(v=vs.80).aspx
MSDN Definition : Automatically generate Transact-SQL statements to update single table
Nota : In your Update selectCommand, you defined left join query, and so you can create left join query, replace this query with just select.
Add DISTINCT in your select statement with inner join.
This will solve the issue.
like Select Distinct Employee.Ecode, .........

Find underlying tables of views using Linq to Entity to make Aggregate Dependencies

I have a function:
public static List<T> EntityCache<T>(this System.Linq.IQueryable<T> q, ObjectContext dc, string CacheId)
{
try
{
List<T> objCache = (List<T>)System.Web.HttpRuntime.Cache.Get(CacheId);
string connStr = (dc.Connection as System.Data.EntityClient.EntityConnection).StoreConnection.ConnectionString;
if (objCache == null)
{
ObjectQuery<T> productQuery = q as ObjectQuery<T>;
string sqlCmd = productQuery.ToTraceString();
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
{
conn.Open();
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn))
{
string NotificationTable = q.ElementType.Name;
System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd);
cmd.ExecuteNonQuery();
objCache = q.ToList();
System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep);
}
}
}
return objCache;
}
catch (Exception ex)
{
throw ex;
}
}
q can be a table, view or a procedure.
What i want is to find the underlying tables associated with a view or a procedure.
like if q is a join of tow tables i want to get the name of both the tables and finally
execute like:
If there are tw0 tables say A and B
Then i need to make Aggregate Dependency like:
string sqlCmd1 = string.Empty;
string sqlCmd2 = string.Empty;
using (testEntities ctx1 = new testEntities())
{
sqlCmd1 = ((System.Data.Objects.ObjectQuery)(from p in ctx1.A select p)).ToTraceString();
sqlCmd2 = ((System.Data.Objects.ObjectQuery)(from p in ctx1.B select p)).ToTraceString();
}
System.Data.SqlClient.SqlCommand cmd1 = new System.Data.SqlClient.SqlCommand(sqlCmd1, conn);
System.Data.SqlClient.SqlCommand cmd2 = new System.Data.SqlClient.SqlCommand(sqlCmd2, conn);
System.Web.Caching.SqlCacheDependency
dep1 = new System.Web.Caching.SqlCacheDependency(cmd1),
dep2 = new System.Web.Caching.SqlCacheDependency(cmd2);
System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency();
aggDep.Add(dep1, dep2);
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
then the query i want to execute is
select * from A;
select * from B;
This i am using for SqlCacheDependency using Linq to Entity.
It works well for views when i hardcode the underlying tables but now i want the code automatically check for the underlying tables
and execute nonquery like
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
and make aggregate dependencies.
Any help is appreciated.
Thanks.
You must use database level tools to find which database objects your views or stored procedures depends on (but it also means you must know their full names in the database). For example SQL server offers sp_depends system stored procedure to track dependencies. This can be quite complicated because dependencies can have multiple levels (procedure can be dependent on view, view can be dependent on another view, etc.).
Be aware that advanced EF mapping also allows writing SQL directly to EDMX and in such case you will have to parse ToTraceString to find database objects.
I have found a solution for the problem i have posted.
There is a query that is valid for sql server 2005 onward.
We need to pass the name of the object and it will return us the name of the tables on which it depends
Example:
The name of the View is say AllProducts_Active_Inactive
;WITH CTE AS (SELECT o.name
, o.type_desc
, p.name
, p.type_desc as B
, p.object_id
FROM sys.sql_dependencies d
INNER JOIN sys.objects o
ON d.object_id = o.object_id
INNER JOIN sys.objects p
ON d.referenced_major_id = p.object_id
where o.name = 'AllProducts_Active_Inactive'
UNION ALL
SELECT o.name
, o.type_desc
, p.name
, p.type_desc as B
, p.[object_id]
FROM sys.sql_dependencies d
INNER JOIN CTE o
ON d.object_id = o.object_id
INNER JOIN sys.objects p
ON d.referenced_major_id = p.object_id
where o.name = 'AllProducts_Active_Inactive'
)
SELECT DISTINCT * FROM [CTE]
where B = 'USER_TABLE'
This post is the modified answer of the question i have posted on the website:
http://ask.sqlservercentral.com/questions/81318/find-the-underlying-tables-assocaited-with-a-view-or-a-stored-procedure-in-sql-server
What i changed is added the line where B = 'USER_TABLE'
Which means only those dependencies are returned who are tables.
And the seconds thing is added a WHERE clause so that a specific object is found.
Thanks

Categories

Resources