SQL Performance Issue ASP.Net App - c#

We have a performance Issue in our ASP.NET based web App and it's visible only once when we login in the morning. As per Log looks like below query takes more than 1 minute 20 secs. Next time onward when user login to the app and try to access same page doesn't find any issue. Can you please let me know how to optimize this query ? Any thoughts how we can fix this problem ?
Log -
"11/13/15","08:38:27","ExecuteSql - ---3---- ","8",""
"11/13/15","08:38:27","ExecuteSql - ---4---- : SQL : SELECT TOP 1 CONVERT(varchar(15), Period_End_Date, 107) as PDate FROM PBHISTORY..STATEMENT_OF_CHANGE
ORDER BY Period_End_Date DESC","8",""
"11/13/15","08:39:48","ExecuteSql - ---5---- ","8",""
SQL :
SELECT TOP 1
CONVERT(varchar(15), Period_End_Date, 107) as PDate
FROM
PBHISTORY..STATEMENT_OF_CHANGE
ORDER BY
Period_End_Date DESC
C# ASP.NET -
public string GetDateRangeReportingDate(int reportId)
{
var report = GetReportInfoById(reportId);
string sql = string.Format(#"SELECT TOP 1 CONVERT(varchar(15), Period_End_Date, 107) as PDate FROM {0}..{1} ORDER BY Period_End_Date DESC", _historyDatabase, report.SourceTableName);
var data = ExecuteSql(sql);
while (data.Read())
{
return data["PDate"].ToString();
}
return null;
}
private SqlDataReader ExecuteSql(string sql)
{
SqlDataReader reader;
SqlConnection conn;
var commandTimeOut = ConfigurationManager.AppSettings["PBReportCommandTimeout"].ToString();
string connString = ConfigurationManager.ConnectionStrings["PBReportCS"].ConnectionString;
conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandTimeout = int.Parse(commandTimeOut);
reader = cmd.ExecuteReader();
return reader;
}

I found solution of my performance issue I mentioned above. Issue was wrong indexing in table and I fixed this issue by changing the indexing of table where from we are fetching records in production.
Also, I fixed Framework level entity class by using SQL Data Adapter and using statements. App runs super fast in production. Thanks for your help.
private string ExecuteSqlNew(string sql)
{
string connectionString = ConfigurationManager.ConnectionStrings["PBReportCS"].ConnectionString;
string commandTimeOut = ConfigurationManager.AppSettings["PBReportCommandTimeout"].ToString();
DataSet result = new DataSet();
string pDate = "";
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
cmd.CommandTimeout = int.Parse(commandTimeOut);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(result);
adapter.Dispose();
conn.Close();
pDate = result.Tables[0].Rows[0]["PDate"].ToString();
}
}
}
catch(Exception ex)
{
throw ex;
}
return pDate;
}

Related

Fetching Data from MySQL and Inserting it in MS SQL SERVER in visual studio c#

