Files
panaceantech/QuickFTP/gQuery/Program.cs

125 lines
4.0 KiB
C#

using Sdaleo;
using Sdaleo.Systems.Advantage;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Yaulw.Assembly;
using Yaulw.File;
using Yaulw.Registry;
namespace quickftp
{
class Program
{
public static string DataRet_Retrieve(object o)
{
if(o != null && !String.IsNullOrEmpty(o.ToString()))
{
return o.ToString().Trim();
}
return "";
}
static void Main(string[] args)
{
string path = gQuery.Properties.Settings.Default.AdvantagePath;
string user = gQuery.Properties.Settings.Default.AdvantageUser;
string pass = gQuery.Properties.Settings.Default.AdvantagePass;
string fpath = AssemblyW.SpecializedAssemblyInfo.GetAssemblyPath(AssemblyW.AssemblyST.Executing);
string fname = DateTime.Now.ToShortDateString().Replace('/', '_');
if (String.IsNullOrEmpty(path) || String.IsNullOrEmpty(user) || String.IsNullOrEmpty(pass))
{
Console.WriteLine("Advantage settings blank or null");
return;
}
AdvantageCredential cred = new AdvantageCredential(path, user, pass, AdvantageCredential.ServerType.REMOTE_LOCAL);
if(!cred.IsValid)
{
Console.WriteLine("Advantage connection invalid");
return;
}
string query1 = ReadQuery("query1.txt");
if (String.IsNullOrEmpty(query1))
return;
DB db = DB.Create(cred);
DBRetVal retVal = db.FillDataTable(query1);
if(!retVal.IsValid)
{
Console.WriteLine(retVal.ErrorMsg);
return;
}
try
{
DataTable dt = retVal.GetDataTableRetVal();
StringBuilder sbColumns = new StringBuilder();
foreach (DataColumn c in dt.Columns)
{
sbColumns.Append(c.ColumnName);
sbColumns.Append(',');
}
String columnHeader = sbColumns.ToString().Substring(0, sbColumns.Length - 1);
String[] columns = columnHeader.Split(',');
FileWriter fw = new FileWriter(fname, "csv", fpath);
fw.WriteLineA(columnHeader);
foreach (DataRow r in dt.Rows)
{
StringBuilder sbRow = new StringBuilder();
for (int i = 0; i < columns.Length; ++i)
{
sbRow.Append(DataRet_Retrieve(r[columns[i]]));
sbRow.Append(',');
}
String rowValues = sbRow.ToString().Substring(0, sbRow.Length - 1);
fw.WriteLineA(rowValues);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Process.GetCurrentProcess().Close();
}
public static string ReadQuery(string filename)
{
try
{ // Open the text file using a stream reader.
using (StreamReader sr = new StreamReader(filename))
{
// Read the stream to a string, and write the string to the console.
String line = sr.ReadToEnd();
return line;
}
}
catch (Exception e)
{
Console.WriteLine(String.Format("The file could not be read: {0}", filename));
Console.WriteLine(e.Message);
}
return "";
}
}
}