I'm working on an assignment that requires a simple data transfer data of a SQL Server to MS Excel; using C#. I was able to develop the "foundation" of what I believe works/ However I'm unable to run the program. Assistance is appreciated!
namespace ProjectLab1
{
class Program
{
static void Main(string[] args)
{
//Connection string needed to talk to Omnymbus
string conString = "Driver={MySQL ODBC 5.3 ANSI Driver};"
+ "Server=devry.edupe.net;Port=4300;"
+ "Database=Inventory_XXXX;"
+ "uid=XXXX;pwd=DeVry_Student";
OdbcConnection connection = new OdbcConnection(conString);
connection.Open();
//Get all data
//string theQuery = "SELECT * FROM item";
string theQuery = "SELECT * FROM item i, inventory v where i.invent_id=v.invent_id";
OdbcDataAdapter DataAdapter = new OdbcDataAdapter(theQuery, connection);
DataSet theData = new DataSet();
DataAdapter.Fill(theData, "item");
DataTable theTable = theData.Tables[0];
Console.Write("ITEM".PadRight(30));
Console.Write("QUANT".PadLeft(10));
Console.Write("SIZE".PadLeft(10));
Console.Write("COLOR".PadLeft(10));
Console.Write("PRICE\\n".PadLeft(10));//the "\\n" moves cursor to next line
Console.WriteLine("-".PadLeft(70, '-'));// "WriteLine" places cursor next line
// Loop through all data results
foreach (DataRow dataRow in theTable.Rows)
{
Console.Write(dataRow\["invent\_desc"\].ToString().PadRight(30));
Console.Write(dataRow\["qoh"\].ToString().PadLeft(10));
Console.Write(dataRow\["itemsize"\].ToString().PadLeft(10));
Console.Write(dataRow\["color"\].ToString().PadLeft(10));
Console.WriteLine(((decimal)dataRow\["curr\_price"\]).ToString("C").PadLeft(10));
}
{
StreamWriter file = new StreamWriter(@"C:\\Users\\redfaction\\Desktop\\Data\\" + strFileName);
file.WriteLine(sb.ToString());
File.Close();
connection.Close(); // Close connection
//Have program pause to keep from closing console window
}
}
}
}