コード例 #1
0
ファイル: Form1.cs プロジェクト: fandashtic/sql_export_tool
        private void ExportCSV(string tableName)
        {
            string constr = SingleConnection.Connect(txt_ServerName.Text, txt_Database.Text, txt_UserName.Text, txt_Password.Text);
            string csv    = string.Empty;

            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM " + tableName))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection    = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            foreach (DataColumn column in dt.Columns)
                            {
                                csv += column.ColumnName + ',';
                            }
                            csv += "\r\n";
                            foreach (DataRow row in dt.Rows)
                            {
                                foreach (DataColumn column in dt.Columns)
                                {
                                    csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
                                }
                                csv += "\r\n";
                            }
                        }
                    }
                }
            }
            WriteIntoFile("Tables", tableName, csv, "csv");
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: fandashtic/sql_export_tool
 private void Cmd_Start_Click(object sender, EventArgs e)
 {
     txt_Log.Clear();
     if (ConnectionValidation())
     {
         cmd_Clear.Enabled = false;
         connectionString  = SingleConnection.Connect(txt_ServerName.Text, txt_Database.Text, txt_UserName.Text, txt_Password.Text);
         using (connection = new SqlConnection(connectionString))
         {
             try
             {
                 connection.Open();
                 SqlCommand cmd = new SqlCommand(";WITH ROUTINES AS ( SELECT o.type_desc AS ROUTINE_TYPE ,o.[name] AS ROUTINE_NAME ,m.definition AS ROUTINE_DEFINITION FROM sys.sql_modules AS m INNER JOIN sys.objects AS o ON m.object_id = o.object_id ) SELECT * FROM ROUTINES", connection)
                 {
                     CommandType = CommandType.Text
                 };
                 SqlDataReader reader = cmd.ExecuteReader();
                 if (reader.HasRows)
                 {
                     while (reader.Read())
                     {
                         WriteIntoFile(reader.GetString(0), reader.GetString(1), reader.GetString(2));
                     }
                 }
                 WriteLog("Process completed");
             }
             catch (Exception ex)
             {
                 connection = null;
                 WriteLog("Connection failed. Pleasecheck the inputs");
             }
             finally
             {
                 if (connection != null && connection.State == ConnectionState.Open)
                 {
                     connection.Close();
                 }
             }
         }
         WriteTables();
         WriteLog("OverAll Completed");
         cmd_Clear.Enabled = true;
         MessageBox.Show("OverAll Completed");
         Application.Exit();
     }
 }
コード例 #3
0
ファイル: Form1.cs プロジェクト: fandashtic/sql_export_tool
 private void FillTablesList()
 {
     chk_TableList.Items.Clear();
     connectionString = SingleConnection.Connect(txt_ServerName.Text, txt_Database.Text, txt_UserName.Text, txt_Password.Text);
     using (connection = new SqlConnection(connectionString))
     {
         try
         {
             connection.Open();
             SqlCommand cmd = new SqlCommand("SELECT DISTINCT NAME FROM SYS.TABLES WHERE TYPE = 'U'", connection)
             {
                 CommandType = CommandType.Text
             };
             SqlDataReader reader = cmd.ExecuteReader();
             if (reader.HasRows)
             {
                 while (reader.Read())
                 {
                     chk_TableList.Items.Add(reader.GetString(0));
                 }
             }
             WriteLog("Table list added");
         }
         catch (Exception ex)
         {
             connection = null;
             WriteLog("Connection failed. Pleasecheck the inputs");
         }
         finally
         {
             if (connection != null && connection.State == ConnectionState.Open)
             {
                 connection.Close();
             }
         }
     }
 }
コード例 #4
0
ファイル: Form1.cs プロジェクト: fandashtic/sql_export_tool
 private bool ConnectionValidation()
 {
     if (!string.IsNullOrEmpty(txt_ServerName.Text) && !string.IsNullOrEmpty(txt_Database.Text) && !string.IsNullOrEmpty(txt_UserName.Text) && !string.IsNullOrEmpty(txt_Password.Text))
     {
         try
         {
             cmd_Clear.Enabled = false;
             txt_Log.Clear();
             connectionString = SingleConnection.Connect(txt_ServerName.Text, txt_Database.Text, txt_UserName.Text, txt_Password.Text);
             connection       = new SqlConnection(connectionString);
             connection.Open();
             WriteLog("Connected.");
             cmd_Clear.Enabled = true;
             isConnected       = true;
             return(true);
         }
         catch (Exception ex)
         {
             connection = null;
             WriteLog("Connection failed. Please check the connection inputs");
         }
         finally
         {
             if (connection != null && connection.State == ConnectionState.Open)
             {
                 connection.Close();
             }
             cmd_Clear.Enabled = true;
         }
     }
     else
     {
         WriteLog("Connection failed. Please check the connection inputs");
     }
     return(false);
 }