I am writing my C# application that connects to database, does couple selects and then inserts record back to the server in the network.
But I have around 40k records and my program processes one records like for 1 second.
I don’t know how to improve the performance. Here are my sql getter and inserter. Any suggestions?
public bool insert_and_ConfirmSQL(String Query, String comments)
{
bool success = false;
NpgsqlCommand cmd = new NpgsqlCommand();
NpgsqlConnection mycon = new NpgsqlConnection();
string connstring = String.Format("Server={0};Port={1}; User Id={2};Password={3};Database={4};", tbHost, tbPort, tbUser, tbPass, tbDataBaseName);
mycon.ConnectionString = connstring;
cmd = mycon.CreateCommand();
cmd.CommandText = Query;
mycon.Open();
int temp = 0;
try
{
temp = cmd.ExecuteNonQuery();
success = true;
}
catch
{
success = false;
}
finally
{
if (mycon.State.ToString() == "Open")
{
mycon.Close();
}
}
return success;
}
public String getString(String sql, NpgsqlConnection conn)
{
using (DataSet ds = new DataSet())
{
using (NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn))
{
da.Fill(ds);
if (ds.Tables.Count > 0)
{
DataTable dt = ds.Tables[0];
// check count of Rows
if (dt.Rows.Count > 0)
{
object o = dt.Rows[0][0];
if (o != DBNull.Value && o != null)
{
return o.ToString();
}
}
}
}
}
// Return default value
return "";
}
Sometimes you can improve performance by using a sql Datareader instead of DataSets.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader(v=vs.100).aspx#Y0
You then can Inspect the data while you are reading it from the database.
So I’d implement the above code with a DataReader and retime it.
Edit: Especially that getString method. if that ds.Fill was taking 40k rows that could be the cause of your performance problem.