/// <summary> /// 写文本文件 /// </summary> /// <param name="Content"></param> /// <param name="FilePath"></param> /// <returns></returns> public static bool Writer(string Content, string FilePath) { bool check = false; if (!File.Exists(FilePath)) { return(false); } StreamWriter Sw; Sw = File.AppendText(FilePath); try { Sw.WriteLine(Content); check = true; } catch { check = false; } finally { Sw.Close(); Sw = null; } return(check); }
private void Save() { if (mSettingsNeedSaving) { string newSettings = Path.Combine(Path.GetDirectoryName(mSettingsFile), "new." + Path.GetFileName(mSettingsFile)); StreamWriter Sw; Sw = File.CreateText(newSettings); foreach (KeyValuePair <string, string> setting in mSettings) { Sw.WriteLine(setting.Key + mSeparator + setting.Value); } Sw.Close(); } }
public void Close() { Sw.Close(); }
public void WriteToExcel_By_DS(DataSet DS1, string path, string filename) { // this function is used for wiring data in excel..... ds will be called which is alrady filled. Waheed // Try StreamWriter Sw; DataRow DR; // Dim str, fn, s As String string str = ""; string s; int I; int J; int x; int i1; int i2; int no; int page; string PATH1 = (path + "\\"); no = 1; page = 0; x = DS1.Tables[0].Columns.Count; J = DS1.Tables[0].Rows.Count; if (J == 0) { return; } //if (J != 0) //{ Sw = File.CreateText((PATH1 + (filename + ".xls"))); for (i2 = 0; (i2 <= (x - 1)); i2++) { str = str + DS1.Tables[0].Columns[i2].ColumnName + "\t"; } Sw.WriteLine(str); //} //else //{ for (I = 0; (I <= (J - 1)); I++) { if ((((page % 60000) == 0) || (page == 0))) { if ((page != 0)) { Sw.Close(); } if ((no > 1)) { Sw = File.CreateText((PATH1 + (filename + (no + (" - " + (DateTime.UtcNow.AddHours(5.5).ToString("dd-MMM-yy") + ".xls")))))); } else { Sw = File.CreateText((PATH1 + (filename + (" - " + (DateTime.UtcNow.AddHours(5.5).ToString("dd-MMM-yy") + ".xls"))))); } // If page <> 0 Then // Sw.Close() // End If no = (no + 1); for (i2 = 0; (i2 <= (x - 1)); i2++) { str = str + DS1.Tables[0].Columns[i2].ColumnName + "\t"; } Sw.WriteLine(str); str = ""; } page = (page + 1); DR = DS1.Tables[0].Rows[I]; for (i1 = 0; (i1 <= (x - 1)); i1++) { if (Information.IsDBNull(DR[i1])) { s = " "; } else { s = DR[i1].ToString(); } if (Information.IsNumeric(s)) { if ((Math.Round(Convert.ToDouble(s), 0).ToString().Length > 10)) { str = str + "\'" + s + "\t"; } else { str = str + "" + s + "\t"; } } else { str = str + "" + s + "\t"; } } Sw.WriteLine(str); str = ""; } //} Sw.Close(); Sw.Dispose(); // Catch ex As Exception // Finally // If con.State = ConnectionState.Open Then con.Close() // End Try }
static void Main(string[] args) { Option option = new Option(); option.Setup(args); if (args.Length > 0) { if (string.IsNullOrEmpty(option.SqlPath)) { return; } PrepareLogAndExportFile(option); var sql_connection_string = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", option.ServerIp, option.DestDatabase, option.DbAccount, option.DbPassword); var sql_script_content = ReadSql(option); bool has_error = false; while (true) { int encounter_error = 0; try { using (var conn = new SqlConnection(sql_connection_string)) { conn.Open(); foreach (var sql in sql_script_content.Split('\t')) { try { if (string.IsNullOrEmpty(sql)) { continue; } using (var cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = sql; cmd.CommandTimeout = 0; if (string.IsNullOrEmpty(option.ExportPath)) { cmd.ExecuteNonQuery(); } else { if (null == ExportFileSw) { throw new ArgumentNullException("You didn't specify a file path for exporting!", "exportFile"); } CodeScan(sql); OutputResultToExportFile(cmd); } } } catch (Exception ex) { LogMessage("ERROR : " + option.SqlPath + " : " + ex.Message); has_error = true; } } conn.Close(); } break; } catch (SqlException sqlEx) { LogMessage("ERROR : " + sqlEx.Message); if (encounter_error < 3) { LogMessage("Encounter SQL error, wait 5 seconds and retry...."); encounter_error++; Thread.Sleep(5 * 1000); } else { throw; } } } if (null != Sw) { Sw.Close(); } Sw.Dispose(); if (null != ExportFileSw) { ExportFileSw.Close(); } ExportFileSw.Dispose(); //if (has_error) // throw new ApplicationException("Some scripts were running with error, please check out!"); } else { ShowHelp(); } }