在 IVR 系统的 apsx 页面中打开/关闭数据库连接

发布于 2024-11-15 19:15:38 字数 5440 浏览 9 评论 0原文

我目前正在开发一个 IVR 系统,我的问题更多是关于基本架构和打开/关闭数据库连接的开发方面。正如您从下面的代码中看到的,在 page_load 中我打开一个连接,传递变量,然后关闭连接。我的问题在于,变量不是在页面加载期间设置的,而是在调用进入时设置的,位于 Boolean ParseXML 部分。我需要知道在页面加载期间打开连接的最佳方式是什么,在收集变量后传递变量,然后最终关闭连接。最重要的是如何做到这一点,我尝试了几种不同的方法,但没有成功。

我最初的思考过程和方法是拆分数据库连接代码,并将它们放置在页面生命周期的不同部分。但我在准确放置它的位置方面取得了零成功。

布尔解析,写入文本文件。但我希望它也写入数据库。

<%@ 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>

I am currently developing an IVR system, my question is more on a development side of basic architecture and opening/closing a database connection. As you can see from the code below, in the page_load I am opening a connection, passing variables, and then closing the connection. My problem lies in the fact that the variables are not set during the page load, they are set as the call comes in, which is in the Boolean ParseXML section. I need to know what is the best way of opening the connection during page load, pass the variables after they are collected, and then finally close the connection. And most important how to do this, I have tried several different methods, with no success.

My initial thought process and approach is to split up the database connection code, and place them in different parts of the page life cycle. But I have had zero success with where to exactly put it.

The Boolean parse, writes to a text file. But I want it to also write to a database.

<%@ 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-11-22 19:15:38

我假设在调用 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>

以下链接可帮助您了解 ASP.NET 页面的生命周期

I am assuming that the SqlDataAdapter is being used after the ParseXml method is called. Try the following:

  • Declare a field for SqlConnection and SqlCommand.
  • Open the connection in Page_Load.
  • Set the command parameters in ParseXml.
  • Close the connection in 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>

Here is a link that will help you understand the life cycle of an ASP.NET page.

半城柳色半声笛 2024-11-22 19:15:38

我没有太多 IVR 经验,但以下是它在我工作的一个系统(使用 VXML)上的工作原理。

IVR 应答了呼叫。这导致 IVR 语音浏览器向 Web 服务器发出 HTTP 请求。

Web 服务器收到请求以及端口号,以识别唯一的呼叫者。就我而言,我们使用输出 VMXL(而不是 HTML 或 XHTML)的标准 ASPX 页面,因此所有处理都必须在 Page_Load 方法中完成。如果页面需要有关呼叫的其他信息(例如呼叫者号码),我们将向 IVR 发出 Web 服务呼叫,包括端口号。

所有与 IVR 的用户交互(按下按钮等)均在 IVR 上处理,并且 Web 服务器仅在请求不同的 VXML 文档时才会参与。

I don't have a lot of IVR experience, but here's how it worked on one system I worked on (which used VXML).

The call was answered by the IVR. This caused the IVR Voice Browser to issue an HTTP request to the web server.

The web server received the request, along with a port number, to identify the unique caller. In my case, we were using standard ASPX pages that output VMXL (rather than HTML or XHTML), so all processing had to be done in the Page_Load method. If the page needed additional information about the call, for example the callers number, we would issue a web services call to the IVR, including the port number.

All of the user interaction with the IVR - button presses, etc - were handled on the IVR, and the web server would only get involved when a different VXML document was requested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文