Am trying to read a table from a Mysql database and store all of it in a ms sql server in c# am reading from mysql correctly my problem is how to store the data i read in ms sql in the second part of code
string constring= ConfigurationManager.ConnectionStrings["cnxMysql"].ConnectionString;
MySqlConnection conn = new MySqlConnection(constring);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM i_evt WHERE Updt=0",conn);
conn.Open();
cmd.ExecuteNonQuery();
string constring2 = ConfigurationManager.ConnectionStrings["cnxsql"].ConnectionString;
SqlConnection conn2 = new SqlConnection(constring2);
SqlCommand cmd2 = new SqlCommand("INSERT INTO i_evt",conn2);
conn2.Open();
cmd2.ExecuteNonQuery();
conn2.Close();
conn.Close();
I have a reverse issue, I have to transfer data from MS SQL Server to MySQL.
I have used below snippet for this, but I personally don't recommend this for production as it adds data one by one row. But you can use it for a small dataset or local environment. You can add a try-catch as well as customize as per your need. This is for just reference.
public void Main()
{
var table = GetDataTableFromSQLServer();
AddToMySQL(table);
}
private DataTable GetDataTableFromSQLServer()
{
var connection = GetSqlServerConnection();
string query = "SELECT Column1, Column2 FROM TableName WHERE Column3 is NOT NULL";
var command = new SqlCommand(query, connection);
connection.Open();
var reader = command.ExecuteReader();
DataTable table = new DataTable();
table.Load(reader);
connection.Close();
return table;
}
private void AddToMySQL(DataTable table)
{
var connection = GetMySQLConnection();
connection.Open();
string query = "INSERT INTO TableName (column1, column2) VALUES(#column1, #column2);";
int i = 0;
foreach (DataRow row in table.Rows)
{
if (i % 1000 == 0)
{
// Closing & Reopening connection after 1000 records
connection.Close();
connection.Open();
}
Console.WriteLine($"Adding ({++i}/{table.Rows.Count})");
MySqlCommand command = new MySqlCommand(query, connection);
command.Parameters.Add(new MySqlParameter("#column1", MySqlDbType.Int64) { Value = row.Field<long>("column1").Trim() });
command.Parameters.Add(new MySqlParameter("#column2", MySqlDbType.VarChar) { Value = row.Field<string>("column2").Trim() });
var affectedRows = command.ExecuteNonQuery();
}
connection.Close();
}
private SqlConnection GetSqlServerConnection()
{
string connectionString = #"Data Source=...";
SqlConnection connection = new SqlConnection(connectionString);
return connection;
}
private MySqlConnection GetMySQLConnection()
{
MySqlConnectionStringBuilder connectionBuilder = new MySqlConnectionStringBuilder
{
Server = "...",
Database = "...",
UserID = "...",
Password = "...",
Port = 3306
};
MySqlConnection connection = new MySqlConnection(connectionBuilder.ToString());
return connection;
}
Refer below code, you can optimize this code as there is alot of space for optimization but this is simple for beginner to understand basic level:
MySqlConnection conn = new MySqlConnection(constring);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM i_evt WHERE Updt=0", conn);
conn.Open();
DataSet data;
using (MySqlDataAdapter sqlAdapter = new MySqlDataAdapter(mySqlCommand))
{
data = new DataSet();
sqlAdapter.Fill(data);
}
string constring2 = ConfigurationManager.ConnectionStrings["cnxsql"].ConnectionString;
SqlConnection conn2 = new SqlConnection(constring2);
conn2.Open();
for (int i = 0; i < data.Tables[0].Rows.Count; i++)
{
SqlCommand cmd2 = new SqlCommand("INSERT INTO i_evt(column1,column2) values(#col1,#col1)", conn2);
cmd2.Parameters.AddWithValue("col1", data.Tables[0].Rows[i][0].ToString());
cmd2.Parameters.AddWithValue("col12", data.Tables[0].Rows[i][1].ToString());
cmd2.ExecuteNonQuery();
}
conn2.Close();
conn.Close();

SqlCommand with more than one result table

I'm working on an ASP.NET Web Application with Visual Studio 2010. My target framework is ".NET Framework 4" and I'm sending queries to a SQL Server 2008 database which version is "Microsoft SQL Server 2008 R2 (SP2)".
I'm connecting using the following connection string "Data Source=XXXX;Initial Catalog=XXXX;Integrated Security=False;User Id=XXXX;Password= XXXX;MultipleActiveResultSets=True" and sending queries with the code below:
public static List<DataTable> getData(String query)
{
var results = new List<DataTable>();
try
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
command.CommandTimeout = 0;
using (SqlDataReader reader = command.executeReader())
{
do
{
while (reader.Read()) ;
var dataTable = new DataTable();
dataTable.Load(reader);
results.Add(dataTable);
} while (reader.NextResult());
}
connection.Close();
}
}
}
}
The query I'm sending is an Stored Procedure which returns two tables, at first it had a loop which calls another Stored Procedure depending on some internal condition, creation and insertion on a tempdb..#table and two SELECT statements.
But now it only contains:
SELECT 1,2,3,4,5
SELECT 6,7,8,9,0
I don't know why but the reader.NextResult() is always false so I never get the second table result.
Does anyone know what I'm doing wrong? What should I do to receive and read the two results from the query?
if this is using a stored proc you need something like this: notice the command type
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(myConnString))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
{
cmd.CommandText = "myMultipleTablesSP";
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
conn.Close();
}
}
if for example you return 2 tables in your SP, like:
SELECT * FROM [TableA];
SELECT * FROM [TableB];
you would access this tables as:
DataTable tableA = ds.Tables[0];
DataTable tableB = ds.Tables[1];
OK, I have run some test and find out the problem is from dataTable.Load(reader); somehow and I don't know why and what is exactly happening behind that method.
but if you use
do {
while(reader.Read()) {
...
}
} while (reader.NextResult());
everything works as expected.

Kill SQL query execution if it exceeds allotted time in DB2

