Code below is taking hours of time to execute. I'm comparing each string from database to calculate N00,N01,N10,N11 parameters. Temp1 is List of type string it consists of more than 5000 words
foreach (string ri in temp1)
{
for (int a3 = 0; a3 < ssl.Count; a3++)
{
//for (int tn = 0; tn < tempNam.Count ; tn++)
//{
try
{
SqlCommand cmd5 = new SqlCommand("select count(*) from sample s inner join sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + "=0 and s.KeyWord='y' and s. " + ssl[a3].ToString()+ "=0", con);
int im = (int)cmd5.ExecuteScalar();
if (im == 1)
{
gh += 1;
}
SqlCommand cmd6 = new SqlCommand("select count(*) from sample s inner join sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + "=0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + ">0", con);
int im1 = (int)cmd6.ExecuteScalar();
if (im1 == 1)
{
gh2 += 1;
}
SqlCommand cmd7 = new SqlCommand("select count(*) from sample s inner join sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + ">0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + "=0", con);
int im2 = (int)cmd7.ExecuteScalar();
if (im2 == 1)
{
gh3 += 1;
}
SqlCommand cmd8 = new SqlCommand("select count(*) from sample s inner join sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + ">0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + ">0", con);
int im3 = (int)cmd8.ExecuteScalar();
if (im3 == 1)
{
gh4 += 1;
}
if (a3 == (ssl.Count-1))
{
SqlCommand ins = new SqlCommand("update sample set N00=" + gh + " where KeyWord='" + ri + "'", con);
ins.ExecuteNonQuery();
gh = 0;
SqlCommand ins1 = new SqlCommand("update sample set N01=" + gh2 + " where KeyWord='" + ri + "'", con);
ins1.ExecuteNonQuery();
gh2 = 0;
SqlCommand ins2 = new SqlCommand("update sample set N10=" + gh3 + " where KeyWord='" + ri + "'", con);
ins2.ExecuteNonQuery();
gh3 = 0;
SqlCommand ins4 = new SqlCommand("update sample set N11=" + gh4 + " where KeyWord='" + ri + "'", con);
ins4.ExecuteNonQuery();
gh4 = 0;
}
}
catch (Exception ex)
{
}
// }
}
}
SqlCommand cmd1s = new SqlCommand("select KeyWord from sample", con);
SqlDataAdapter da = new SqlDataAdapter(cmd1s);
DataSet ds = new DataSet();
da.Fill(ds, "sample");
foreach (DataRow dr in ds.Tables[0].Rows)
{
string dd = dr["KeyWord"].ToString();
if (dd != "y")
{
if (!li.Contains(dd))
{
li.Add(dd);
}
}
}
Put an index on Sample.KeyWord. This would make this a hell of a lot faster.
As with Anthony's comment below, there's a lot wrong with this code. The index is what I'm guessing is taking the most time, but you should read these topics as well:
Evil Practices - Swallowing Exceptions
The SqlParameter Class
The using() construct - This applies to any object implementing the IDisposable interface.
.NET Naming Conventions and Programming Standards - Best Practice - It is crucial to have meaningful names at the very least.
Look here for creating indices: http://msdn.microsoft.com/en-us/library/ms188783.aspx
Sorry, but your code is very hard to understand with the variables named the way they are.
I refactored your query for readability, moved to reusing a single SqlCommand (two in total if you count the one outside of the loop) and removed the extra queries.
I'm guessing the poor performance is tied to lack of object reuse, replication of effort with your queries, and you're probably throwing exceptions left and right and the try/catch is causing the stack to unwind every time you throw an exception! Check the output pane for debug information or set a breakpoint on the line System.Diagnostics.Debug.Print(ex.ToString());. Unwinding the stack will kill execution time.
I'm not sure if you're using poor variable names, or if you obfuscated your code, but you should really thing about putting meaningful variable names in code you need help with.
I'm positive that there is alot more you can do to optimize your code, but I'd take care of reusing the SqlCommand, removing repeated queries, and correctly handling your exception handling first.
foreach (string ri in temp1)
{
for (int index = 0; index < ssl.Count; index++)
{
try
{
SqlCommand command;
string query;
query = string.Format("select count(*) from sample s inner join sample ss on ss.KeyWord='{0}' and ss.{1}=0 and s.KeyWord='y' and s.{1}=0", ri, ssl[index].ToString());
command = new SqlCommand(query, con);
if ((int)command.ExecuteScalar() == 1)
{
gh++;
gh3++;
}
query = string.Format("select count(*) from sample s inner join sample ss on ss.KeyWord='{0}' and ss.{1}=0 and s.KeyWord='y' and s.{1}>0", ri, ssl[index].ToString());
command = new SqlCommand(query, con);
if ((int)command.ExecuteScalar() == 1)
{
gh2++;
gh4++;
}
if (index == (ssl.Count-1))
{
query = string.Format("update sample set N00={0} where KeyWord='{1}'", gh.ToString(), ri);
command = new SqlCommand(query, con);
command.ExecuteNonQuery();
gh = 0;
query = string.Format("update sample set N01={0} where KeyWord='{1}'", gh2.ToString(), ri);
command = new SqlCommand(query, con);
command.ExecuteNonQuery();
gh2 = 0;
query = string.Format("update sample set N10={0} where KeyWord='{1}'", gh3.ToString(), ri);
command = new SqlCommand(query, con);
command.ExecuteNonQuery();
gh3 = 0;
query = string.Format("update sample set N11={0} where KeyWord='{1}'", gh4.ToString(), ri);
command = new SqlCommand(query, con);
command.ExecuteNonQuery();
gh4 = 0;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print(ex.ToString());
}
}
}
SqlCommand command = new SqlCommand("select KeyWord from sample", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds, "sample");
foreach (DataRow dr in ds.Tables[0].Rows)
{
string KeywordCell = dr["KeyWord"].ToString();
if (KeywordCell != "y")
{
if (!li.Contains(KeywordCell))
{
li.Add(KeywordCell);
}
}
}
One minor thing to try once you've dealt with the larger issues pointed out by the others...
You're calculating ssl[a3].ToString() 8 times for each time through the inner loop. That's 40,000 times for each value of a3. You can pre-calculate the string values of each item in a3 and store them in a List before the outer loop. Then, instead of iterating over the index off ssl in the inner loop, use a foreach on the values of your pre-calculated list of strings.
If you do this, you will need to move the code in the if (a3 == (ssl.Count-1)) out of the inner loop to the end of the outer loop.
Related
I am using C# to create a windows form.
I am trying to set a condition statement for a particular value that is retrieved from my database by the onclick of a button. The datatype of the column is 'integer'.
Below is my code:
string checkquantity = "SELECT `inventory_item`.`Item_Quantity_Available`FROM `inventory_item` , `patient`, `out_treatment`WHERE `inventory_item`.`Item_ID` = `out_treatment`.`Inventory_ID`AND `patient`.`Patient_ID` = `out_treatment`.`Patient_ID`AND `out_treatment`.`Patient_ID`= '" + pid + "' ";
MySqlCommand selectout = new MySqlCommand(checkquantity, connect);
MySqlDataAdapter selectdata = new MySqlDataAdapter(checkquantity, connect);
DataTable selecttable = new DataTable();
selectdata.Fill(selecttable);
DataSet ds = new DataSet();
selectdata.Fill(selecttable);
selectdata.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if ( i <= 0)
{
MessageBox.Show("Out of Stock");
}
I'm new with c#.
I don't think the int i = ds.Tables[0].Rows.Count; is the right way.
Any help is much appreciated.
First of all, like #Flydog57 said, you should not concatenate your sql query. The best way is to use parameters, for example:
string checkquantity = "SELECT i.Item_Quantity_Available " +
" FROM inventory_item i JOIN out_treatment t ON i.Item_Id = t.Inventory_ID " +
" JOIN patient p ON t.Patient_ID = p.PatiendID " +
" WHERE t.Patient_ID = #Patiend_ID";
MySqlCommand selectout = new MySqlCommand(checkquantity, connect);
// set the parameter value
selectout.Parameters.AddWithValue("#Patiend_ID", patient_id_value);
MySqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
if ((int)rdr["Item_Quantity_Available"] == 0)
MessageBox.Show("Out of Stock");
}
In second place, you could use a MySqlDataReader to verify that Item_Quantity_Available is equal to 0, like in the previous example. Otherwise, If you just wants to verify if there is data, the condition could be something like that:
if (!rdr.Read())
{
MessageBox.Show("Out of Stock");
}
The third improvemente is to join tables with the join clause.
I 'm dealing with a query which results in above mentioned error when called with no or limited search string.
The table is 30k records big and when the query is fired with no search string I would expect the database to return all records and have Visual Studio put them in a table (ASP.net), however, if the query is fired like above the StackOverflow error is returned :(
If I fire the query with a more specific search string the script seems to work normal and results are being returned as expected.
I have been googling and all seems to point to an infinite loop or recursion but returning records make me believe it might be another problem.
The query:
SqlConnection conn = new SqlConnection(_cstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
SqlDataReader dr;
if (mode == "zoek_met_zoekterm")
{
cmd.CommandText = "select W1.id, W1.soort_brief, W1.omschrijving, W1.referentie, W1.url, W1.factuur, W3.naam, W3.klantnummer, W4.omschrijving AS w4_omschr, W4.referentie AS w4_ref, substring(W1.referentie,6,2) as w1_jaar, W1.parent AS w1_parent from brief W1 left join brief W4 on W1.parent = W4.id join klant W3 on W3.klantnummer = W1.klantnummer where W1.referentie in (select referentie from brief where substring(referentie,6,2) = #jaar) and (W3.klantnummer like #zoekterm or W1.referentie like #zoekterm or W1.omschrijving like #zoekterm or W3.naam like #zoekterm or W4.omschrijving like #zoekterm) order by W1.id";
cmd.Parameters.Clear();
cmd.Parameters.Add("#zoekterm", SqlDbType.VarChar).Value = "%" + (string)parameters["zoekterm"] + "%";
string jaar = parameters["jaar"].ToString();
jaar = jaar.Substring(2, 2);
cmd.Parameters.Add("#jaar", SqlDbType.VarChar).Value = jaar;//(Int32)parameters["jaar"];
base.LogQuery(cmd);
dr = cmd.ExecuteReader();
while (dr.Read())
{
CBrief brief = new CBrief();
brief.Id = dr.GetInt32(0);
brief.SoortBrief = (Enums.SoortBrief)dr.GetInt32(1);
brief.Omschrijving = Functies.Decrypt(dr.GetString(2), EncryptData);
brief.Referentie = Functies.Decrypt(dr.GetString(3), EncryptData);
brief.Url = Functies.Decrypt(dr.GetString(4), EncryptData);
brief.Factuur = (Enums.SoortDocument)dr.GetInt32(5);
brief.Klantnummer = dr.GetInt32(7);
if (dr.GetString(2).Length == 0)
{
if (!dr.IsDBNull(9) && !dr.IsDBNull(8))
brief.Omschrijving = dr.GetString(9) + " " + dr.GetString(8);
}
ret_value.Add(brief);
}
dr.Close();
dr.Dispose();
}
And the function that calls the query:
public List<CBrief> GetBriefOverzicht(string zoekterm, int jaar)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("zoekterm", zoekterm);
parameters.Add("jaar", jaar);
if (zoekterm.Length == 0)
{
return _dal.brief_search(parameters, "geen_invoer");
}
return _dal.brief_search(parameters, "zoek_met_zoekterm");
}
And last but not least the function that builds the table to display in the web application:
protected void Bzoeken_Click(object sender, EventArgs e)
{
string zoekterm = TBzoekterm.Text;
DateTime nu = DateTime.Now;
int jaar = nu.Year;
List<CBrief> brieven = br_rbs.GetBriefOverzichtWijzigen(zoekterm);
StringBuilder overzicht = new StringBuilder("<table class=\"tabel-beheer-1\">");
foreach (CBrief b in brieven)
{
List<CBijlage> bijlagen = new List<CBijlage>();
CKlant k = sec.GetKlant(b.Klantnummer);
string klantnaam = k.Naam;
string richting = "";
if (b.OmschrijvingAanwezig == true)
{
bijlagen = br_rbs.GetBijlagenBrieven(b.Id);
}
if (b.SoortBrief == Enums.SoortBrief.Inkomend) richting = "van: "; else richting = "naar: ";
// MG10082017 Ik heb hier twee icoontjes in elkaar geflanst om te testen. Deze moeten nog aangepast worden zodat ze in het geheeel passen.
overzicht.AppendLine("<tr><td><img src=\"/images/bg/document.jpg\" alt=\"open document\"></td><td><img src=\"/images/bg/detail.jpg\" alt=\"details\"></td><td><b>" + b.Referentie + "</b></td><td><i>" + b.SoortBrief.ToString() + " " + richting + "</i> " + klantnaam + "</td><td><b> " + b.Factuur.ToString() + ":</b> " + b.Omschrijving /*+"</td><td>"*/);
if (bijlagen.Count > 0)
{
foreach (CBijlage bijl in bijlagen)
{
overzicht.AppendLine("</br> " + bijl.Naam + "");
}
}
}
overzicht.AppendLine("</td></tr></table>");
Loverzicht.Text = overzicht.ToString();
}
The error usually points to: System.Web.dll or w3wp.exe (when not ran in debug mode). If my assumption is wrong and it seems to be an infinite loop or recursion, how am I going to find this?
The query works flawless in MS-SQL management console.
The Problem is not an infinite loop!
The Problem lies in the UI.
You can't display 30k units at once without a StackOverflow-Exception.
You should call pages of maybe 20 or 30 units at once with an pageIndex.
This way, your application will perform better and avoid such errors.
I am trying to get a sum with a column from a ms access database but so not nothing has worked this is what I have
Conn.Open();
Command.CommandText = "Select SUM(c_qty) from COLLVAULT_COINS WHERE c_type!='US Currency' AND c_type!='World Currency' AND c_type!='World Coins' AND c_listName = '" + activeCollection + "'";
int total_1 = Convert.ToInt32(Command.ExecuteScalar());
//total sum - get how many match the query
lblcollectedcount.Text = Convert.ToString(total_1);
Conn.Close();
any help would be nice.
Conn.Open();
Command.CommandText = "Select SUM(c_qty) from COLLVAULT_COINS WHERE c_type <>'US Currency' AND c_type<>'World Currency' AND c_type<>'World Coins' AND c_listName = '" + activeCollection + "'";
int total_1 = Convert.ToInt32(Command.ExecuteScalar());
//total records
lblcollectedcount.Text = Convert.ToString(total_1);
Conn.Close();
I had to use <> as not equals to instead of !=
You could try something like this, assuming you want the number of records that were matched with your query:
Conn.Open();
Command.CommandText = "Select SUM(c_qty) as sum, Count(*) as count from COLLVAULT_COINS WHERE c_type <> 'US Currency' AND c_type <> 'World Currency' AND c_type <> 'World Coins' AND c_listName = '" + activeCollection + "'"; //
int total_matches = 0;
using (MySqlDataReader dataReader = Command.ExecuteReader())
{
if (dataReader.Read())
{
lblcollectedcount.Text = Convert.ToString(dataReader["sum"]);
total_matches = Convert.ToInt32(dataReader["count"]);
}
else
{
//nothing was read, handle that case
}
}
Conn.Close();
EDIT:
Misunderstood what the OP was asking for, and I also didn't catch the '<>' error.
I'll still keep this answer here, just for reference code.
I am using the same button "Save" to update a table called AnalysisExperiments and also to insert data into a table called "Analysis". however the update is not working. Here is the code:
#region Insert Data into Analysis Table
if (checkIfRepeatedJobNumber(tbJobNumber.Text.Trim()))
{
MessageBox.Show("Job Number is already exist.", "Repeated Data");
return;
}
string query = "insert into Analysis (ID, WellName, EstimatedStartDate, SOWComments, ProgressComments, Field, FocalPoint, StudyCompleted, Company, ReservoirPressure, ReservoirTemp, SelectedSamples) " +
"values (#ID, #WellName, #SamplingDate, #SOWComments, #ProgressComments, #Field, #FocalPoint, #StudyCompleted, #Company, #ReservoirPressure, #ReservoirTemp, #SelectedSamples)";
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.Parameters.AddWithValue("#ID", tbJobNumber.Text);
cmd.Parameters.AddWithValue("#WellName", tbWellName.Text);
cmd.Parameters.AddWithValue("#SamplingDate", dtpSamplingDate.Text);
cmd.Parameters.AddWithValue("#SOWComments", tbSOW_Comments.Text);
cmd.Parameters.AddWithValue("#ProgressComments", tbProgressComments.Text);
cmd.Parameters.AddWithValue("#Field", tbFieldName.Text);
cmd.Parameters.AddWithValue("#FocalPoint", tbFocalName.Text);
if (radYes.Checked)
cmd.Parameters.AddWithValue("#StudyCompleted", "Yes");
else
cmd.Parameters.AddWithValue("#StudyCompleted", "No");
cmd.Parameters.AddWithValue("#Company", tbCompany.Text);
cmd.Parameters.AddWithValue("#ReservoirPressure", tbReservoirPressure.Text);
cmd.Parameters.AddWithValue("#ReservoirTemp", tbReservoirTemp.Text);
cmd.Parameters.AddWithValue("##SelectedSamples", tbSelectedSamples.Text);
try
{
conn.Open();
int j = cmd.ExecuteNonQuery();
if (j == 1)
{
MessageBox.Show("Done");
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
#endregion
#region update analysis Exp but still no working
#region Update database
try
{ int k = 0;
OleDbDataAdapter da;
da = new OleDbDataAdapter("select* from [AnalysisExperiments]", conn);
string ExpQuery = "update AnalysisExperiments set SampleNumber = #SampleNumber, Status = #Status where ID = '" + tbJobNumber.Text + "' and Experiment = '";
foreach (DataGridViewRow row in dgvExperiments.Rows)
{
ExpQuery += row.Cells["Experiment"].Value.ToString() + "'";
OleDbCommand updateCommand = new OleDbCommand(ExpQuery, conn);
updateCommand.Parameters.Add("#SampleNumber", OleDbType.VarWChar);
MessageBox.Show(row.Cells["SampleNumber"].Value.ToString() + " | " + row.Cells["Status"].Value.ToString() + " | " + row.Cells["Experiment"].Value.ToString());
updateCommand.Parameters["#SampleNumber"].Value = row.Cells["SampleNumber"].Value.ToString();
updateCommand.Parameters.Add("#Status", OleDbType.Boolean);
updateCommand.Parameters["#Status"].Value = row.Cells["Status"].Value;
da.UpdateCommand = updateCommand;
conn.Open();
k = da.UpdateCommand.ExecuteNonQuery();
conn.Close();
}
if (k == 1)
MessageBox.Show("Done");
else
{
MessageBox.Show("Nothing Updated!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
#endregion
#endregion
knowing that the compiler skips the loop in the update region.
At the second loop the query syntax becomes invalid cause the += that concatenates the previous text with the new one
Dim newQuery = ExQuery + row.Cells["Experiment"].Value.ToString() + "'";
And then use the new string for the ExecuteNonQuery.
Still this approach has many problems. You should separate this code in different methods and use parameters also for the last value
Have you tried to debug the foreach line to see if there are any rows in your dgvExperiments and if they are of type (or subtype of) DataGridViewRow ?
I have to search the employee details, which is contained within 3 tables. I have used joins in the query query, but it shows error when I press the search button:
sql command not properly ended
c# coding:
try {
//Search Employee Details
Oracle.DataAccess.Client.OracleConnection cn = new Oracle.DataAccess.Client.OracleConnection();
cn.ConnectionString = "user id=system; password=system;";
Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand();
cmd.Connection = cn;
//cn = new Oracle.DataAccess.Client.OracleConnection();
cmd.CommandText = " select deposit.loanid,
form1.empedoj,
form1.empshare,
sharecapital.shareint,
sharecapital.loandt,
sharecapital.loandeduc,
sharecapital.dividend,
sharecapital.sharetot
from form1,
deposit,
sharecapital
where deposit.loanid(+) = sharecapital.loanid = '" + txtlnid.Text.Trim() + "'"; // shows sql command not properly ended
Oracle.DataAccess.Client.OracleDataAdapter ada = new Oracle.DataAccess.Client.OracleDataAdapter(cmd);
System.Data.DataTable dt = new DataTable();
dt.Clear();
ada.Fill(dt);
//Display in Textbox
if (dt.Rows.Count > 0) {
txtlnid.Text = dt.Rows[0].ItemArray[0].ToString();
admdate.Text = dt.Rows[0].ItemArray[1].ToString();
txtadmamt.Text = dt.Rows[0].ItemArray[2].ToString();
txtadmint.Text = dt.Rows[0].ItemArray[3].ToString();
loandt.Text = dt.Rows[0].ItemArray[4].ToString();
txtlnamt.Text = dt.Rows[0].ItemArray[5].ToString();
txtlnint.Text = dt.Rows[0].ItemArray[6].ToString();
txtsctot.Text = dt.Rows[0].ItemArray[7].ToString();
}
if (cn.State == ConnectionState.Closed) {
cn.Open();
}
string str;
str = cmd.ExecuteScalar().ToString();
if (str != null) {
MessageBox.Show("Record Found");
} else {
MessageBox.Show("ID not Match");
}
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
Your SQL statement becomes
SELECT DEPOSIT.LOANID,
FORM1.EMPEDOJ,
FORM1.EMPSHARE,
SHARECAPITAL.SHAREINT,
SHARECAPITAL.LOANDT,
SHARECAPITAL.LOANDEDUC,
SHARECAPITAL.DIVIDEND,
SHARECAPITAL.SHARETOT
FROM FORM1, DEPOSIT, SHARECAPITAL
WHERE DEPOSIT.LOANID(+) = SHARECAPITAL.LOANID = '" + txtlnid.Text.Trim() + "'";
I suspect it should be:
SELECT DEPOSIT.LOANID,
FORM1.EMPEDOJ,
FORM1.EMPSHARE,
SHARECAPITAL.SHAREINT,
SHARECAPITAL.LOANDT,
SHARECAPITAL.LOANDEDUC,
SHARECAPITAL.DIVIDEND,
SHARECAPITAL.SHARETOT
FROM FORM1, DEPOSIT, SHARECAPITAL
WHERE DEPOSIT.LOANID(+) = SHARECAPITAL.LOANID
AND SHARECAPITAL.LOANID = '" + txtlnid.Text.Trim() + "'";
Also, you have a 3-table join without the correct join conditions, the query is highly likely to return a Cartesian product.
Have you tried putting a semicolon at the end of your query string?
cmd.CommandText = " select deposit.loanid, form1.empedoj, form1.empshare,
sharecapital.shareint, sharecapital.loandt, sharecapital.loandeduc,
sharecapital.dividend, sharecapital.sharetot from form1, deposit ,
sharecapital where deposit.loanid(+) = sharecapital.loanid = '" + txtlnid.Text.Trim() + "';";