我目前正在开发 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>