从控制台应用程序将行插入 SQL 服务器
Insert Rows into SQL Server from Console Application
我正在尝试从从 google 分析 api 中提取的控制台应用程序插入行。
我的主要目标是提取数据并将数据插入 sql 服务器 table。
这是我当前用于通过控制台显示行和数据的代码:
using System;
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;
namespace GooAnalyzer
{
class Program
{
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 = 100000;
//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
foreach (var h in d.ColumnHeaders)
Console.WriteLine(h.Name);
foreach (var row in d.Rows)
Console.WriteLine(row[0] + " - " + row[1] + " - " + row[2] + " - " + row[3] + " - " + row[4] + " - " + row[5]);
Console.ReadLine();
}
}
}
我想要做的不是在控制台中显示数据,而是想将它插入一个 6 列 table 到 sql 服务器。
下面是生成这 6 列数据的代码:
foreach (var h in d.ColumnHeaders)
Console.WriteLine(h.Name);
foreach (var row in d.Rows)
Console.WriteLine(row[0] + " - " + row[1] + " - " + row[2] + " - " + row[3] + " - " + row[4] + " - " + row[5]);
Console.ReadLine();
我如何 运行 使用这些行向 sql 服务器插入查询?
会是这样吗????
using (SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString))
{
using (SqlCommand sqlCmd1 = new SqlCommand { CommandText = "INSERT INTO [GooData] ([gasource], [gamedium], [gacampaign], [gatransactionid], [gadate], [gatotalvalue]) VALUES (@gasource, @gamedium, @gacampaign, @gatransactionid, @gadate, @gatotalvalue)", Connection = sqlCon })
{
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]);
sqlCon.Open();
{
sqlCmd1.ExecuteNonQuery();
sqlCon.Close();
}
}
}
我不知道该怎么做,这就是我苦苦挣扎的地方。请指教,谢谢
像这样...
using (SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString))
{
sqlCon.Open();
foreach (var row in d.Rows)
{
using (SqlCommand sqlCmd1 = new SqlCommand { CommandText = "INSERT INTO [GooData] ([gasource], [gamedium], [gacampaign], [gatransactionid], [gadate], [gatotalvalue]) VALUES (@gasource, @gamedium, @gacampaign, @gatransactionid, @gadate, @gatotalvalue)", Connection = sqlCon })
{
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.ExecuteNonQuery();
}
}
sqlCon.Close();
}
既然你用的是文本命令,为什么不用string.format
像这样
SqlCommand sqlCmd1 = new SqlCommand { CommandText = string.format("INSERT INTO [GooData] ([gasource], [gamedium], [gacampaign], [gatransactionid], [gadate], [gatotalvalue]) VALUES ({0}, {1}, {2}, {3}, {4}, {5})", row[0], row[1], row[2], row[3], [row[4], row[5]), Connection = sqlCon }
我正在尝试从从 google 分析 api 中提取的控制台应用程序插入行。
我的主要目标是提取数据并将数据插入 sql 服务器 table。
这是我当前用于通过控制台显示行和数据的代码:
using System;
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;
namespace GooAnalyzer
{
class Program
{
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 = 100000;
//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
foreach (var h in d.ColumnHeaders)
Console.WriteLine(h.Name);
foreach (var row in d.Rows)
Console.WriteLine(row[0] + " - " + row[1] + " - " + row[2] + " - " + row[3] + " - " + row[4] + " - " + row[5]);
Console.ReadLine();
}
}
}
我想要做的不是在控制台中显示数据,而是想将它插入一个 6 列 table 到 sql 服务器。
下面是生成这 6 列数据的代码:
foreach (var h in d.ColumnHeaders)
Console.WriteLine(h.Name);
foreach (var row in d.Rows)
Console.WriteLine(row[0] + " - " + row[1] + " - " + row[2] + " - " + row[3] + " - " + row[4] + " - " + row[5]);
Console.ReadLine();
我如何 运行 使用这些行向 sql 服务器插入查询?
会是这样吗????
using (SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString))
{
using (SqlCommand sqlCmd1 = new SqlCommand { CommandText = "INSERT INTO [GooData] ([gasource], [gamedium], [gacampaign], [gatransactionid], [gadate], [gatotalvalue]) VALUES (@gasource, @gamedium, @gacampaign, @gatransactionid, @gadate, @gatotalvalue)", Connection = sqlCon })
{
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]);
sqlCon.Open();
{
sqlCmd1.ExecuteNonQuery();
sqlCon.Close();
}
}
}
我不知道该怎么做,这就是我苦苦挣扎的地方。请指教,谢谢
像这样...
using (SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString))
{
sqlCon.Open();
foreach (var row in d.Rows)
{
using (SqlCommand sqlCmd1 = new SqlCommand { CommandText = "INSERT INTO [GooData] ([gasource], [gamedium], [gacampaign], [gatransactionid], [gadate], [gatotalvalue]) VALUES (@gasource, @gamedium, @gacampaign, @gatransactionid, @gadate, @gatotalvalue)", Connection = sqlCon })
{
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.ExecuteNonQuery();
}
}
sqlCon.Close();
}
既然你用的是文本命令,为什么不用string.format
像这样
SqlCommand sqlCmd1 = new SqlCommand { CommandText = string.format("INSERT INTO [GooData] ([gasource], [gamedium], [gacampaign], [gatransactionid], [gadate], [gatotalvalue]) VALUES ({0}, {1}, {2}, {3}, {4}, {5})", row[0], row[1], row[2], row[3], [row[4], row[5]), Connection = sqlCon }