2017-01-12 84 views
1
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; } 
    } 
} 

將數據發送到SQL Server在此代碼創建一個SqlConnection到數據庫和HTTP Web服務來獲取JSON數據,但未能將數據轉儲到SQL服務器,也是我的控制檯窗口不會保持打開狀態以逐行調試程序我無法讀取,並通過控制檯應用程序

+0

爲了調試,你是否設置了一個斷點? –

+0

是的,我設置了斷點,但控制檯窗口立即關閉,不顯示我的輸出? –

回答

1

您的Main()方法爲空。它是console app,您應該使用Main()而不是Page_Load()事件。將Page_Load()中的所有代碼放在Main()之內並刪除Page_Load()

此外,您還應該製作所有字段,屬性和方法static,因爲您不會創建任何實例。

OR

改變這一行:

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

       } 
      } 
     } 
    } 
} 
+0

對不起,我無法得到它我已經改變了Page_Load(object sender,EventArgs e)public void Start(),但問題仍然是相同的數據不會在sql –

+0

@ R-T,你改變'Main'方法嗎? –

+0

是,這是程序'c:\ Users \ Administrator \ AppData \ Local \ Temporary Projects \ JSONARECHESTRA \ obj \ Debug \ JSONARECHESTRA.exe'不包含適用於入口點的靜態'Main'方法的錯誤 –

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;[email protected];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打算以及控制檯窗口立刻關閉

+0

你不用你的代碼調用方法。修改你的'Main': 'static void Main(){user.start(); }' –

+0

@羅馬可以請粘貼整個代碼與這些更改 –

+0

@羅馬感謝噸它工作我欣賞你的幫助 –

相關問題