I wrote this SQL statement, then my SQL server was running.
SELECT r.RentID,c.CustomerName,v.VehicleName,r.[Hours], r.[Hours] * v.Rent AS Total FROM Rent as r
INNER JOIN Customer AS c ON r.CustomerID = c.CustomerID
INNER JOIN Vehicle AS v ON r.VehicleID = v.VehicleID
Also when I write in C #, it does not display, although some SQL statements such as Add, Update, Delete are running normally.
I want to ask where I'm wrong and how to fix it. Thank you very much.
public IList getRents()
{
IList result = new List();
try
{
SqlConnection con = new SqlConnection(sCon);
String query = "SELECT r.RentID,c.CustomerName,v.VehicleName,r.[Hours], r.[Hours] * v.Rent AS Total FROM Rent as r INNER JOIN Customer AS c ON r.CustomerID = c.CustomerID INNER JOIN Vehicle AS v ON r.VehicleID = v.VehicleID";
//String query = "SELECT * FROM Rent";
SqlCommand com = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Rent r = new Rent();
r.rentID = (int)reader[0];
r.customerID = (int)reader[1];
r.vehicleID = (int)reader[2];
r.hours = (int)reader[3];
//r.total = (int)reader[4];
result.Add(r);
}
con.Close();
}
catch (Exception ex)
{
ex.ToString();
}
return result;
}
It looks like you're trying to map the SQL column CustomerName, which is likely a VARCHAR or string-compatible type of some kind, to an int.
Rent r = new Rent();
r.rentID = (int)reader[0];
r.customerID = (int)reader[1]; // here's your problem: reader[1] is CustomerName
r.vehicleID = (int)reader[2];
r.hours = (int)reader[3];
//r.total = (int)reader[4];
result.Add(r);
How you resolve it depends on what you're trying to accomplish.
If you want CustomerName and not CustomerId, you'll need to add a property called CustomerName with type string to your Rent model and update your reader:
Rent r = new Rent();
r.rentID = (int)reader[0];
r.customerName = (string)reader[1]; // updated
r.vehicleID = (int)reader[2];
r.hours = (int)reader[3];
//r.total = (int)reader[4];
result.Add(r);
If you want CustomerId, then you need to change your SQL:
SELECT r.RentID, c.CustomerId, v.VehicleName, r.[Hours], r.[Hours] * v.Rent AS Total FROM Rent as r INNER JOIN Customer AS c ON r.CustomerID = c.CustomerID INNER JOIN Vehicle AS v ON r.VehicleID = v.VehicleID
More Information
There are some issues with your code that prevented you from debugging what was wrong.
First, you're catching any exceptions that can possibly be thrown and swallowing them up. getRents() will always successfully return, but if there's any errors at all, it'll return an empty List and you'll never know why. My guess is that's why you're saying "it doesn't display".
For the purposes of debugging, remove the try catch block. You can add it later but only do so if you're planning on doing something with the exception, such as displaying friendly error messages and/or logging them.
When you run your code without the try catch block, it will throw an exception that should help you figure out what was wrong.
There are some other issues as well. It's generally a good idea to utilize using statements on objects that implement IDisposable. A quick and easy way to determine if they do is to initialize it, then see if there's a .Dispose method:
var conn = new SqlConnection();
conn.Dispose();
If conn.Dispose() doesn't have a compiler error, or Intellisense shows it as a valid method, then the object is disposable and you should use a using statement:
using(var conn = new SqlConnection())
{
// ...
}
Disposing disposable objects helps to prevent memory leaks and free up resources you no longer need. Further, it lets you do stuff like this:
public IList getRents()
{
using (var con = new SqlConnection(sCon))
{
var query = "SELECT r.RentID,c.CustomerName,v.VehicleName,r.[Hours], r.[Hours] * v.Rent AS Total FROM Rent as r INNER JOIN Customer AS c ON r.CustomerID = c.CustomerID INNER JOIN Vehicle AS v ON r.VehicleID = v.VehicleID";
using (var com = new SqlCommand(query, con))
{
con.Open();
using (var reader = com.ExecuteReader())
{
IList result = new List<Rent>();
while (reader.Read())
{
Rent r = new Rent();
r.rentID = (int)reader[0];
r.customerID = (int)reader[1];
r.vehicleID = (int)reader[2];
r.hours = (int)reader[3];
//r.total = (int)reader[4];
result.Add(r);
}
return result;
}
}
}
}
I refactored your method a bit to be easier to read and debug. Note that return is being called right after the SqlDataReader completes instead of at the very end of the method. This enables you to keep variables closer to where they're being used, in this case result. This makes it easier to find potential problems and correct exceptions that do come up.
Finally, since getRents really only does mapping from SQL to C# objects, I think a micro-ORM like Dapper will help you greatly. This is more of a personal decision but if you're app is doing a lot of CRUD work, then an micro-ORM like Dapper would cut down considerably on the time it takes to write as well as lower potential problems.
Consider this refactored version using Dapper:
public IList GetRents()
{
using(var conn = new SqlConnection(sCon))
{
var query = "SELECT r.RentID,c.CustomerName,v.VehicleName,r.[Hours], r.[Hours] * v.Rent AS Total FROM Rent as r INNER JOIN Customer AS c ON r.CustomerID = c.CustomerID INNER JOIN Vehicle AS v ON r.VehicleID = v.VehicleID";
return conn.Query<Rent>(sql).ToList();
}
}
As you can see, it's a lot shorter, simpler, and straight to the point. Just make sure your class properties match your SQL column names. Stack Overflow uses Dapper; in fact, they developed it. Read more about it here.
You are casting everything to int. Clarify if you want the name or the ID in your code. If you want the ID then change your query to read
String query = "SELECT r.RentID,c.CustomerID,v.VehicleID,r.[Hours], r.[Hours] * v.Rent AS Total FROM Rent as r INNER JOIN Customer AS c ON r.CustomerID = c.CustomerID INNER JOIN Vehicle AS v ON r.VehicleID = v.VehicleID";
If it was the names you wanted then change the respective fields to cast to a string. Also check the data type produced by multiplying r.Hours * v.Rent as this could be rounding your results inadvertently for the line you commented out.
You never specified the exception you are getting. It's possible it fails because your are casting strings (the 2 names) to an integer. As it is, your code swallows any exception and doesn't report it or log it so finding the exact source of the problem won't be to easy.
Consider this in your exception handler
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
then check your Output window for what error you got. And place it here so we can better help.
Try it.
In your code you had a "*" the most
public IList getRents()
{
IList result = new List();
try
{
SqlConnection con = new SqlConnection(sCon);
String query = "SELECT r.RentID,c.CustomerName,v.VehicleName,r.[Hours], r.[Hours] , v.Rent AS Total FROM Rent as r INNER JOIN Customer AS c ON r.CustomerID = c.CustomerID INNER JOIN Vehicle AS v ON r.VehicleID = v.VehicleID";
//String query = "SELECT * FROM Rent";
SqlCommand com = new SqlCommand(query, con);
con.Open();
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Rent r = new Rent();
r.rentID = (int)reader[0];
r.customerID = (int)reader[1];
r.vehicleID = (int)reader[2];
r.hours = (int)reader[3];
//r.total = (int)reader[4];
result.Add(r);
}
con.Close();
}
catch (Exception ex)
{
ex.ToString();
}
return result;
}
Related
I have parameterized stored procedure (tested and works) in mysql.
I wanted to replace my long select statement with stored procedure in my code.
With select statement it worked but now I am not able to run it with sp (commented string req).
My stored procedure:
CREATE DEFINER=`root`#`%` PROCEDURE `GetProjectVM`(
IN projectName NVARCHAR(255)
)
BEGIN
select VirtualMachines.Name,
VirtualMachines.IpAddress,
VirtualMachines.DiskSize,
VirtualMachines.CPU,
VirtualMachines.Ram,
VirtualMachines.ImageUrl,
VirtualMachines.Role,
Hypervisors.Name as Hypervisor,
Managements.Netmask,
Managements.Gateway from VirtualMachines inner join Projects
on Projects.id = VirtualMachines.ProjectId inner join Hypervisors
on Hypervisors.HypervisorId = VirtualMachines.HypervisorId inner join Managements
on VirtualMachines.ManagementId = Managements.Id
where Projects.Name = projectName;
END
My code looks like:
private string _dbUrl;
private MySqlConnection _cnn;
public Dbmanagement(string dbUrl)
{
this._dbUrl = dbUrl;
this._cnn = new MySqlConnection(this._dbUrl);
}
private MySqlDataReader ExecuteMysqlCommand(string cmd)
{
_cnn.Open();
var result = new MySqlCommand(cmd, _cnn);
return result.ExecuteReader();
}
public IEnumerable<VM> GetProjectVM(string projectName)
{
var vmList = new List<VM>();
//string req =
// $"select VirtualMachines.Name,VirtualMachines.IpAddress,VirtualMachines.DiskSize,VirtualMachines.CPU,VirtualMachines.Ram,VirtualMachines.ImageUrl,VirtualMachines.Role,Hypervisors.Name as Hypervisor,Managements.Netmask,Managements.Gateway from VirtualMachines inner join Projects on Projects.id = VirtualMachines.ProjectId inner join Hypervisors on Hypervisors.HypervisorId = VirtualMachines.HypervisorId inner join Managements on VirtualMachines.ManagementId = Managements.Id where Projects.Name = \"{projectName}\" ;";
MySqlCommand cmd = new MySqlCommand("GetProjectVM", _cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#projectName", projectName);
var req = cmd;
var rd = ExecuteMysqlCommand(req.ToString());
while (rd.Read())
{
vmList.Add(new VM(rd));
}
_cnn.Close();
return vmList;
}
The problem - or at least the first problem - is how you're executing the command.
Calling ToString on a MySqlCommand is very unlikely to be able to convey all the relevant information. You're losing parameterization, command type etc.
Change this:
var req = cmd;
var rd = ExecuteMysqlCommand(req.ToString());
to
_cnn.Open();
var rd = req.ExecuteReader();
I'd also suggest not having a single MySqlConnection, but just constructing one when you need to, and letting the connection pool manage making that efficient. So something like:
using (var connection = new MySqlConnection(_dbUrl))
{
connection.Open();
using (var cmd = new MySqlCommand("GetProjectVM", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
// Avoid AddWithValue where possible, to avoid conversion issues.
cmd.Parameters.Add("#projectName", MySqlDbType. VarChar).Value = projectName;
using (var reader = cmd.ExecuteReader())
{
var list = new List<VM>();
while (reader.Read())
{
list.Add(new VM(reader));
}
return list;
}
}
}
The idea under REST is, if an http request may come for an unknown record, we return 404, if it exists then roles of the employee.
The naive way would be that I can do this in two SQL statements, check the result of the first return null if not found else proceed with retrieving roles. The caller can check if result of the function is null and can return 404 based on that otherwise it will dislay roles of the user.
"SELECT Id FROM Employee WHERE Id = #Id"
"SELECT * FROM Role WHERE EmployeeId = #Id"
My current implementation is:
public List<object> GetUserRolesById(int id)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// statement 1
string sql = "SELECT Id FROM Employee WHERE Id = #Id";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.Add("#Id", SqlDbType.Int, 32).Value = id;
using (SqlDataReader reader = command.ExecuteReader())
{
if (!reader.Read() || reader.IsDBNull(reader.GetOrdinal("Id")))
{
return null; // caller to return 404 if record not found
}
}
}
// statement 2
sql = #"SELECT Id, Name FROM Role WHERE EmployeeId = #Id";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.Add("#Id", SqlDbType.Int, 32).Value = id;
using (SqlDataReader reader = command.ExecuteReader())
{
List<object> roles = new List<object>();
if (reader.Read())
{
for (int i = 0; i < roleIds.Length; i++)
{
roles.Add(new {Id = Int32.Parse(reader.GetString((0)), Name = reader.GetString(1)});
}
}
return roles;
}
}
}
}
Question:
How can I combine both SQL statements in one in a nicer way?
Edit
Following the answer, incorporating suggestions in my solution, minus the user non-existent condition.
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = #"
SELECT Employee.Id, Role.Id AS [RoleId], Role.NAME AS [RoleName]
FROM Employee
LEFT OUTER JOIN EmployeeRole on Employee.Id = EmployeeRole.EmployeeId
LEFT OUTER JOIN Role on EmployeeRole.RoleId = Role.Id
WHERE Employee.Id = #Id";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.Add("#Id", SqlDbType.Int).Value = id;
using (SqlDataReader reader = command.ExecuteReader())
{
List<object> roles = new List<object>();
while (reader.Read()) // 404 condition missing?
{
roles.Add(new {Id = reader.GetInt32(1), Name = reader.GetString(2)});
}
return roles;
}
}
}
Query 2
Will it work if we combine both queries? however, I don't know how to retrieve double query result from the reader.
string sql = #"SELECT FIRST FROM Employee WHERE Id = #Id;
SELECT Employee.Id, Employee.First, Role.Id AS [RoleId], Role.NAME AS [RoleName]
FROM Employee
LEFT OUTER JOIN EmployeeRole on Employee.Id = EmployeeRole.EmployeeId
LEFT OUTER JOIN Role on EmployeeRole.RoleId = Role.Id
WHERE Employee.Id = #Id2";
I'd suggest using SQL like:
SELECT Employee.Id, Role.WhateverColumnYouWantHere
FROM Employee LEFT OUTER JOIN Role On Employee.Id = Role.EmployeeID
WHERE Employee.Id = #Id
If the employee isn't there then Read will return false. If the employee is there, but lacks a role, then Role.WhateverColumnYouWantHere will be NULL (IsDBNull will return true).
Additionally, you likely want to remove your for (int i = 0; i < roleIds.Length; i++) loop (leave the logic inside it - just remove the loop) since it isn't doing anything useful. Also, change if (reader.Read()) to while (reader.Read()) to handle the possibility of multiple roles. Plus, you likely should use reader.GetInt32(0) rather than Int32.Parse(reader.GetString((0)) - assuming that the Id is a 32-bit integer (rather than a string). Also, remove the , 32 code - it is unnecessary, since SqlDbType.Int has a fixed size (i.e. it knows it is 32-bits).
I have a C# application that displays products that a company sells. Next to that column is the product type and in the last column is the number of customers that have bought that product. This is my SQL query to populate the first two columns:
string selectQuery;
selectQuery = "SELECT Products.ProductID, Products.ProductTypeID, ";
selectQuery = selectQuery + "Products.ProductName, Products.YearlyPremium, ProductTypes.ProductType ";
selectQuery = selectQuery + "FROM Products ";
selectQuery = selectQuery + "INNER JOIN ProductTypes ON Products.ProductTypeID = ProductTypes.ProductTypeID "
Now I need to work out how many customers bought each product. I guess I need to use the COUNT(*) method to get CustomerID's but now sure how to integrate it into this query.
Here is the schema
The code I use to display the data in the listView is this:
SqlConnection conn = ConnectionsManager.DatabaseConnection();
SqlDataReader rdr = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(selectQuery, conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Products product = new Products(int.Parse(rdr["ProductID"].ToString()),
int.Parse(rdr["ProductTypeID"].ToString()),
rdr["ProductName"].ToString(),
Math.Round(decimal.Parse(rdr["YearlyPremium"].ToString()), 2));
ProductTypes productType = new ProductTypes(int.Parse(rdr["ProductTypeID"].ToString()),
rdr["ProductType"].ToString());
ListViewItem lvi = new ListViewItem(product.ProductName.ToString());
lvi.SubItems.Add(productType.ProductType.ToString());
\\lvi.SubItems.Add(customer.CustomerID.ToString()); <---this will be the line to display the customer count
lvMain.Items.Add(lvi);
}
if (rdr != null)
rdr.Close();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Unsuccessful" + ex);
}
Considering that ProductTypeId is has to be a NON NULL value in final result (hence using INNER JOIN), the SQL part which you will have to embed in your C# code:
SELECT P.ProductID, P.ProductTypeID, P.ProductName, P.YearlyPremium, PT.ProductType, CustomerCount = COUNT(S.CustomerID)
FROM Sales S
INNER JOIN Products P
ON S.ProductID = P.ProductID
INNER JOIN ProductTypes PT
ON P.ProductTypeID = PT.ProductTypeID
GROUP BY P.ProductID, P.ProductTypeID, P.ProductName, P.YearlyPremium, PT.ProductType
Note:- Since you do not need any attribute of the customer, and you have a foreign key on Sales.CustomerID column to Customer.CustomerId column, hence for the purpose of getting the CustomerCount, join to Customer table is not necessary.
You would seem to want a basic GROUP BY query:
SELECT p.ProductID, p.ProductTypeID, p.ProductName, p.YearlyPremium,
pt.ProductType,
COUNT(DISTINCT s.CustomerID) as num_customers
FROM Products p INNER JOIN
ProductTypes pt
ON p.ProductTypeID = PT.ProductTypeID LEFT JOIN
Sales S
ON s.ProductID = p.ProductID
GROUP BY p.ProductID, p.ProductTypeID, p.ProductName, p.YearlyPremium, pt.ProductType;
Notes:
This uses a LEFT JOIN to Sales to get products that have no sales.
This uses an INNER JOIN to ProductTypes because, presumably, all products have a type.
The GROUP BY has all non-aggregated columns.
This counts unique customer ids, because the same customer may have purchased the same product on more than one occasion. If you just want a count of the records in Sales, remove the DISTINCT.
try using Group by clause. It will work.
Example:
SELECT COUNT(ProductId),CustomerId
FROM Product
GROUP BY CustomerId;
kind of this
This is my first foray into LINQ.
I still have to wrap my head around the results part, but I can't seem to get any results from this.
var institutions = from lots in lotsdb.NEWinstitution
join webs in webbitdb.tblinstitution
on lots.institutionid equals webs.dispenseinstid into newinsts
from webs2 in newinsts.DefaultIfEmpty()
where webs2 == null
select new
{
instid = lots.institutionid,
instname = lots.institutionname
};
foreach(var instfound in institutions)
{
MessageBox.Show(instfound.instid.ToString() + " " + instfound.instname.ToString());
}
I'm using Datasets created by Visual Studio in the DATASources list.
Below is my original SQL string that i have "tried" to adapt to LINQ
string strgetloc = #"
SELECT NEWinstitution.institutionid, NEWinstitution.institutionname
FROM NEWinstitution
LEFT JOIN tblinstitution
ON NEWinstitution.institutionid = tblinstitution.dispenseinstid
WHERE (((tblinstitution.institutionid) Is Null));"
You probably need something like this:
var institutions =
from lots in lotsdb.NEWinstitution
join webs in webbitdb.tblinstitution on lots.institutionid equals webs.dispenseinstid
where webs.IsInstitutionIdNull()
select new
{
instid = lots.institutionid,
instname = lots.institutionname
};
The IsInstitutionIdNull() method is generated by the MSDataSetGenerator when the columns allows DBNull. Because you cannot compare it directly to DBNull or to null.
(fixed a typo)
So I ended up using the following code:
var idsNotInB = dtLotsInst.AsEnumerable().Select(r => r.Field<int>("institutionid"))
.Except(dtWebbitInst.AsEnumerable().Select(r => r.Field<int>("institutionid")));
count = idsNotInB.Count();
if (count != 0)
{
DataTable dtOnlyLots = (from row in dtLotsInst.AsEnumerable()
join id in idsNotInB
on row.Field<int>("institutionid") equals id
select row).CopyToDataTable();
using (OleDbConnection con = new OleDbConnection(PackChecker.Properties.Settings.Default["WebbitConnectionString"].ToString()))
{
string strgetloc = #"INSERT INTO tblinstitution ( dispenseinstid, institutionname ) VALUES (?,?)";
using (OleDbCommand cmd = new OleDbCommand(strgetloc, con))
{
con.Open();
foreach (DataRow dr in dtOnlyLots.Rows)
{
cmd.Parameters.Add("?", OleDbType.Integer).Value = Convert.ToInt32(dr["institutionid"]);
cmd.Parameters.Add("?", OleDbType.VarWChar).Value = dr["institutionname"].ToString();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
con.Close();
}
}
}
This uses LINQ and works well by using "EXCEPT" in the first LINQ section to find values not in one table. Then it uses this list to generate the rows from the table I want.
I have 21 tables in Oracle 11g, and I need to display them using JOIN clauses. But the query is too slow and throws an System.OutOfMemoryException
My current code:
try
{
//string ConString;
using (OracleConnection con = new OracleConnection(ConString))
{
//string query = "SELECT PAX.CREATION_DATE,PAX.PNR,PAX.PAX_NMBR,PAX.UPDATE_LEVEL_MOD,PAX.PAX_NAME,PAX.CHANGE_OR_CANCEL_IND,PAX_SEATS.LEG_NMBR,PAX_SEATS.INSTANCE_NMBR,pax_seats.ssr_cd,pax_seats.carrier_cd,pax_seats.seat_nmbr,pax_seats.previous_status_codes FROM PAX INNER JOIN PAX_SEATS ON PAX.PNR = PAX_SEATS.PNR";
string query1 = "SELECT PNR FROM HISTORY_LEGS";
string query2 = "SELECT PAX.CREATION_DATE,PAX.PNR,PAX.PAX_NMBR,PAX.UPDATE_LEVEL_MOD,PAX.PAX_NAME,PAX.CHANGE_OR_CANCEL_IND,PAX_SEATS.LEG_NMBR,PAX_SEATS.INSTANCE_NMBR,pax_seats.ssr_cd,pax_seats.carrier_cd,pax_seats.seat_nmbr,pax_seats.previous_status_codes,PAX_SSRS.SSR_NMBR,PAX_TKT.TKTNMBR,PAX_TKT.ISSUING_AIRLINE, PAX_TKT.ISSUING_OFFC_NMBR, PAX_TKT.ISSUING_COUNTER_CD, PAX_TKT.ISSUING_AGNT_NMBR, PAX_TKT.TKT_IND, PAX_TKT.TKT_STATUS, PAX_TKT.TARIFICATION_IND,PAX_TKT.S_IND,PAX_TKT.ISSUANCE_DATE FROM PAX INNER JOIN PAX_SEATS ON PAX.PNR = PAX_SEATS.PNR RIGHT JOIN PAX_SSRS ON PAX.PNR = PAX_SSRS.PNR RIGHT JOIN PAX_TKT ON PAX.PNR = PAX_TKT.PNR";
//string query4 = "SELECT * FROM PAX";
//string query3 = "SELECT PAX.PAX_NMBR,PAX.PAX_NAME,RES_LEGS.ARNK_IND,RES_LEGS.CARRIER_CD1,RES_LEGS.CARRIER_CD2,RES_LEGS.FLT_NMBR,RES_LEGS.CLASS_CD,RES_LEGS.DAY_OF_WEEK,RES_LEGS.FLT_DATE,RES_LEGS.LEG_ORIGIN_CD,RES_LEGS.LEG_DES_CD,RES_LEGS.CURRENT_STATUS_CD,RES_LEGS.NUMBER_IN_PARTY,RES_LEGS.DP_TIME,RES_LEGS.AR_TIME,RES_LEGS.DATE_CHANGE_IND,RES_LEGS.FLT_IRREGULARITY_IND,RES_LEGS.LEG_TXT,RES_LEGS.PREVIOUS_STATUS_CODES,RES_LEGS.MESSAGE FROM RES_LEGS INNER JOIN PAX ON RES_LEGS.PNR = PAX.PNR";
OracleCommand cmd = new OracleCommand(query2, con);
OracleDataAdapter oda = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
//dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;
//dataGridView1.RowHeadersVisible = false;
if (ds.Tables.Count > 0)
{
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Questions:
How to get all data?
How to prevent System.OutofMemory?
How to speed up query execution performance?
You can look at indexes on your key fields to speed up the query a bit perhaps, but if you are getting a lot of rows back you'll be hitting some memory caps somewhere.
If you can, I would suggest implementing some paging: Paging with Oracle.
This will let you bring back smaller chunks of your data making it quicker and less likely to hit any memory limits.