Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要IDispose方法来更新数据库_C#_Asp.net_Sql - Fatal编程技术网

C# 需要IDispose方法来更新数据库

C# 需要IDispose方法来更新数据库,c#,asp.net,sql,C#,Asp.net,Sql,我想我需要另一种方法来打开和关闭数据库连接,因为我得到了一个锁定IIS6的错误。谢谢你的帮助 根据我所读的内容,我需要IDispose方法从XML更新数据库 我收到以下错误消息: StackTrace:在 System.Data.ProviderBase.DbConnectionInternal.PrePush(对象 预期所有者) 位于System.Data.ProviderBase.DbConnectionPool.PutObject(DbConnectionInternal 对象,对象拥有对

我想我需要另一种方法来打开和关闭数据库连接,因为我得到了一个锁定IIS6的错误。谢谢你的帮助

根据我所读的内容,我需要
IDispose
方法从XML更新数据库

我收到以下错误消息:

StackTrace:在 System.Data.ProviderBase.DbConnectionInternal.PrePush(对象 预期所有者)
位于System.Data.ProviderBase.DbConnectionPool.PutObject(DbConnectionInternal 对象,对象拥有对象)
位于System.Data.ProviderBase.DbConnectionInternal.CloseConnection(DbConnection owningObject,DbConnectionFactory连接工厂)
在System.Data.SqlClient.SqlInternalConnection.CloseConnection(DbConnection)中 owningObject,DbConnectionFactory连接工厂)
在System.Data.SqlClient.SqlConnection.Close()上 在中的ASP.chat7\u handlers\u broadcasting\u handler\u aspx.XML.Finalize()处 g:\blah.handler.aspx:第73行

代码:


