コード例 #1
0
 public void Insert(string query)
 {
     using (var db = new xConnection())
     {
         db.Connection.Open();
         using (var cmd = db.Connection.CreateCommand())
         {
             cmd.CommandText = @query;
             cmd.ExecuteNonQuery();
         }
     }
 }
コード例 #2
0
        public async void InsertAsync(string query)
        {
            using (var db = new xConnection())
            {
                await db.Connection.OpenAsync();

                using (var cmd = db.Connection.CreateCommand())
                {
                    cmd.CommandText = @query;
                    await cmd.ExecuteNonQueryAsync();
                }
            }
        }
コード例 #3
0
        public async Task <DataTable> QueryAsync(string query, bool isQuery = true)
        {
            using (var db = new xConnection())
            {
                await db.Connection.OpenAsync();

                if (isQuery)
                {
                    using (var cmd = db.Connection.CreateCommand())
                    {
                        cmd.CommandText = @query;
                        try
                        {
                            using (DbDataReader dr = await cmd.ExecuteReaderAsync())
                            {
                                if (dr.HasRows)
                                {
                                    DataTable dt = new DataTable();
                                    dt.Load(dr);
                                    return(dt);
                                }
                            }
                            return(null);
                        }
                        catch (MySqlException e)
                        {
                            Log.Error("MYSQL Error query:" + e.ToString());
                            return(null);
                        }
                    }
                }
                else
                {
                    try
                    {
                        using (var cmd = db.Connection.CreateCommand())
                        {
                            cmd.CommandText = @query;
                            await cmd.ExecuteNonQueryAsync();
                        }
                        return(null);
                    }
                    catch (MySqlException e)
                    {
                        Log.Error("MYSQL Error:" + e.ToString());
                        return(null);
                    }
                }
            }
        }
コード例 #4
0
 public DataTable Query(string query, bool isQuery = true)
 {
     using (var db = new xConnection())
     {
         db.Connection.Open();
         if (isQuery)
         {
             DataTable dt = new DataTable();
             using (var cmd = db.Connection.CreateCommand())
             {
                 cmd.CommandText = @query;
                 try
                 {
                     using (DbDataReader dr = cmd.ExecuteReader())
                     {
                         if (dr.HasRows)
                         {
                             dt.Load(dr);
                             return(dt);
                         }
                         dr.Close();
                     }
                     return(null);
                 }
                 catch (MySqlException e)
                 {
                     Log.Error("MYSQL Error select:" + e.ToString());
                     return(null);
                 }
             }
         }
         else
         {
             try
             {
                 using (var cmd = db.Connection.CreateCommand())
                 {
                     cmd.CommandText = @query;
                     cmd.ExecuteNonQuery();
                 }
             }
             catch (MySqlException e)
             {
                 Log.Error("MYSQL Error:" + e.ToString());
             }
             return(null);
         }
     }
 }
コード例 #5
0
 public void Open()
 {
     try
     {
         using (var db = new xConnection())
         {
             db.Connection.Open();
             Log.Info("Connection Open ! ");
         }
     }
     catch (Exception ex)
     {
         Log.Info("Can not open connection ! ");
     }
 }
コード例 #6
0
 public void Update(string query)
 {
     Task.Run(() =>
     {
         using (var db = new xConnection())
         {
             db.Connection.Open();
             using (var cmd = db.Connection.CreateCommand())
             {
                 cmd.CommandText = @query;
                 cmd.ExecuteNonQuery();
             }
         }
     });
 }
コード例 #7
0
        public async Task <DataTable> MultipleQueryAsync(string query, string nametable, bool isQuery = true)
        {
            using (var db = new xConnection())
            {
                await db.Connection.OpenAsync();

                if (isQuery)
                {
                    using (var cmd = db.Connection.CreateCommand())
                    {
                        cmd.CommandText = @query;

                        try
                        {
                            using (MySqlDataAdapter data = new MySqlDataAdapter(cmd))
                            {
                                DataSet ds = new DataSet();
                                data.Fill(ds, nametable);
                                DataTable dt = ds.Tables[nametable];
                                return(dt);
                            }
                            return(null);
                        }
                        catch (MySqlException e)
                        {
                            Log.Error("MYSQL Error select:" + e.ToString());
                            return(null);
                        }
                    }
                }
                else
                {
                    try
                    {
                        using (var cmd = db.Connection.CreateCommand())
                        {
                            cmd.CommandText = @query;
                            await cmd.ExecuteNonQueryAsync();
                        }
                    }
                    catch (MySqlException e)
                    {
                        Log.Error("MYSQL Error:" + e.ToString());
                    }
                    return(null);
                }
            }
        }