我无法通过控制台应用程序读取数据并将数据发送到SQL Server

时间:2017-01-12 12:15:13

标签: c# sql-server console

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Data.SqlClient;
using System.Data;
using HttpWebRequestResponse;

namespace JSONARECHESTRA
{
    class Class1
    {
        static void Main()
        { }

        public SqlConnection con =newSqlConnection(@"server=SERVE;database=DATABASE;uid=XX;password=XXXX;MultipleActiveResultSets=True");
        public SqlCommand cmd;
        public SqlDataReader dr1;

        public void CONNECTION()
        {
            if (con.State == ConnectionState.Closed)
                con.Open();
        }

        public void executeNonQuery(string query)
        {
            CONNECTION();
            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
        }
    }

    public class user
    {
        public void Page_Load(object sender, EventArgs e)
        {
            var httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http:URL");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new System.IO.StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"Username\":\"DEV\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();

            using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                JObject jObject = JObject.Parse(result);
                response = jObject["Rdata"][0].ToArray();
                JArray array = (JArray)jObject["Rdata"];

                for (int i = 0; i <= 3; i++)
                {
                    int Alert_Type = Convert.ToInt32((string)array[i]["Alert_Type"]);
                    DateTime datetime = Convert.ToDateTime((string)array[i]["Date_time"]);
                    string Location = Convert.ToString((string)array[i]["Location"]);
                    string Vehicle = Convert.ToString((string)array[i]["Vehicle"]);
                    Console.WriteLine(Vehicle);
                    Console.ReadKey();

                    Class1 cs = new Class1();
                    cs.CONNECTION();

                    string insert = "INSERT INTO [DATABASE].[dbo].[TEST6] (datet) VALUES ('" + datetime + "');";
                    cs.executeNonQuery(insert);
                    // return Page_Load;
                }
            }
        }

        public string json { get; set; }

        public JToken[] response { get; set; }
    }
}

在这段代码中,我为数据库和Http Web服务创建了一个SqlConnection来获取JSON数据但是无法将数据转储到SQL Server中,并且我的控制台窗口也没有保持打开状态来逐行调试程序< / p>

2 个答案:

答案 0 :(得分:1)

您的Main()方法为空。它是console app,您应该使用Main()而不是Page_Load()事件。将Page_Load()中的所有代码放在Main()内并删除Page_Load()

此外,您应该创建所有字段,属性和方法static,因为您不创建任何实例。

更改此行:

public void Page_Load(object sender, EventArgs e)

由此:

public void Start() // or any other name

Main()应为:

static void Main()
{
    Class1 obj = new Class1();

    obj.Start();
}

修改

你的代码应该是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Data.SqlClient;
using System.Data;
using HttpWebRequestResponse;

namespace JSONARECHESTRA
{
    class Class1
    {
        public SqlConnection con = new SqlConnection(@"server=SERVE;database=DATABASE;uid=XX;password=XXXX;MultipleActiveResultSets=True");
        public SqlCommand cmd;
        public SqlDataReader dr1;

        public string json { get; set; }
        public JToken[] response { get; set; }


        static void Main()
        {
            Class1 obj = new Class1();
            obj.DoSomething();
        }

        public void CONNECTION()
        {
            if (con.State == ConnectionState.Closed)
                con.Open();
        }

        public void executeNonQuery(string query)
        {
            CONNECTION();
            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
        }

        public void DoSomething()
        {
            var httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http:URL");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new System.IO.StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"Username\":\"DEV\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();

            using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                JObject jObject = JObject.Parse(result);
                response = jObject["Rdata"][0].ToArray();
                JArray array = (JArray)jObject["Rdata"];

                for (int i = 0; i <= 3; i++)
                {
                    int Alert_Type = Convert.ToInt32((string)array[i]["Alert_Type"]);
                    DateTime datetime = Convert.ToDateTime((string)array[i]["Date_time"]);
                    string Location = Convert.ToString((string)array[i]["Location"]);
                    string Vehicle = Convert.ToString((string)array[i]["Vehicle"]);
                    Console.WriteLine(Vehicle);
                    Console.ReadKey();

                    this.CONNECTION();

                    string insert = "INSERT INTO [DATABASE].[dbo].[TEST6] (datet) VALUES ('" + datetime + "');";
                    this.executeNonQuery(insert);

                }
            }
        }
    }
}

答案 1 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Data.SqlClient;
using System.Data;
using HttpWebRequestResponse;

namespace JSONARECHESTRA
{
class Class1{


    static void Main()
    {
        Class1 obj = new Class1();

        obj.Start();
    }

    private void Start()
    {
        Console.WriteLine("It is Start() method");
    }




        public SqlConnection con = new SqlConnection(@"server=TNGSRV;database=astha;uid=sa;password=Admin@1234;MultipleActiveResultSets=True");
        public SqlCommand cmd;
        public SqlDataReader dr1 ;
        public void CONNECTION()
        {
            if (con.State == ConnectionState.Closed)
                con.Open();
        }

        public void executeNonQuery(string query)
        {
            CONNECTION();
            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
        }
    }
       public class user

       {
           private static JToken[] response;
           static void start()

    {
        var httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://182.18.161.47:99/vts.svc/geo");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";

        using (var streamWriter = new System.IO.StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"Username\":\"wave\"}";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            JObject jObject = JObject.Parse(result);
            response = jObject["Rdata"][0].ToArray();
            JArray array = (JArray)jObject["Rdata"];
            for (int i = 0; i <= 3; i++)
            {
                int Alert_Type = Convert.ToInt32((string)array[i]["Alert_Type"]);
                DateTime datetime = Convert.ToDateTime((string)array[i]["Date_time"]);
                string Location = Convert.ToString((string)array[i]["Location"]);
                string Vehicle = Convert.ToString((string)array[i]["Vehicle"]);
                Console.WriteLine(Vehicle);
                Console.ReadKey();
                Class1 cs = new Class1();
                cs.CONNECTION();
                string insert = "INSERT INTO [astha].[dbo].[TEST6] (datet) VALUES ('" + datetime + "');";
                cs.executeNonQuery(insert);
               //return Page_Load;
            }
        }
    }


    public string json { get; set; }





   }
}

我已经完成了这个但是得到同样的问题仍然数据不在sql以及控制台窗口立即关闭