Exemplo n.º 1
0
        /// <summary>
        ///     Called when the Connection instance receives any messages.
        ///     Resets the MySQLConnection object Message and Errors members.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Odbc Message Event Arguments</param>
        private void OnMessage(object sender, OdbcInfoMessageEventArgs e)
        {
            if (IsDisposed)
            {
                return;
            }

            if (e.Errors != null && e.Errors.Count > 0)
            {
                var errList = e.Errors;

                Errors = new OdbcError[errList.Count];

                for (var i = 0; i < errList.Count; i++)
                {
                    Errors[i] = errList[i];
                }
            }
            else
            {
                Errors = null;
            }

            Message = e.Message.Length > 0 ? e.Message : null;
        }
Exemplo n.º 2
0
        private void AsyncSQL()
        {
            while (!asyncQueue.IsCompleted)
            {
                try
                {
                    IList <Tuple <string, string> > colvals;
                    if (asyncQueue.TryTake(out colvals, -1, asyncCanceled.Token))
                    {
                        Save(colvals);
                    }
                }
                catch (OperationCanceledException ex)
                {
                    Log.Info("Canceled async take (queue size: " + asyncQueue.Count + "): " + ex.Message);
                    break;
                }
                catch (OdbcException ex)
                {
                    Log.Error("ODBC Exception in async thread: " + ex.Message);

                    if (Log.Level == EventLogEntryType.Information)
                    {
                        for (int i = 0; i < ex.Errors.Count; i++)
                        {
                            OdbcError err = ex.Errors[i];
                            Log.Info("ODBC Exception details #" + i
                                     + ": message='" + err.Message
                                     + "', native='" + err.NativeError.ToString()
                                     + "', source='" + err.Source
                                     + "', SQL='" + err.SQLState);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("Exception(" + ex.GetType().Name + ") in async thread: " + ex.Message);
                }
            }
        }
    private void Button2_Click(System.Object sender, System.EventArgs e)
    {
        //Dim m_strConnection As String = "server='Server_Name';Initial Catalog='Database_Name';Trusted_Connection=True;"

        //Catch ex As Exception
        //    MessageBox.Show(ex.ToString())
        //End Try

        //Dim objDataset1 As DataSet()
        //Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        //Dim da As OdbcDataAdapter
        System.Windows.Forms.OpenFileDialog OpenFile = new System.Windows.Forms.OpenFileDialog();
        // Does something w/ the OpenFileDialog
        string  strFullPath = null;
        string  strFileName = null;
        TextBox tbFile      = new TextBox();

        // Sets some OpenFileDialog box options
        OpenFile.Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*";
        // Shows only .csv files
        OpenFile.Title = "Browse to file:";
        // Title at the top of the dialog box

        // Makes the open file dialog box show up
        if (OpenFile.ShowDialog() == DialogResult.OK)
        {
            strFullPath = OpenFile.FileName;
            // Assigns variable
            strFileName = Path.GetFileName(strFullPath);

            // Checks to see if they've picked a file
            if (OpenFile.FileNames.Length > 0)
            {
                tbFile.Text = strFullPath;
                // Puts the filename in the textbox

                // The connection string for reading into data connection form
                string connStr = null;
                connStr = "Driver={Microsoft Text Driver (*.txt; *.csv)}; Dbq=" + Path.GetDirectoryName(strFullPath) + "; Extensions=csv,txt ";

                // Sets up the data set and gets stuff from .csv file
                OdbcConnection  Conn        = new OdbcConnection(connStr);
                DataSet         ds          = default(DataSet);
                OdbcDataAdapter DataAdapter = new OdbcDataAdapter("SELECT * FROM [" + strFileName + "]", Conn);
                ds = new DataSet();

                try {
                    DataAdapter.Fill(ds, strFileName);
                    // Fills data grid..
                    DataGridView1.DataSource = ds.Tables(strFileName);
                    // ..according to data source

                    // Catch and display database errors
                } catch (OdbcException ex) {
                    OdbcError odbcError = default(OdbcError);
                    foreach (odbcError in ex.Errors)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                // Cleanup
                OpenFile.Dispose();
                Conn.Dispose();
                DataAdapter.Dispose();
                ds.Dispose();
            }
        }
    }