从控制台应用程序将行插入SQL Server

时间:2015-01-11 03:54:38

标签: c# sql-server

我正在尝试从google analytics api中提取控制台应用程序中的行。

我的主要目标是提取数据并将数据插入到sql server表中。

以下是我目前用于通过控制台显示行和数据的代码:

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列表中的sql server中。

以下是生成这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 server运行插入查询?

会是这样吗????

        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();
        }
    }
}

我不知道该怎么做,这就是我在努力的地方。请指教,谢谢。

2 个答案:

答案 0 :(得分:2)

像这样......

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();
}

答案 1 :(得分:0)

由于您使用的是文本命令,为什么不使用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 }