0

I am working on asp.net frame work with 4.5 version and i am using mysql database for db transaction. I am using method which is common for DML operations such as insert,update and for select query also and I am using ExecuteNonQuery() method.

Here are my code sample

    public DataSet test(string sql, string Connectionstring)
    {
        DataSet ds = new DataSet();
        try
        {
            string timeZone = '-05:00'
            using (MySqlConnection conn = new MySqlConnection(Connectionstring))
            {
                try
                {
                    conn.Open();
                    MySqlCommand cmd;
                    if (!string.IsNullOrEmpty(timeZone))
                    {
                        cmd = new MySqlCommand();
                        string zoneQuery = "SET SESSION time_zone = '" + timeZone + "';";
                        cmd.CommandText = zoneQuery;
                        cmd.Connection = conn;
                        cmd.CommandTimeout = connectiontimeout;
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                    }
                    cmd = new MySqlCommand();
                    cmd.CommandText = sql;
                    cmd.Connection = conn;
                    cmd.CommandTimeout = connectiontimeout;
                    cmd.ExecuteNonQuery();
                    MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
                    ad.Fill(ds);
                    ad.Dispose();
                    cmd.Dispose();
                    conn.Close();
                }
                finally
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
        }
        catch (Exception ex)
        {
         objlogger.WritetoError("\nError in test: " + ex.StackTrace.ToString());

        }
        return ds;
    }

And my sql is:

 select distinct(s.st),s.a,s.a1,s.a2,s.a3,s.a4,q.a5,l.a6,t.a7
 from test1 as s
   join test2 as q on s.st = q.st 
   join test3 as t on t.Uid = s.Uid 
   join  test4 as l on q.lt1 = l.lt1 
 where s.is1 = false 
   &&  l.lt1 =8 && q.is_deleted = false && t.Is_Deleted = false &&
   s.Is_Deleted = false && l.Is_Deleted = 0;

In the above code select query is working but sometimes it is not giving result.

Here is my connection string:

Server=localhost;Database=abcdtest;Uid=root;Pwd=test;Connection Timeout=60000;CharSet=utf8;Convert Zero Datetime=true;

I also tried with ExecuteReader() please see below

public DataSet test(string sql, string Connectionstring)
    {
        DataSet ds = new DataSet();
        try
        {
            string timeZone = '-04:00';
            int connectiontimeout = 60000;               
            using (MySqlConnection conn = new MySqlConnection(Connectionstring))
            {
                try
                {
                    conn.Open();
                    MySqlCommand cmd;
                    MySqlDataReader myReader;
                    if (!string.IsNullOrEmpty(timeZone))
                    {
                        cmd = new MySqlCommand();
                        string zoneQuery = "SET SESSION time_zone = '" + timeZone + "';";
                        cmd.CommandText = zoneQuery;
                        cmd.Connection = conn;
                        cmd.CommandTimeout = connectiontimeout;
                        myReader = cmd.ExecuteReader();
                        myReader.Close();
                        cmd.Dispose();
                    }
                    MySqlDataAdapter adapter = new MySqlDataAdapter();
                    adapter.SelectCommand = new MySqlCommand(sql, conn);
                    adapter.Fill(ds);
                    conn.Close();
                }
                finally
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            objlogger.WritetoError("Error in test : " + ex.Message);
        }
        return ds;
    }
santosh
  • 435
  • 1
  • 7
  • 24
  • 3
    "sometimes it is not giving result" - so what happens when it goes wrong? The *first* thing I'd do is to remove the empty catch block that is swallowing all exceptions, hiding problems from you. I'd then use parameterized SQL instead of appending `timeZone` within the conditional part. I'd then use `using` statements for everything that's disposable, removing all the code that manually calls `Close` and `Dispose`. These steps almost certainly won't fix the problem, but they'll leave you with cleaner code and I suspect you'll see an exception explaining what's going wrong. – Jon Skeet Apr 03 '23 at 09:41
  • 1
    @santosh In addition to all the great changes suggested by @jon-skeet, change your ExecuteNonQuery to `ExecuteReader` since the select query will be returning data set. ExecuteNonQuery is only for non-row returning operations. – Anand Sowmithiran Apr 03 '23 at 09:59
  • @AnandSowmithiran DataAdapter does call ExecuteReader itself the previous call to ExecuteNonQuery is unnecessary or even hurting and maybe part of the problem. – Ralf Apr 03 '23 at 10:07
  • @AnandSowmithiran I tried with ExecuteReader() but not working please see updated code. – santosh Apr 03 '23 at 10:19
  • @Ralf I tried with ExecuteReader() but not working please see updated code. – santosh Apr 03 '23 at 10:20
  • 2
    "but not working" is *never* enough information. You *have* more information - you know the way in which it's not working, what you've observed, whether exceptions are thrown or not, etc. Please don't make us ask repeatedly for information which is clearly relevant. – Jon Skeet Apr 03 '23 at 10:27
  • @santosh you are returning a dataset from the test() method, but you already disposed it , just after calling the Fill(), whats the fun in that!? – Anand Sowmithiran Apr 03 '23 at 10:31
  • @AnandSowmithiran I update method and removed ds.Dispose(); but still not workig – santosh Apr 03 '23 at 10:43
  • See this page to create a [Minimum repro example](https://stackoverflow.com/help/minimal-reproducible-example) and then understand the workings, then debug the problem. You are not saying yet what you mean by "not working" - are you getting any SQL error or exception? – Anand Sowmithiran Apr 03 '23 at 10:54
  • @AnandSowmithiran Actually we have more than 20 database with same DDL and it is working with most of all database but for some of db it is not giving result and when i am running that query on sqlyog than it is giving result. – santosh Apr 03 '23 at 11:07
  • There is code in the following posts that may be helpful: [MySqlDataAdapter post](https://stackoverflow.com/a/68510196/10024425) and [MySqlDataReader post](https://stackoverflow.com/a/71540398/10024425) – Tu deschizi eu inchid Apr 03 '23 at 14:42
  • You shouldn't be using the `root` user in your app. Create a new user and grant only the necessary permissions to that user. – Tu deschizi eu inchid Apr 03 '23 at 14:44
  • After replacing && with AND and || with OR in the query then it is working. – santosh Apr 04 '23 at 10:15

0 Answers0