I'm creating a program in .NET that uses the IBM DB2 .NET provider(IBM.Data.DB2) to connect to a DB2 database(v9.7) and run select queries.
The program should terminate the SQL execution, if a submitted select query takes more than 5 seconds to return the data.
To implement this as per IBM's documentation I could use the QueryTimeout parameter in the connection.
string connStr = "Server=server:12345;Database=db;UID=user;PWD=pass; QueryTimeout = 5;";
DB2Connection conn = null;
DB2Command cmd = null;
conn = new DB2Connection(connStr);
conn.Open();
Console.WriteLine("IBM DB2: " + conn.IsOpen);
if (conn.IsOpen)
{
Console.WriteLine(conn.ConnectionTimeout);
cmd = conn.CreateCommand();
cmd.CommandText = "select * from user.orders";
DB2DataReader reader = cmd.ExecuteReader();
int counter = 0;
while (reader.Read())
{
counter += 1;
Console.WriteLine(reader.GetDB2Int64(0));
}
reader.Close();
}
conn.Close();
When I ran the query in winSQL, the query took about 20 seconds to execute. But when I execute it in this program, it took me the same 20 seconds. As per the documentation the query should have terminated in 5 seconds.
Why has the execution not stopped?
PS: I've also tried setting the cmd.CommandTimeout to 5 and it still would not stop the execution.
Read DB2Command.CommandTimeout property tutorial on IBM website. I hope this will solve your problem. C# example code is below:
[C#]
public void CreateMyDB2Command()
{
string mySelectQuery = "SELECT * FROM EMPLOYEE ORDER BY EMPNO";
DB2Command myCommand = new DB2Command(mySelectQuery);
myCommand.CommandTimeout = 20;
}
The problem with the above example is that ExecuteReader() creates a cursor to the database that pulls the data as an when its read, so the query never times out.
But using a DataAdapter to a dataset, pulls the data in a single go. So the below seems to kill the execution.
string connStr = "Server=server:12345;Database=db;UID=user;PWD=pass;";
conn = new DB2Connection(connStr);
conn.Open();
if (conn.IsOpen)
{
Console.WriteLine(conn.ConnectionTimeout);
cmd = conn.CreateCommand();
cmd.CommandText = "select * from orders";
cmd.CommandTimeout = 5;
DB2DataAdapter adp = new DB2DataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows){
Console.WriteLine(row[0]);
}
}
conn.Close();

Changes not saving to database using SqlDataAdapter

