コード例 #1
0
ファイル: Form1.cs プロジェクト: perjahn/PerJahnUtils
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt;
                using (db mydb = new db(cbProviders.SelectedValue.ToString(), cbConnstr.Text))
                {
                    mydb.FillSchema = cbSchema.Checked;

                    dt = mydb.ExecuteDataTableSQL(tbSql.Text, null);

                    if (cbSchema.Checked)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            dc.ColumnName = dc.DataType + "|" + dc.ColumnName + "|";
                        }
                    }
                }

                if (cbSaveToFile.Checked)
                {
                    if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }

                    string filename = saveFileDialog1.FileName;

                    string separator;
                    if (saveFileDialog1.FilterIndex == 1)
                    {
                        separator = ",";  // 1=csv
                    }
                    else
                    {
                        separator = "\t";  // 2=tab
                    }

                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename))
                    {
                        for (int c = 0; c < dt.Columns.Count; c++)
                        {
                            sw.Write((c == 0 ? string.Empty : separator) + dt.Columns[c].ColumnName);
                        }
                        sw.WriteLine();

                        foreach (DataRow dr in dt.Rows)
                        {
                            for (int c = 0; c < dt.Columns.Count; c++)
                            {
                                sw.Write((c == 0 ? string.Empty : separator) + dr[c]);
                            }
                            sw.WriteLine();
                        }
                    }
                }
                else
                {
                    dataGridView1.DataSource = null;  // Forget column order
                    dataGridView1.DataSource = dt;
                }
                this.Text = "SQL Util - Rows: " + dt.Rows.Count + ", Columns: " + dt.Columns.Count;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: perjahn/PerJahnUtils
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt;
                using (db mydb = new db(cbProviders.SelectedValue.ToString(), cbConnstr.Text))
                {
                    mydb.FillSchema = cbSchema.Checked;

                    dt = mydb.ExecuteDataTableSQL(tbSql.Text, null);

                    if (cbSchema.Checked)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            dc.ColumnName = dc.DataType + "|" + dc.ColumnName + "|";
                        }
                    }
                }

                if (cbSaveToFile.Checked)
                {
                    if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }

                    string filename = saveFileDialog1.FileName;

                    string separator;
                    if (saveFileDialog1.FilterIndex == 1)
                    {
                        separator = ",";  // 1=csv
                    }
                    else
                    {
                        separator = "\t";  // 2=tab
                    }

                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename))
                    {
                        for (int c = 0; c < dt.Columns.Count; c++)
                        {
                            sw.Write((c == 0 ? string.Empty : separator) + dt.Columns[c].ColumnName);
                        }
                        sw.WriteLine();

                        foreach (DataRow dr in dt.Rows)
                        {
                            for (int c = 0; c < dt.Columns.Count; c++)
                            {
                                sw.Write((c == 0 ? string.Empty : separator) + dr[c]);
                            }
                            sw.WriteLine();
                        }
                    }
                }
                else
                {
                    dataGridView1.DataSource = null;  // Forget column order
                    dataGridView1.DataSource = dt;
                }
                this.Text = "SQL Util - Rows: " + dt.Rows.Count + ", Columns: " + dt.Columns.Count;

            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }