Why can I not Cast ServiceStack Ormlite Connection to SqlConnection? - c#

I am trying to use SqlBulkCopy with ServiceStack Ormlite and have written the below extension method:
public static void BulkInsertSqlServer<T>(this IDbConnection dbConn, string targetTable, IEnumerable<T> data, params string[] columns)
{
Ensure.NotNull(dbConn);
Ensure.That(dbConn.State == ConnectionState.Open);
Ensure.NotNullOrEmptyOrWhiteSpace(targetTable);
Ensure.NotNull(data);
Ensure.NotNullOrEmpty(columns);
var sqlConnection = dbConn as SqlConnection;
using (var bcp = new SqlBulkCopy(sqlConnection))
using (var reader = ObjectReader.Create(data, columns))
{
bcp.BatchSize = data.Count();
bcp.DestinationTableName = targetTable;
bcp.WriteToServer(reader);
}
}
Which I am using by:
_connFactory = new OrmLiteConnectionFactory(connStr, SqlServerOrmLiteDialectProvider.Instance);
using (var db = _connFactory.Open())
using (var tran = db.BeginTransaction())
{
db.BulkInsertSqlServer("User", users, "Name", "Age");
var allRoles = new List<Role>();
foreach (var listOfRoles in users.Select(d => d.Roles))
{
allRoles.AddRange(listOfRoles);
}
db.BulkInsertSqlServer("Role", allRoles, "Name", "UserId", "IsAdmin");
tran.Commit();
}
However sqlConnection is always null, any ideas?

Because the connection is wrapped in a managed OrmLiteConnectionWrapper, you can get the SqlConnection with:
var adoNetConn = ((IHasDbConnection)dbConn).DbConnection;
var sqlConnection = adoNetConn as SqlConnection;

Related

Access multiple tables returned by stored procedure [duplicate]

Can I execute multiple queries and return their results executing a SqlCommand just once?
See SqlDataReader.NextResult (an SqlDataReader is returned from calling SqlCommand.ExecuteReader):
Advances the data reader to the next result [set], when reading the results of batch Transact-SQL statements.
Example:
string commandText = #"SELECT Id, ContactId
FROM dbo.Subscriptions;
SELECT Id, [Name]
FROM dbo.Contacts;";
List<Subscription> subscriptions = new List<Subscription>();
List<Contact> contacts = new List<Contact>();
using (SqlConnection dbConnection = new SqlConnection(#"Data Source=server;Database=database;Integrated Security=true;"))
{
dbConnection.Open();
using (SqlCommand dbCommand = dbConnection.CreateCommand())
{
dbCommand.CommandText = commandText;
using(SqlDataReader reader = dbCommand.ExecuteReader())
{
while(reader.Read())
{
subscriptions.Add(new Subscription()
{
Id = (int)reader["Id"],
ContactId = (int)reader["ContactId"]
});
}
// this advances to the next resultset
reader.NextResult();
while(reader.Read())
{
contacts.Add(new Contact()
{
Id = (int)reader["Id"],
Name = (string)reader["Name"]
});
}
}
}
}
Other examples:
C# Multiple Result Sets
Executing a Query That Returns Multiple Result Sets with SqlDataReader : SqlCommand Select « ADO.Net « C# / CSharp Tutorial
Create a Stored Procedure that has multiple selects, and fill the DataSet.
using (SqlConnection conn = new SqlConnection(connection))
{
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("MyProcedure", conn);
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.Fill(dataset);
return dataset;
}
The returned dataset will have a DataTable in it's Tables array for each select in the stored procedure.
Tools like "dapper" make this very easy, regardless of whether you use ad-hoc text queries or stored procedures; for example:
using(var multi = conn.QueryMultiple(sql, args))
{
var customers = multi.Read<Customer>().AsList(); // first grid
var regionName = multi.ReadFirstOrDefault<string>(); // second grid
var addresses = multi.Read<Address>().AsList(); // third grid
// todo: use those things
}
Individual grids can also be read without buffering (as an open IEnumerable<T> over the reader itself) via the optional parameters to Read[<T>].
I am using a dictionary approach.
You can use Newton Json to convert it to json.
This way you are not tied to types and IDataRecord
public List<List<Dictionary<string, object>>> ExecuteSqlReader(string cmd, int commandTimeout = 30, CommandType commandType = CommandType.Text)
{
var sqlCmd = new SqlCommand(cmd);
var allRecord = new List<List<Dictionary<string, object>>>();
using (var connection = GetSqlConnection(commandTimeout, commandType, ref sqlCmd))
{
using (var reader = sqlCmd.ExecuteReader())
{
if (reader.HasRows)
{
var result = new List<Dictionary<string, object>>();
while (reader.Read())
{
result = GetTableRowData(reader);
}
allRecord.Add(result);
}
while (reader.NextResult())
{
if (reader.HasRows)
{
var result = new List<Dictionary<string, object>>();
while (reader.Read())
{
result = GetTableRowData(reader);
}
allRecord.Add(result);
}
}
}
}
return allRecord;
}
I call a sproc and get multiple result sets with object, so you end up with a
List<List<Dictionary<string, object>>>
In the multiResultsSet each results set is then
List<Dictionary<string, object>>
They can be cast to their types and transformed into the models as needed.
After you set up the sproc command with everything needed, pass it to this:
private static List<List<Dictionary<string, object>>> ProcessReader(SqlCommand command)
{
var tables = new List<List<Dictionary<string, object>>>();
using (var reader = command.ExecuteReader())
{
do
{
var table = new List<Dictionary<string, object>>();
while (reader.Read())
table.Add(Read(reader));
tables.Add(table);
} while (reader.NextResult());
}
return tables;
}
and Read() is fairly straight forward.
private static Dictionary<string, object> Read(IDataRecord reader)
{
var row = new Dictionary<string, object>();
for (var i = 0; i < reader.FieldCount; i++)
{
var val = reader[i];
row[reader.GetName(i)] = val == DBNull.Value ? null : val;
}
return row;
}
This is what i have been using for returning multiple result sets.
public abstract class BaseRepo
{
private string _connectionString;
protected BaseRepo(string connectionString)
{
_connectionString = connectionString;
}
private SqlConnection GetSqlConnection(int commandTimeout, CommandType commandType, ref SqlCommand sqlCmd)
{
var connection = new SqlConnection(_connectionString);
connection.Open();
sqlCmd.Connection = connection;
sqlCmd.CommandTimeout = commandTimeout;
sqlCmd.CommandType = commandType;
return connection;
}
protected int ExecuteSql(SqlCommand sqlCmd, int commandTimeout = 30, CommandType commandType = CommandType.Text)
{
using (var connection = GetSqlConnection(commandTimeout, commandType, ref sqlCmd))
{
return sqlCmd.ExecuteNonQuery();
}
}
protected IEnumerable<T> ExecuteSqlReader<T>(Func<IDataRecord, T> CreateObject, SqlCommand sqlCmd, int commandTimeout = 30, CommandType commandType = CommandType.Text)
{
using (var connection = GetSqlConnection(commandTimeout, commandType, ref sqlCmd))
{
using (var reader = sqlCmd.ExecuteReader())
return ExecuteReader(CreateObject, reader);
}
}
protected Tuple<IEnumerable<T1>, IEnumerable<T2>> ExecuteSqlReader<T1,T2>(Func<IDataRecord, T1> CreateObject1, Func<IDataRecord, T2> CreateObject2, SqlCommand sqlCmd, int commandTimeout = 30, CommandType commandType = CommandType.Text)
{
using (var connection = GetSqlConnection(commandTimeout, commandType, ref sqlCmd))
{
using (var reader = sqlCmd.ExecuteReader())
{
var result1 = ExecuteReader(CreateObject1, reader).ToList();
var result2 = ExecuteReader(CreateObject2, reader).ToList();
return Tuple.Create<IEnumerable<T1>, IEnumerable<T2>>(result1, result2);
}
}
}
protected Tuple<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>> ExecuteSqlReader<T1, T2, T3>(Func<IDataRecord, T1> CreateObject1, Func<IDataRecord, T2> CreateObject2, Func<IDataRecord, T3> CreateObject3, SqlCommand sqlCmd, int commandTimeout = 30, CommandType commandType = CommandType.Text)
{
using (var connection = GetSqlConnection(commandTimeout, commandType, ref sqlCmd))
{
using (var reader = sqlCmd.ExecuteReader())
{
var result1 = ExecuteReader(CreateObject1, reader).ToList();
var result2 = ExecuteReader(CreateObject2, reader).ToList();
var result3 = ExecuteReader(CreateObject3, reader).ToList();
return Tuple.Create<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>>(result1, result2, result3);
}
}
}
protected Tuple<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>> ExecuteSqlReader<T1, T2, T3, T4>(Func<IDataRecord, T1> CreateObject1, Func<IDataRecord, T2> CreateObject2, Func<IDataRecord, T3> CreateObject3, Func<IDataRecord, T4> CreateObject4, SqlCommand sqlCmd, int commandTimeout = 30, CommandType commandType = CommandType.Text)
{
using (var connection = GetSqlConnection(commandTimeout, commandType, ref sqlCmd))
{
using (var reader = sqlCmd.ExecuteReader())
{
var result1 = ExecuteReader(CreateObject1, reader).ToList();
var result2 = ExecuteReader(CreateObject2, reader).ToList();
var result3 = ExecuteReader(CreateObject3, reader).ToList();
var result4 = ExecuteReader(CreateObject4, reader).ToList();
return Tuple.Create<IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>>(result1, result2, result3, result4);
}
}
}
private IEnumerable<T> ExecuteReader<T>(Func<IDataRecord, T> CreateObject, SqlDataReader reader)
{
while (reader.Read())
{
yield return CreateObject(reader);
}
reader.NextResult();
}
}
Then I just inherit it like so:
public class ReviewRepo : BaseRepo
{
public ReviewRepo(string connectionString) : base(connectionString) { }
public ReviewPageableResult GetAllReviews(string productType, string serviceType, int pageNumber, int itemsPerPage, string sortBy, string sortDirection)
{
var parameters = new List<SqlParameter>
{
new SqlParameter("ProductRefDescription", productType),
new SqlParameter("ServiceRefDescription", serviceType),
new SqlParameter("ZipCodes", "NULL"),
new SqlParameter("PageNumber", pageNumber),
new SqlParameter("ItemsPerPage", itemsPerPage),
new SqlParameter("SortBy", sortBy),
new SqlParameter("SortDirection", sortDirection)
};
var cmd = new SqlCommand("dbo.GetReviews");
cmd.Parameters.AddRange(parameters.ToArray());
var results = ExecuteSqlReader(CreateReview, CreateReviewPageableResult, cmd, commandType: CommandType.StoredProcedure);
var reviewResult = results.Item2.Single();
reviewResult.Items = results.Item1;
return reviewResult;
}
public ReviewPageableResult GetReviewsByZip(string productType, string serviceType, string zipCodes, int pageNumber, int itemsPerPage, string sortBy, string sortDirection)
{
var parameters = new List<SqlParameter>
{
new SqlParameter("ProductRefDescription", productType),
new SqlParameter("ServiceRefDescription", serviceType),
new SqlParameter("ZipCodes", zipCodes),
new SqlParameter("PageNumber", pageNumber),
new SqlParameter("ItemsPerPage", itemsPerPage),
new SqlParameter("SortBy", sortBy),
new SqlParameter("SortDirection", sortDirection)
};
var cmd = new SqlCommand("dbo.GetReviewsByZipCodes");
cmd.Parameters.AddRange(parameters.ToArray());
var results = ExecuteSqlReader(CreateReview, CreateReviewPageableResult, cmd, commandType: CommandType.StoredProcedure);
var reviewResult = results.Item2.Single();
reviewResult.Items = results.Item1;
return reviewResult;
}
private Review CreateReview(IDataRecord record)
{
return new Review
{
PageReviewId = (int)record["PageReviewId"],
ProductRefId = (Guid)record["ProductRefId"],
ServiceTypeRefId = Convert.IsDBNull(record["ServiceTypeRefId"]) ? Guid.Empty : (Guid)record["ServiceTypeRefId"],
TerritoryId = Convert.IsDBNull(record["TerritoryId"]) ? Guid.Empty : (Guid)record["TerritoryId"],
FirstName = $"{record["FirstName"]}",
LastName = $"{record["LastName"]}",
City = $"{record["City"]}",
State = $"{record["State"]}",
Answer = $"{record["Answer"]}",
Rating =(double)record["Rating"],
SurveyDate = (DateTime)record["SurveyDate"]
};
}
private ReviewPageableResult CreateReviewPageableResult(IDataRecord record)
{
return new ReviewPageableResult
{
AverageRating = (double)record["AverageRating"],
Count1Stars = (int)record["Count1Stars"],
Count2Stars = (int)record["Count2Stars"],
Count3Stars = (int)record["Count3Stars"],
Count4Stars = (int)record["Count4Stars"],
Count5Stars = (int)record["Count5Stars"],
ItemsPerPage = (int)record["ItemsPerPage"],
PageNumber = (int)record["PageNumber"],
TotalCount = (int)record["TotalCount"],
};
}
}
Try this
Dim dt1, dt2, dt3As New DataTable
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim Sql1, Sql2, Sql3 As String
Sql1 = "select id, CurName from Table1 where IS_Deleted=0 order by id"
Sql2 = "select id ,Item from Table2 order by id"
Sql3 = "select id ,SellPrice from Table3 order by id"
Try
conn1.Open()
command = New SqlCommand(Sql1, conn1)
command.CommandType = CommandType.Text
adapter.SelectCommand = command
adapter.Fill(ds, "dt1")
adapter.SelectCommand.CommandText = Sql2
adapter.Fill(ds, "dt2")
adapter.SelectCommand.CommandText = Sql3
adapter.Fill(ds, "dt3")
adapter.Dispose()
command.Dispose()
conn1.Close()
cmbCurrency.DataSource = ds.Tables("dt1")
cmbCurrency.DisplayMember = "CurName"
cmbCurrency.ValueMember = "id"
cmbCurrency.SelectedIndex = -1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
cmbGroups.DataSource = ds.Tables("dt2")
cmbGroups.DisplayMember = "Item"
cmbGroups.ValueMember = "id"
cmbGroups.SelectedIndex = -1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
cmbUnits.DataSource = ds.Tables("dt3")
cmbUnits.DisplayMember = "SellPrice"
cmbUnits.ValueMember = "id"
cmbUnits.SelectedIndex = -1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''|
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
For More Help http://vb.net-informations.com/dataset/dataset-multiple-tables-sqlserver.htm

Does my code close connection to SQL Server

So method GetAllData is one of my universal methods for connecting to the database and here, since I have to add mupltiple records in once to the database I need to have one connetion with multiple commands to run. So my question is does this way of connection closes the database correctly, or if you have any improvments to my code please share with me.
var conn = new SqlConnection(ConnString);
conn.Open();
var data = new Dictionary<string, List<object>>();
foreach (var h in hours)
{
data += SqlUniversal.GetAllData(query,//idk how I will collect the data yet... i know += wouldnt work for dictionary
new[] {
//some parameters
},
conn);
}
//Here is the method, above is how I call it.
public static Dictionary<string, List<object>> GetAllData(string command, SqlParameter[] pars, SqlConnection conn)
{
if (conn == null)
{
conn = new SqlConnection(ConnString);
conn.Open();
}
var res = new Dictionary<string, List<object>>();
using (conn)
{
using (var cmd = new SqlCommand(command, conn))
{
cmd.Parameters.AddRange(pars);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
for (var i = 0; i < reader.VisibleFieldCount; i++)
{
if (reader.GetValue(i) == DBNull.Value)
continue;
var name = reader.GetName(i);
if (res.ContainsKey(name))
res[name].Add(reader.GetValue(i));
else
res.Add(name, new List<object> {reader.GetValue(i)});
}
return res;
}
}
}
}
Probably the better way to do this is to let the overseeing method manage the connection:
public void SomeDataMethod()
{
using (var conn = new SqlConnection(ConnString))
{
conn.Open();
var data = new Dictionary<string, List<object>>();
foreach (var h in hours)
{
data += SqlUniversal.GetAllData(query,
new[] {
//some parameters
},
conn);
}
}
}
And remove the using (conn) from the GetData method:
public static Dictionary<string, List<object>> GetAllData(string command, SqlParameter[] pars, SqlConnection conn)
{
var res = new Dictionary<string, List<object>>();
using (var cmd = new SqlCommand(command, conn))
{
cmd.Parameters.AddRange(pars);
using (var reader = cmd.ExecuteReader())
{
//...
return res;
}
}
}
Now SomeDataMethod controls the connection lifetime and GetData doesn't worry about connection management.

c# genereic sql context patameter

I have a mssql-context-class for easy access to the database. It contains a function for inserting datarows, that looks like this:
public int? Insert(string tableName, Action<SqlParameterCollection> actionSqlParameterCollection)
{
using (var sqlConnection = new SqlConnection(ConnectionString))
{
sqlConnection.Open();
using (var sqlCommand = sqlConnection.CreateCommand())
{
var commandText = $"insert into {tableName} (#columns) output inserted.id values (#values)";
var valueBuilder = new StringBuilder();
var columnBuilder = new StringBuilder();
actionSqlParameterCollection?.Invoke(sqlCommand.Parameters); //Fill the parameters from outside with some values
foreach (SqlParameter parameter in sqlCommand.Parameters)
{
valueBuilder.Append($",#{parameter.ParameterName}");
columnBuilder.Append($",{parameter.ParameterName}");
}
commandText = commandText.Replace("#values", valueBuilder.ToString().Substring(1));
commandText = commandText.Replace("#columns", columnBuilder.ToString().Substring(1));
sqlCommand.CommandText = commandText;
object result = sqlCommand.ExecuteScalar();
return (int?)result;
}
}
}
Calling this would look something like this:
var context = MsSqlContext.CreateFrom("some_connectionstring");
context.Insert("myTable", parameters => {
parameters.AddWithValue("foo_1", "bar_1");
parameters.AddWithValue("foo_2", "bar_2");
});
Now i want to build a generic sql-context-class which can also handle mysql-databases. The insert-function looks like this so far:
public int? Insert(string tableName, Action<IDataParameterCollection> actionParameterCollection)
{
using (var connection = this.CreateConnection())
{
using (var command = connection.CreateCommand())
{
var commandText = $"insert into {tableName} (#field) values (#values)";
var valueBuilder = new StringBuilder();
var columnBuilder = new StringBuilder();
actionParameterCollection?.Invoke(command.Parameters);
foreach (IDbDataParameter parameter in command.Parameters)
{
valueBuilder.Append($",#{parameter.ParameterName}");
columnBuilder.Append($",{parameter.ParameterName}");
}
commandText = commandText.Replace("#values", valueBuilder.ToString().Substring(1));
commandText = commandText.Replace("#columns", columnBuilder.ToString().Substring(1));
command.CommandText = commandText;
object result = command.ExecuteScalar();
return (int?)result;
}
}
}
When i try to call the function it looks like this:
var context = SqlContext.CreateFrom(SqlProvider.MySql, "Server=localhost;Database=4713_demo;Uid=root;Pwd=;");
context.Insert("my_table", parameters =>
{
parameters.Add(?); //It expects an object
});
My Problem is, dont want to do something like
context.Insert("my_table", parameters =>
{
parameters.Add(context.CreateParameter("foo","bar"));
});
I just want to pass the parametername and the parametervalue. the context-class itself is aware of its provider and should create the parameter. How to afford that?
The solution i came up with, is this SqlParameterizer-class.
public class SqlParameterizer
{
private SqlProvider Provider { get; set; }
private List<IDbDataParameter> ParameterList { get; set; }
public SqlParameterizer(SqlProvider sqlProvider)
{
this.Provider = sqlProvider;
this.ParameterList = new List<IDbDataParameter>();
}
public void Add(string parameterName, object parameterValue)
{
switch(this.Provider)
{
case SqlProvider.MsSql:
this.ParameterList.Add(new SqlParameter(parameterName, parameterValue));
break;
case SqlProvider.MySql:
this.ParameterList.Add(new MySqlParameter(parameterName, parameterValue));
break;
case SqlProvider.OracleSql:
throw new Exception($"SqlProvider '{this.Provider}' not supported yet...");
default:
throw new Exception($"Unknown SqlProvider '{this.Provider}'");
}
}
public IDbDataParameter[] GetParameters()
{
return ParameterList.ToArray();
}
}
Using this class will look like this:
var commandText = $"insert into {tableName} (#columns) values (#values)";
var valueBuilder = new StringBuilder();
var columnBuilder = new StringBuilder();
var parameterizer = new SqlParameterizer(this.Provider);
actionValueParameterizer?.Invoke(parameterizer);
foreach(IDbDataParameter parameter in parameterizer.GetParameters())
{
command.Parameters.Add(parameter);
valueBuilder.Append($",#{parameter.ParameterName}");
columnBuilder.Append($",{parameter.ParameterName}");
}
commandText = commandText.Replace("#values", valueBuilder.ToString().Substring(1));
commandText = commandText.Replace("#columns", columnBuilder.ToString().Substring(1));
command.CommandText = commandText;
command.ExecuteNonQuery();
Calling my insert-function:
context.Insert("some_table", parameterizer =>
{
parameterizer.Add("some_column", "some_value");
});

Continuous For Each Loop Will not stop inserting into SQL Server

I have a for each loop that will not stop inserting into sql server.
Here is my code:
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.Analytics.v3;
using Google.Apis.Analytics.v3.Data;
using Google.Apis.Services;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Auth.OAuth2;
using System.Data.SqlClient;
using System.Configuration;
namespace GooAnalyzer
{
class Program
{
static void RunSecondSite()
{
var serviceAccountEmail = "543652534806-7lk19u13619g9pthghqger31sth9jpr4#developer.gserviceaccount.com";
var certificate = new X509Certificate2(#"C:\Key\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
// Create the service.
//Twistandtango
var gas = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GooAnalyzer",
});
var r = gas.Data.Ga.Get("ga:1860066", "2014-12-27", "2015-01-10", "ga:totalValue");
//Specify some addition query parameters
r.Dimensions = "ga:source,ga:medium,ga:campaign,ga:transactionId,ga:date";
r.MaxResults = 100000000;
//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
foreach (var h in d.ColumnHeaders)
{
SqlConnection sqlCon = new SqlConnection("Data Source=UNDERGOD-PC\\SQLEXPRESS;Initial Catalog=GooAnalyzer;Integrated Security=True");
sqlCon.Open();
foreach (var row in d.Rows)
{
using (SqlCommand sqlCmd1 = new SqlCommand { CommandText = "INSERT INTO [GooData] ([Website], [gasource], [gamedium], [gacampaign], [gatransactionid], [gadate], [gatotalvalue], [Timeinserted]) VALUES (#Website, #gasource, #gamedium, #gacampaign, #gatransactionid, #gadate, #gatotalvalue, #Timeinserted)", Connection = sqlCon })
{
sqlCmd1.Parameters.AddWithValue("#Website", "4wheelparts.com");
sqlCmd1.Parameters.AddWithValue("#gasource", row[0]);
sqlCmd1.Parameters.AddWithValue("#gamedium", row[1]);
sqlCmd1.Parameters.AddWithValue("#gacampaign", row[2]);
sqlCmd1.Parameters.AddWithValue("#gatransactionid", row[3]);
sqlCmd1.Parameters.AddWithValue("#gadate", row[4]);
sqlCmd1.Parameters.AddWithValue("#gatotalvalue", row[5]);
sqlCmd1.Parameters.AddWithValue("#Timeinserted", DateTime.Now.ToString("MM-dd-yyyy"));
sqlCmd1.ExecuteNonQuery();
}
}
sqlCon.Close();
}
}
static void Main(string[] args)
{
var serviceAccountEmail = "543652534806-7lk19u13619g9pthghqger31sth9jpr4#developer.gserviceaccount.com";
var certificate = new X509Certificate2(#"C:\Key\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
// Create the service.
//Twistandtango
var gas = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GooAnalyzer",
});
var r = gas.Data.Ga.Get("ga:1381000", "2014-12-27", "2015-01-10", "ga:totalValue");
//Specify some addition query parameters
r.Dimensions = "ga:source,ga:medium,ga:campaign,ga:transactionId,ga:date";
r.MaxResults = 100000000;
//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
Console.WriteLine("Starting...");
foreach (var h in d.ColumnHeaders)
{
SqlConnection sqlCon = new SqlConnection("Data Source=UNDERGOD-PC\\SQLEXPRESS;Initial Catalog=GooAnalyzer;Integrated Security=True");
sqlCon.Open();
foreach (var row in d.Rows)
{
using (SqlCommand sqlCmd1 = new SqlCommand { CommandText = "INSERT INTO [GooData] ([Website], [gasource], [gamedium], [gacampaign], [gatransactionid], [gadate], [gatotalvalue], [Timeinserted]) VALUES (#Website, #gasource, #gamedium, #gacampaign, #gatransactionid, #gadate, #gatotalvalue, #Timeinserted)", Connection = sqlCon })
{
sqlCmd1.Parameters.AddWithValue("#Website", "4wd.com");
sqlCmd1.Parameters.AddWithValue("#gasource", row[0]);
sqlCmd1.Parameters.AddWithValue("#gamedium", row[1]);
sqlCmd1.Parameters.AddWithValue("#gacampaign", row[2]);
sqlCmd1.Parameters.AddWithValue("#gatransactionid", row[3]);
sqlCmd1.Parameters.AddWithValue("#gadate", row[4]);
sqlCmd1.Parameters.AddWithValue("#gatotalvalue", row[5]);
sqlCmd1.Parameters.AddWithValue("#Timeinserted", DateTime.Now.ToString("MM-dd-yyyy"));
sqlCmd1.ExecuteNonQuery();
}
}
sqlCon.Close();
RunSecondSite();
}
}
}
}
The code continues to loop when i run this:
RunSecondSite();
It will keep on inserting and I do not know how to stop it or where to place the correct breaks.
I want to call more methods of this code in the console application later using the same for each loop as well.
Please advise.
Your current logic is that you are adding each row to the database, each time you iterate over the column headers, resulting in identical rows being inserted.
From your code, it appears that "h" is never being used..
Simply remove:
foreach (var h in d.ColumnHeaders)
{
...
}
to become:
SqlConnection sqlCon = new SqlConnection("Data Source=UNDERGOD-PC\\SQLEXPRESS;Initial Catalog=GooAnalyzer;Integrated Security=True");
sqlCon.Open();
foreach (var row in d.Rows)
{
using (SqlCommand sqlCmd1 = new SqlCommand { CommandText = "INSERT INTO [GooData] ([Website], [gasource], [gamedium], [gacampaign], [gatransactionid], [gadate], [gatotalvalue], [Timeinserted]) VALUES (#Website, #gasource, #gamedium, #gacampaign, #gatransactionid, #gadate, #gatotalvalue, #Timeinserted)", Connection = sqlCon })
{
sqlCmd1.Parameters.AddWithValue("#Website", "4wheelparts.com");
sqlCmd1.Parameters.AddWithValue("#gasource", row[0]);
sqlCmd1.Parameters.AddWithValue("#gamedium", row[1]);
sqlCmd1.Parameters.AddWithValue("#gacampaign", row[2]);
sqlCmd1.Parameters.AddWithValue("#gatransactionid", row[3]);
sqlCmd1.Parameters.AddWithValue("#gadate", row[4]);
sqlCmd1.Parameters.AddWithValue("#gatotalvalue", row[5]);
sqlCmd1.Parameters.AddWithValue("#Timeinserted", DateTime.Now.ToString("MM-dd-yyyy"));
sqlCmd1.ExecuteNonQuery();
}
}
sqlCon.Close();
I think you simply need to replace
foreach (var h in d.ColumnHeaders)
{
SqlConnection sqlCon = ...
sqlCon.Open();
... // snip
sqlCon.Close();
}
with:
using(SqlConnection sqlCon = ...)
{
sqlCon.Open();
... // snip
sqlCon.Close();
}
Currently you are inserting all rows separately and identically for every column-header.