公共类XML
{
sqlconnectiondbconn;
公共XML()
{
dbConn=新的SqlConnection(ConfigurationManager.ConnectionString[“LocalSqlServer”].ToString());
dbConn.Open();
}
public void add2Db(字符串表、字符串sqlRows、字符串sqlValues)
{
string sql=“插入到“+table+”(“+sqlRows+”)值(“+sqlValues+”)中”;
SqlCommand cmd=新的SqlCommand(sql,dbConn);
cmd.CommandType=CommandType.Text;
cmd.ExecuteNonQuery();
}
公共空解析XML(XmlDocument文档)
{
字符串tsLogpro=“”;
字符串sqlRows=“”;
字符串sqlValues=“”;
//选择节点:logPro
foreach(文档SelectNodes(“logPro”)中的XmlNode logPro)
{
tsLogpro=logPro.SelectSingleNode(@ts”).InnerText;
//选择子节点:logPro
foreach(logPro.ChildNodes中的XmlNode子节点)
{
//选择所有子节点
foreach(logPro.SelectNodes(child.Name)中的XmlNode节点)
{
//吵架
foreach(child.Attributes中的XmlNode行)
{
sqlRows+=rows.Name+“,”;
}
foreach(节点中的XmlNode nodeChild)
{
sqlRows+=nodeChild.Name+“,”;
}
//获取价值
foreach(child.Attributes中的XmlNode值)
{
sqlValues+=“'”+values.InnerText+“',”;
}
foreach(节点中的XmlNode nodeChild)
{
sqlValues+=“'”+nodeChild.InnerText+“',”;
}
sqlRows=“action,”+sqlRows;
sqlRows=sqlRows.Substring(0,sqlRows.Length-2);
sqlValues=“”+child.Name+”,“+sqlValues;
sqlValues=sqlValues.Substring(0,sqlValues.Length-2);
//响应。写入(“\n\n”);
//Write(sqlRows);
//响应。写入(“\n”+sqlValues);
add2Db(“fchat7_logpro”,sqlRows,sqlValues);
}
sqlValues=“”;
sqlRows=“”;
}
}
}
~XML()
{
dbConn.Close();
}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
Response.Clear();
Response.ExpiresAbsolute=DateTime.Now;
AddHeader(“内容类型”、“文本/普通”);
HttpRequest请求=HttpContext.Current.request;
System.IO.Stream body=request.InputStream;
System.Text.Encoding Encoding=request.ContentEncoding;
System.IO.StreamReader reader=新的System.IO.StreamReader(正文,编码);
XmlDocument doc=新的XmlDocument();
字符串s=reader.ReadToEnd();
if(Request.Params[“action”]!=string.Empty&&Request.Params[“action”]=“test”)
{
doc.InnerXml=“”+
"" +
"" +
"" +
“客人”+
"" +
"" +
“管理员”+
"" +
"" +
“客人”+
"" +
"" +
"" +
"" +
"" +
“测试”+
"" +
"" +
“script.txt”+
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
}
如果(!string.IsNullOrEmpty)
{
doc.InnerXml=s;
}
XML-oXML=newxml();
parseXML(doc);
答复。填写(“完成”);
Response.End();
}
/*
* 
* 
将ANSI_空值设置为ON
去
在上设置带引号的\u标识符
去
创建表[dbo]。[chat7_logpro](
[log_id][int]标识(1,1)不为空,
[行动][nchar](40)无效,
[ip][nchar](40)无效,
[ts][nchar](40)空,
[uid][nchar](70)空,
[用户名][nchar](70)空,
[hasCam][nchar](40)空,
[id][nchar](40)空,
[错误][nchar](250)空,
[roomI
<%@ Page language="c#" %>
<%@ Import namespace="System.Net"%>
<%@ Import namespace="System.Data"%>
<%@ Import namespace="System.Data.SqlClient"%>
<%@ Import namespace="System.IO"%>
<%@ Import namespace="System.Xml"%>

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

    public class XML
    {
        SqlConnection dbConn;

        public XML()
        {
            dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
            dbConn.Open();
        }

        public void add2Db(string table, string sqlRows, string sqlValues)
        {
            string sql = "INSERT INTO " + table + " (" + sqlRows + ") VALUES (" + sqlValues + ")";
            SqlCommand cmd = new SqlCommand(sql, dbConn);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }

        public void parseXML(XmlDocument doc)
        {
            string tsLogpro = "";
            string sqlRows = "";
            string sqlValues = "";

            //SELECT NODE: logPro
            foreach (XmlNode logPro in doc.SelectNodes("logPro"))
            {
                tsLogpro = logPro.SelectSingleNode("@ts").InnerText;

                //SELECT CHILD NODE: logPro
                foreach (XmlNode child in logPro.ChildNodes)
                {
                    //SELECT ALL CHILD NODES
                    foreach (XmlNode node in logPro.SelectNodes(child.Name))
                    {
                        //GET ROWS
                        foreach (XmlNode rows in child.Attributes)
                        {
                            sqlRows += rows.Name + ", ";
                        }

                            foreach (XmlNode nodeChild in node)
                            {
                                sqlRows += nodeChild.Name + ", ";
                            }


                        //GET VALUES
                        foreach (XmlNode values in child.Attributes)
                        {
                            sqlValues += "'" + values.InnerText + "', ";
                        }

                            foreach (XmlNode nodeChild in node)
                            {
                                sqlValues += "'" + nodeChild.InnerText + "', ";
                            }

                        sqlRows = "action, " + sqlRows;
                        sqlRows = sqlRows.Substring(0, sqlRows.Length - 2);

                        sqlValues = "'" + child.Name + "', " + sqlValues;
                        sqlValues = sqlValues.Substring(0, sqlValues.Length - 2);

                        //Response.Write("\n\n");
                        //Response.Write(sqlRows);
                        //Response.Write("\n" + sqlValues);

                        add2Db("fchat7_logpro", sqlRows, sqlValues);

                    }

                    sqlValues = "";
                    sqlRows = "";
                }
            }
        }

        ~XML()
        {
            dbConn.Close();
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        Response.ExpiresAbsolute = DateTime.Now;
        Response.AddHeader("Content-type", "text/plain");

        HttpRequest request = HttpContext.Current.Request;
        System.IO.Stream body = request.InputStream;
        System.Text.Encoding encoding = request.ContentEncoding;
        System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
        XmlDocument doc = new XmlDocument();

        string s = reader.ReadToEnd();

        if (Request.Params["action"] != string.Empty && Request.Params["action"] == "test")
        {
            doc.InnerXml = "" +
                "<logPro ts=\"1283351509\">" +
                    "<autoLogin uid=\"d5b0ef4c8c51f303ecbaed81a6e078c5\" hasCam=\"false\" ip=\"127.0.0.1\" ts=\"1283351406\" />" +
                    "<chatLogin hasCam=\"false\" ip=\"127.0.0.1\" ts=\"1283352053\">" +
                        "<userName>Guest</userName>" +
                    "</chatLogin>" +
                    "<adminLogin hasCam=\"false\" ip=\"127.0.0.1\" ts=\"1283352053\">" +
                        "<userName>admin</userName>" +
                    "</adminLogin>" +
                    "<guestLogin hasCam=\"false\" ip=\"127.0.0.1\" ts=\"1283352020\">" +
                        "<userName>guest</userName>" +
                    "</guestLogin>" +
                    "<loginResult ip=\"127.0.0.1\" id=\"1\" error=\"\" ts=\"1283351406\" />" +
                    "<joinRoom id=\"1\" roomID=\"z11283351417236\" ts=\"1283351417\" />" +
                    "<leaveRoom id=\"1\" roomID=\"room1\" ts=\"1283351426\" />" +
                    "<createRoom id=\"1\" ts=\"1283351417\">" +
                        "<roomName>test</roomName>" +
                    "</createRoom>" +
                    "<sendFile id=\"2\" receiverID=\"1\" fileID=\"1283351441141\" fileSize=\"6Kb\" ts=\"1283351441\">" +
                        "<fileName>script.txt</fileName>" +
                    "</sendFile>" +
                    "<acceptFile id=\"1\" senderID=\"2\" isAccept=\"true\" fileID=\"1283351441141\" ts=\"1283351444\" />" +
                    "<changeColor id=\"1\" color=\"6684876\" ts=\"1283351406\" />" +
                    "<changeAvatar id=\"1\" avatarID=\"02\" ts=\"1283351406\" />" +
                    "<changeStatus id=\"2\" status=\"1\" ts=\"1283351383\" />" +
                    "<publish id=\"1\" video=\"false\" audio=\"true\" pause=\"false\" ts=\"1283351421\" />" +
                    "<subscribe id=\"1\" publisherID=\"2\" roomID=\"proom2000\" ts=\"1283351998\" />" +
                    "<unsubscribe id=\"1\" publisherID=\"2\" roomID=\"proom2000\" ts=\"1283351998\" />" +
                    "<sendInvitation id=\"1\" receiverID=\"2\" roomID=\"null\" ts=\"1283351494\" />" +
                    "<answerInvitation id=\"2\" senderID=\"1\" roomID=\"null\" accepted=\"true\" ts=\"1283351499\" />" +
                    "<startRecordVideo userID=\"1\" ts=\"1283351499\" />" +
                    "<videoSaved userID=\"1\" fileName=\"someFile.flv\" fileSize=\"1234121234\" ts=\"1283351499\" />" +
                "</logPro>";
        }
        else if (!string.IsNullOrEmpty(s))
        {

            doc.InnerXml = s;
        }

        XML oXML = new XML();
        oXML.parseXML(doc);

        Response.Write("Done");
        Response.End();

    }
         /*
          * 
          * 
        SET ANSI_NULLS ON
        GO
        SET QUOTED_IDENTIFIER ON
        GO
        CREATE TABLE [dbo].[chat7_logpro](
            [log_id] [int] IDENTITY(1,1) NOT NULL,
            [action] [nchar](40) NULL,
            [ip] [nchar](40) NULL,
            [ts] [nchar](40) NULL,
            [uid] [nchar](70) NULL,
            [userName] [nchar](70) NULL,
            [hasCam] [nchar](40) NULL,
            [id] [nchar](40) NULL,
            [error] [nchar](250) NULL,
            [roomID] [nchar](70) NULL,
            [roomName] [nchar](70) NULL,
            [receiverID] [nchar](70) NULL,
            [fileSize] [nchar](70) NULL,
            [fileName] [nchar](70) NULL,
            [senderID] [nchar](70) NULL,
            [isAccept] [nchar](70) NULL,
            [fileID] [nchar](70) NULL,
            [color] [nchar](70) NULL,
            [avatarID] [nchar](70) NULL,
            [status] [nchar](70) NULL,
            [video] [nchar](40) NULL,
            [audio] [nchar](40) NULL,
            [pause] [nchar](40) NULL,
            [publisherID] [nchar](40) NULL,
            [accepted] [nchar](40) NULL,
            [userID] [nchar](40) NULL
        ) ON [PRIMARY]
        GO
          * 
          */

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head id="Head1" runat="server">
    <title>blah blah</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server"><% Page_Load(null, null); %></form>
  </body>
</html>
string sql = "INSERT INTO " + ...;

using(SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["..."].ConnectionString)){
  //create your SqlCommand
  SqlCommand cmd = ...;

  //THEN open your database -- keep the connection open as short as possible
  dbConn.Open();

  //execute the query
  cmd.ExecuteNonQuery();
}
    public void add2Db(string table, string sqlRows, string sqlValues)
    {
        string sql = "INSERT INTO " + table + " (" + sqlRows + ") VALUES (" + sqlValues + ")";
        using(SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString())
        {
             SqlCommand cmd = new SqlCommand(sql, dbConn);
             cmd.CommandType = CommandType.Text;
             cmd.ExecuteNonQuery();
        }
    }