Using some code I found online, I am able to insert static data into a Google Sheet. Ultimately, I'd like to import data from a SQL stored procedure into a Google Sheet on-demand.
The static code below works just fine:
{
// Specifying Column Range for reading...
var range = $"{sheet}!A:E";
var valueRange = new ValueRange();
// Data for another Student...
var oblist = new List<object>() { "John", "Doe", "john@test.com", "5553332121" };
valueRange.Values = new List<IList<object>> { oblist };
// Append the above record...
var appendRequest = service.Spreadsheets.Values.Append(valueRange, SpreadsheetId, range);
appendRequest.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED;
var appendReponse = appendRequest.Execute();
}
I have the following code I use to create a data table with my stored procedure result set:
(Note that the # of columns is dynamic as this is a pivot)
string lConnString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
string procedure = "usp_get_menupivot";
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(lConnString))
{
conn.Open();
using (SqlCommand command = new SqlCommand(procedure, conn))
{
using (var da = new SqlDataAdapter(command))
{
command.CommandType = CommandType.StoredProcedure;
da.Fill(table);
}
}
}
string data = string.Empty;
StringBuilder sb = new StringBuilder();
if (null != table && null != table.Rows)
{
foreach (DataRow dataRow in table.Rows)
{
foreach (var item in dataRow.ItemArray)
{
sb.Append(item);
sb.Append(',');
}
sb.AppendLine();
}
data = sb.ToString();
}
How can I combine these two scripts to allow me to insert the SQL data to the Google Sheet?