I'm stuck with my changes not getting saved to the SQL Server database.
I can read the database properly, and when calling RefreshGrid() that refreshes from the database, the gridview updates correctly. But not when restarting the program or checking the actual database, then the changes are gone.
I would normally use Entity Framework for this, but sometimes one needs to learn other ways.
private string conString = ConfigurationManager.ConnectionStrings["RewardsConnectionString"].ConnectionString;
private RewardsDS rewardsDset = new RewardsDS();
private SqlDataAdapter CreateDataAdapter(SqlConnection conn)
{
// Build the selection query.
SqlDataAdapter rewardsAdapter = new SqlDataAdapter();
SqlCommand selectCommand = new SqlCommand("SELECT * FROM Purchases", conn);
rewardsAdapter.SelectCommand = selectCommand;
// Build the insertion query
SqlCommand insertCommand = new SqlCommand(
#"INSERT INTO Purchases (PurchaseDate, Amount, CustomerId)
VALUES (#PurchaseDate, #Amount, #CustomerId)", conn);
insertCommand.Parameters.Add("#PurchaseDate", SqlDbType.DateTime, 0, "PurchaseDate"); // Not sure about this zero ...
insertCommand.Parameters.Add("#Amount", SqlDbType.Decimal, 0, "Amount");
insertCommand.Parameters.Add("#CustomerId", SqlDbType.Int, 0, "CustomerId");
rewardsAdapter.InsertCommand = insertCommand;
// Build the update query
SqlCommand updateCommand = new SqlCommand(
#"UPDATE Purchases SET Amount = #Amount, PurchaseDate = #PurchaseDate WHERE PurchaseId = #PurchaseId", conn);
updateCommand.Parameters.Add("#Amount", SqlDbType.Decimal, 0, "Amount");
updateCommand.Parameters.Add("#PurchaseDate", SqlDbType.DateTime, 0, "PurchaseDate");
SqlParameter param = updateCommand.Parameters.Add("#PurchaseId", SqlDbType.Int, 0, "PurchaseId");
param.SourceVersion = DataRowVersion.Original;
rewardsAdapter.UpdateCommand = updateCommand;
// Build the delete query
SqlCommand deleteCommand = new SqlCommand(
"DELETE FROM Purchases WHERE PurchaseId = #PurchaseId", conn);
param = deleteCommand.Parameters.Add("#Id", SqlDbType.Int, 0, "PurchaseId");
param.SourceVersion = DataRowVersion.Original;
rewardsAdapter.DeleteCommand = deleteCommand;
return rewardsAdapter;
}
// Gets changes and saves them to the database.
private void SaveChanges()
{
using (SqlConnection conn = new SqlConnection(conString))
{
var fadapter = CreateDataAdapter(conn);
conn.Open();
DataSet changes = rewardsDset.GetChanges();
try
{
fadapter.Update(changes, "Purchases");
}
catch (Exception ex)
{
MessageBox.Show("There are no updates to be saved");
}
conn.Close();
rewardsDset.AcceptChanges();
}
}
// Refresh the dataGridView from the database
private void RefreshGrid()
{
rewardsDset.Clear();
using (SqlConnection conn = new SqlConnection(conString))
{
conn.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM Purchases", conn);
myAdapter.Fill(rewardsDset, "Purchases");
conn.Close();
dataGridView1.DataSource = null;
this.dataGridView1.DataSource = rewardsDset.Tables["Purchases"];
}
}
You simply have to execute the queries:
rewardsAdapter.InsertCommand.ExecuteNonQuery();
I will start by apologizing for wasting anyone's time. Finally found what was causing this error and it was far more basic than I thought and not at all where I've been trying to look for solutions.
I've made an embarrassing mistake regarding the connection string, but if someone repeats my mistake this was it:
I've been using the connection string to the DataSet, and not to the actual database ...
Adding the proper connection string certainly solved my problem, and was of course impossible for anyone to see with only the code I posted above.

using the same instance of SQLCommand more than one time in the same code for more than one query?

I have question about using why i can not use the same instance of SQLCommand more than one time in the same code?
I tried the code down here and it runs good for the gridview but when i changed the query by using cmd.CommandText() method it keeps saying:
There is already an open DataReader associated with this Command which must be closed first.
This is the code:
string cs = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "Select top 10 FirstName, LastName, Address, City, State from Customers";
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
cmd.CommandText = "SELECT TOP 10 COUNT(CreditLimit) FROM Customers";
int total = (int)cmd.ExecuteScalar();
TotalCreditLble.Text = "The total Credit :" + total.ToString();
}
catch(Exception exp)
{
Response.Write(exp.Message);
}
finally
{
con.Close();
}
The problem is that you are using the SqlCommand object to generate a DataReader via the command.ExecuteReader() command. While that is open, you can't re-use the command.
This should work:
using (var reader = cmd.ExecuteReader())
{
GridView1.DataSource = reader;
GridView1.DataBind();
}
//now the DataReader is closed/disposed and can re-use command
cmd.CommandText = "SELECT TOP 10 COUNT(CreditLimit) FROM Customers";
int total = (int)cmd.ExecuteScalar();
TotalCreditLble.Text = "The total Credit :" + total.ToString();
There is already an open DataReader associated with this Command which must be closed first.
This is the very reason you don't share a command. Somewhere in your code you did this:
cmd.ExecuteReader();
but you didn't leverage the using statement around the command because you wanted to share it. You can't do that. See, ExecuteReader leaves a connection to the server open while you read one row at a time; however that command is locked now because it's stateful at this point. The proper approach, always, is this:
using (SqlConnection c = new SqlConnection(cString))
{
using (SqlCommand cmd = new SqlCommand(sql, c))
{
// inside of here you can use ExecuteReader
using (SqlDataReader rdr = cmd.ExecuteReader())
{
// use the reader
}
}
}
These are unmanaged resources and need to be handled with care. That's why wrapping them with the using is imperative.
Do not share these objects. Build them, open them, use them, and dispose them.
By leveraging the using you will never have to worry about getting these objects closed and disposed.
Your code, written a little differently:
var cs = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
var gridSql = "Select top 10 FirstName, LastName, Address, City, State from Customers";
var cntSql = "SELECT TOP 10 COUNT(CreditLimit) FROM Customers";
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
try
{
using (SqlCommand cmd = new SqlCommand(gridSql, con))
{
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
using (SqlCommand cmd = new SqlCommand(cntSql, con))
{
int total = (int)cmd.ExecuteScalar();
TotalCreditLble.Text = "The total Credit :" + total.ToString();
}
}
catch(Exception exp)
{
Response.Write(exp.Message);
}
}
Thank u quys but for the guys who where talking about using block !
why this code work fine which i seen it on example on a video ! It's the same thing using the same instance of SqlCommand and passing diffrent queries by using the method CommanText with the same instance of SqlCommand and it's execute just fine , this is the code :
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "Delete from tbleProduct where ProductID= 4";
int TotalRowsAffected = cmd.ExecuteNonQuery();
Response.Write("Total rows affected :" + TotalRowsAffected );
cmd.CommandText = "Insert into tbleProduct values (4, 'Calculator', 100, 230)";
TotalRowsAffected = cmd.ExecuteNonQuery();
Response.Write("Total rows affected :" + TotalRowsAffected );
cmd.CommandText = "ypdate tbleProduct set QtyAvailbe = 234 where ProductID = 2";
TotalRowsAffected = cmd.ExecuteNonQuery();
Response.Write("Total rows affected :" + TotalRowsAffected );
}

Categories

Resources