selecting a certain column value from looping in ienumerable

I have a result of IEnumerable from a stored procedure and i am looping through the results inorder to get the value of a column(GUID). I am unsure of how to go about on getting the Guid column from my results set in the foreach loop
this is what i have:
var results = GetGuids(instId);
foreach (var item in results)
{
}
public IEnumerable GetGuids(int id)
{
using (SqlCommand _command = new SqlCommand("StoredProc"))
{
_command.Connection = new SqlConnection(conString);
_command.Connection.Open();
_command.CommandType = CommandType.StoredProcedure;
_command.Parameters.AddWithValue("#ItemID", id);
return _command.ExecuteReader();
}
}
You can't use most of the normal linq extension methods directly on the non-generic IEnumerable... but you can call .Cast<T>() to make it an IEnumerable<T>. At that point, things get easier:
public IEnumerable<Guid> GetGuids(int id)
{
using (SqlCommand _command = new SqlCommand("StoredProc"))
{
_command.Connection = new SqlConnection(conString);
_command.Connection.Open();
_command.CommandType = CommandType.StoredProcedure;
_command.Parameters.Add("#ItemID", SqlDbType.Int).Value = id;
return _command.ExecuteReader()
.Cast<DbDataRecord>()
.Select(r => (Guid)r["GuidColumn"]);
}
}
You need to produce the results yourself from the SqlDataReader
var results = GetGuids(instId);
foreach (var item in results)
{
}
public IEnumerable<Guid> GetGuids(int id)
{
using (SqlCommand _command = new SqlCommand("StoredProc"))
{
_command.Connection = new SqlConnection(conString);
_command.Connection.Open();
_command.CommandType = CommandType.StoredProcedure;
_command.Parameters.AddWithValue("#ItemID", id);
var guids = new List<Guid>();
using (SqlDataReader reader = _command.ExecuteReader())
{
while (reader.Read()
{
guids.Add( (Guid)reader["GuidColumn"]);
}
}
}
}

Categories