2011-06-14 107 views
1

我目前有一個aspx頁面,它有一個通過電話線收集數據的腳本。該數據目前正在寫入一個文本文件,該文件通過DTS作業在晚上結束時泵入數據庫。我需要的是將aspx頁面的數據寫入數據庫。我遇到的部分問題是我不熟悉如何在aspx頁面僅包含腳本時寫入數據庫。我曾嘗試在腳本之前和之後打開連接,但沒有運氣。任何想法,建議或鏈接到文檔將不勝感激。以aspx連接到SQL數據庫,c#

這是我到目前爲止有:

<script language="C#" runat="server"> 

public class SQLSproc 
     { 
      public static void Main() 
      { 
       string connectionString = "server=ABC;database=abc;uid=abc;pwd=1234"; 
       SqlConnection mySqlConnection = new SqlConnection(connectionString); 
       string procedureString = "Callin_Insert"; 
       SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); 
       mySqlCommand.CommandText = procedureString; 
       mySqlCommand.CommandType = CommandType.StoredProcedure; 
       mySqlConnection.Open(); 
       mySqlCommand.ExecuteNonQuery(); 
       SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); 
       mySqlDataAdapter.SelectCommand = mySqlCommand; 
       mySqlConnection.Close(); 

      } 

     } 

Boolean ParseXML(string XMLContent) 
     { 
     try 
     { 
      XmlDocument doc = new XmlDocument(); 
      doc.LoadXml(XMLContent); 

      String MenuID, Duration, CallerID, CallID, DateAndTime, VoiceFileName; 
      XmlNode TempNode; 
      Byte[] VoiceFile; 

      XmlElement root = doc.DocumentElement; 
      XmlAttributeCollection attrColl = root.Attributes; 

      //parse inbound values 
      MenuID  = attrColl["menuid"].Value; 
      Duration = attrColl["duration"].Value; 
      CallID  = attrColl["callid"].Value; 
      CallerID = attrColl["callerid"].Value; 

      //writed parsed values to file 
      StreamWriter w = File.AppendText(Request.MapPath("summaryincallISM.txt")); 

      //w.Write(DateTime.Now.ToString("MM/dd/yyyy,HH:mm:ss")); 

      w.Write(String.Format("\"{0:MM/dd/yyyy}\",\"{0:HH:mm:ss}\"", DateTime.Now)); 

      //w.WriteLine("MenuId: " + MenuID); 
      //w.WriteLine("Duration: " + Duration); 
      //w.WriteLine("CallId: " + CallID); 
      //w.WriteLine("CallerId: " + CallerID); 

      XmlNodeList NodeCount = doc.SelectNodes("/campaign/prompts/prompt"); 
      foreach(XmlNode node in NodeCount) 
      { 
       attrColl = node.Attributes; 

       //w.WriteLine("Prompt ID: " + attrColl["promptid"].Value); 
       //w.WriteLine("Keypress : " + attrColl["keypress"].Value); 
       //w.Write("," + attrColl["keypress"].Value); 

       w.Write("," + "\"" + attrColl["keypress"].Value + "\""); 

       if (node.HasChildNodes) 
       { 
        TempNode = node.FirstChild; 
        attrColl = TempNode.Attributes; 

        //convert file to binary 
        VoiceFile = System.Convert.FromBase64String(TempNode.InnerText); 
        VoiceFileName = attrColl["filename"].Value; 

        //save file in application path 
        FileStream fs = new FileStream(Request.MapPath(VoiceFileName), FileMode.OpenOrCreate); 
        BinaryWriter bw = new BinaryWriter(fs); 
        bw.Write((byte[]) VoiceFile); 
        bw.Close(); 
        fs.Close(); 

        w.WriteLine("Filename : " + VoiceFileName); 
       } 
      } 

void Page_Load(object sender, System.EventArgs e) 
     { 
      try 
      { 
      String xmlcontent, PostResponse, campaign; 
      Byte[] Bindata = Request.BinaryRead(Request.TotalBytes); 

      string XML; 
      XML = System.Text.Encoding.ASCII.GetString(Bindata); 
      StreamWriter w = File.AppendText(Request.MapPath("xmlsummaryincall.txt")); 
      w.WriteLine("--- " + DateTime.Now + " ------------------------------------------------------"); 
      w.WriteLine(XML.Replace("<?xml version=\"1.0\"?>", "")); //needed so ?xml tag will display as text 
      w.WriteLine(""); 
      w.WriteLine("");   
      w.Close(); 

      if (!ParseXML(XML)) Response.Write("Failed"); 

      } 
      catch (Exception error) 
      { 
      Response.Write(error.Message); 
      } 
     } 

</script> 
+0

你看到什麼異常?什麼不起作用? – 2011-06-14 17:35:37

+0

@ agent -j將其寫入文本文件,但不直接寫入數據庫。其核心存儲過程「Callin_Insert」是一個簡化的,精簡的插入,它將字符串插入到數據庫中。 – FluxEngine 2011-06-14 17:41:03

+0

我認爲這是因爲你的public static void Main()沒有被調用。在ASP.NET中,它不能以這種方式工作。嘗試從Page_Load調用Main()。 – 2011-06-14 17:45:53

回答

1

由於ASP.NET代碼不使用標準的靜態無效的主要()入口點,你需要將代碼放在主別的地方,像Page_Load。