using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.IO; namespace PromsGetDoc { class Program { // arguments are: // docId // filename/pathname for resulting word doc // sql server name // database name // Can be run from command line or from macro in microsoft office tools as shown here: // Sub Macro1() // Call Shell("""Path_To_Exe\PromsGetDoc.exe"" 5 C:\temp\sample3.doc .\sqlexpress veproms_app", vbNormalFocus) // End Sub static void Main(string[] args) { if (args.Length != 4) { Console.WriteLine("Need 4 arguments: DocId, Output File/Path, Server Name, Database Name"); return; } Console.WriteLine("Volian Enterprises Proprietary Software - Copyright 2015 \r\n\n" + "To be used under the Volian PROMS End User License Agreement (EULA).\r\n\nFor AP-1000 Westinghouse internal use only.\r\n"); byte[] doc = null; int _docId = int.Parse(args[0]); string connectionStr = string.Format(@"Data Source={0};Initial Catalog={1};Integrated Security=True", args[2], args[3]); try { using (SqlConnection cn = new SqlConnection(connectionStr)) { cn.Open(); // do I need this? using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "getDocument"; cm.Parameters.AddWithValue("@DocID", _docId); cm.CommandTimeout = 100; using (SqlDataReader dr = cm.ExecuteReader()) { if (!dr.Read()) { Console.WriteLine("ERROR: No Record Found"); return; } doc = (byte[])dr.GetValue(2); } } } } catch (Exception ex) { Console.WriteLine("ERROR: Could not access/read data. Check server and database name. " + ex.Message); return; } if (doc != null) { try { FileInfo fi = new FileInfo(args[1]); FileStream fs = fi.Create(); fs.Write(doc, 0, doc.Length); fs.Close(); Console.WriteLine("{0} was successfully created.", fi.FullName); } catch (Exception ex1) { Console.WriteLine("ERROR: Could not write Word file. " + ex1.Message); return; } } } } }