2011-06-16 55 views
0

我目前正在開發IVR系統,我的問題更多的是基本架構的開發方面以及打開/關閉數據庫連接。正如你可以從下面的代碼看到的,在page_load中,我打開一個連接,傳遞變量,然後關閉連接。我的問題在於變量沒有在頁面加載期間設置,它們被設置爲調用進來,這是在布爾ParseXML部分。我需要知道在頁面加載期間打開連接的最佳方式是什麼,在收集後傳遞變量,然後關閉連接。最重要的是如何做到這一點,我嘗試了幾種不同的方法,但都沒有成功。打開/關閉IVR系統的apsx頁面中的數據庫連接

我最初的思考過程和方法是拆分數據庫連接代碼,並將它們放置在頁面生命週期的不同部分。但是,我完全無法成功地把它放在哪裏。

布爾分析,寫入文本文件。但我希望它也寫入數據庫。

<%@ Page Language="C#" aspcompat="true" %> 
<%@ Import Namespace="System" %> 
<%@ Import Namespace="System.Net" %> 
<%@ Import Namespace="System.IO" %> 
<%@ Import Namespace="System.Collections" %> 
<%@ Import Namespace="System.Web" %> 
<%@ Import Namespace="System.Web.SessionState" %> 
<%@ Import Namespace="System.Web.UI" %> 
<%@ Import Namespace="System.Web.UI.WebControls" %> 
<%@ Import Namespace="System.Web.UI.HtmlControls" %> 
<%@ Import Namespace="System.Xml" %> 
<%@ Import Namespace="System.Text.RegularExpressions" %> 
<%@ Import Namespace="System.Data.SqlClient" %> 
<%@ Import Namespace="System.Data.OleDb" %> 
<%@ Import Namespace="System.Data" %> 



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


     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("summaryincall.txt")); 

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

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


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


       w.Close(); 
       return true; 
      } 
      catch (Exception e) 
      { 
       Response.Write(e.Message); 
       return false; 

      } 
     } 


     void Page_Load(object sender, System.EventArgs e) 
     { 

      string connectionString = "server=abc;database=abc;uid=abc;pwd=1234"; 
      SqlConnection mySqlConnection = new SqlConnection(connectionString); 
      string procedureString = "Call_Import"; 
      SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); 
      mySqlCommand.CommandText = procedureString; 
      mySqlCommand.CommandType = CommandType.StoredProcedure; 
      mySqlCommand.Parameters.Add("@CDate", SqlDbType.DateTime).Value = DateTime.Now; 
      mySqlCommand.Parameters.Add("@CTime", SqlDbType.DateTime).Value = DateTime.Now; 
      mySqlCommand.Parameters.Add("@ID", SqlDbType.Int).Value = keypress; 
      mySqlCommand.Parameters.Add("@CType", SqlDbType.Int).Value = CallID; 
      mySqlConnection.Open(); 
      mySqlCommand.ExecuteNonQuery(); 
      SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); 
      mySqlDataAdapter.SelectCommand = mySqlCommand; 
      mySqlConnection.Close(); 


      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

什麼是IVR和您的網頁之間的互動,以及您要找的參數來自哪裏? – chris 2011-06-16 19:22:48

+0

參數來自用戶的按鍵形式。 attrColl [「menuid」]。值;持續時間= attrColl [「持續時間」]。 CallID = attrColl [「callid」]。Value; CallerID = attrColl [「callerid」]。Value;是從API收集的,而attrColl [「keypress」] .value是字面用戶按鍵。 – FluxEngine 2011-06-16 19:46:50

+0

我不明白這是如何工作的,因爲你是從靜態文件中獲取的。當你同時接到兩個電話會發生什麼?是否沒有辦法通過GET/POST變量直接從IVR獲取信息,或者通過回調IVR的方式? – chris 2011-06-16 20:39:31

回答

0

我假設在調用ParseXml方法後使用了SqlDataAdapter。
請嘗試以下操作:

聲明SqlConnection和SqlCommand的字段。
在Page_Load中打開連接。
在ParseXml中設置命令參數。
關閉Page_Unload中的連接。

<script language="C#" runat="server"> 
    private SqlConnection mySqlConnection; 
    private SqlCommand mySqlCommand; 

    Boolean ParseXml(string XMLContent){ 

     // Do other work 

     mySqlCommand.Parameters["@CDate"].Value = DateTime.Now; 
     mySqlCommand.Parameters["@CTime"].Value = DateTime.Now; 
     mySqlCommand.Parameters["@ID"].Value = keypress; 
     mySqlCommand.Parameters["@CType"].Value = CallID; 

     // Do other work 

    } 

    void Page_Load(object sender, System.EventArgs e) 
    { 
     string connectionString = "server=abc;database=abc;uid=abc;pwd=1234"; 
     mySqlConnection = new SqlConnection(connectionString); 
     string procedureString = "Call_Import"; 
     mySqlCommand = mySqlConnection.CreateCommand(); 
     mySqlCommand.CommandText = procedureString; 
     mySqlCommand.CommandType = CommandType.StoredProcedure; 
     mySqlCommand.Parameters.Add(new SqlParameter("@CDate", SqlDbType.DateTime)); 
     mySqlCommand.Parameters.Add(new SqlParameter("@CTime", SqlDbType.DateTime)); 
     mySqlCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int)); 
     mySqlCommand.Parameters.Add(new SqlParameter("@CType", SqlDbType.Int)); 
     mySqlConnection.Open(); 
     SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); 
     mySqlDataAdapter.SelectCommand = mySqlCommand; 
    } 

    void Page_UnLoad(object sender, System.EventArgs e){ 
     if (mySqlConnection.State != ConnectionState.Closed) 
      mySqlConnection.Close(); 
    } 
</script> 

這是一個鏈接,可以幫助您瞭解life cycle of an ASP.NET page

+0

我試過這個,真的認爲它會起作用,但它沒有。即使在你的例子中,它在page_load中也有SqlParameters,但在生命週期的這一點上,這些變量沒有被設置。但是你的想法是在布爾中聲明它們,並添加Page_Unload方法是有道理的......但事實上,它不起作用。任何其他想法? – FluxEngine 2011-06-16 20:01:10

+0

我迷迷糊糊的是如何使用SqlDataAdapter。你可以嘗試調用mySqlCommand.ExecuteNonQuery()後立即給布爾參數賦值? – GemCer 2011-06-17 10:29:19

+0

我做了類似的事情,通過調用db並在布爾分析之後設置變量,並且它在一定程度上起作用。它傳遞固定變量,如menuid,callid和callerid。但讓它通過按鍵是行不通的。任何關於如何讓按鍵通過的想法? – FluxEngine 2011-06-17 12:02:06

0

我沒有很多的IVR經驗,但這是它在我工作的一個系統(使用VXML)時的工作方式。

呼叫由IVR回答。這導致IVR語音瀏覽器向Web服務器發出HTTP請求。

Web服務器接收到請求以及端口號以標識唯一的調用方。就我而言,我們使用標準的ASPX頁面輸出VMXL(而不是HTML或XHTML),因此所有處理都必須在Page_Load方法中完成。如果該頁面需要有關該呼叫的附加信息,例如呼叫者號碼,我們會向IVR發出Web服務呼叫,包括端口號碼。

所有用戶與IVR按鈕的交互等都在IVR上進行處理,並且只有在請求不同的VXML文檔時纔會涉及Web服務器。