在将XML数据写入表之前从表中删除所有内容

时间:2011-07-23 20:14:10

标签: c# asp.net sql xml sql-server-2005

我正在将XML的节点写入数据库列,但我希望在将XML再次写入表之前从数据库中删除所有(*)。

现在,如果将具有用户标识“100”的用户插入到数据库中,则代码不会检查欺骗,并且我将在数据库中拥有多于1个用户标识“100”。我需要检查列id(例如)是否与xml的id节点匹配。如果匹配,则更新v和节点,如果表中没有匹配,则将id,v和节点插入表中

我创建了一个函数private void deleteFromDb(string table),但在将XML数据插入表之前,我似乎无法调用它(如果我在第一时间正确地执行了它)。但这会消除所有数据,但最后一个访问该页面的人

<%@ Page Language="C#"%>
<%@ Import namespace="System.Net"%>
<%@ Import namespace="System.Data"%>
<%@ Import namespace="System.Data.SqlClient"%>
<%@ Import namespace="System.IO"%>
<%@ Import namespace="System.Xml"%>
<%@ Import Namespace="System" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

public class XML
{
    internal string connString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString();

    private void add2Db(string table, string sqlRows, string sqlValues)
    {
        string sql = String.Format("INSERT INTO {0} ({1}) VALUES ({2})", table, sqlRows, sqlValues);
        using (SqlConnection dbConn = new SqlConnection(connString))
        {
            using (SqlCommand cmd = new SqlCommand(sql, dbConn))
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }

    private void deleteFromDb(string table)
    {
        string sql = String.Format("DELETE *", table);
        using (SqlConnection dbConn = new SqlConnection(connString))
        {
            using (SqlCommand cmd = new SqlCommand(sql, dbConn))
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }

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

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

                //SELECT CHILD NODE: logPro
                foreach (XmlNode child in logPro.ChildNodes)
                {

                    //GET ROWS
                    foreach (XmlNode rows in child.Attributes)
                    {
                        sqlRows += rows.Name + ", ";
                    }

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

                    sqlRows = sqlRows.Substring(0, sqlRows.Length - 2);
                    sqlValues = sqlValues.Substring(0, sqlValues.Length - 2);

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

                    add2Db("flashcoms_chat7_broadcast", sqlRows, sqlValues);

                    sqlValues = "";
                    sqlRows = "";

                }
            }
        }
    }

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 = "" +
               "<broadcasting ts=\"12345\">" +
                    "<u id=\"1\" v=\"true\" a=\"true\" />" +
                    "<u id=\"2\" v=\"true\" a=\"true\" />" +
                    "<u id=\"3\" v=\"true\" a=\"false\" />" +
                    "<u id=\"4\" v=\"true\" a=\"true\" />" +
                    "<u id=\"5\" v=\"true\" a=\"true\" />" +
                "</broadcasting>";
        }
        else if (!string.IsNullOrEmpty(s))
    {
        doc.InnerXml = s;
     }

    // Starting at line 111 in your original post
    XML oXML = new XML();
    oXML.deleteFromDb("f_chat7_broadcast")
    oXML.parseXML(doc);
    Response.Write("Done");
    Response.End();
}
    /*
     * 
     * 
   SET ANSI_NULLS ON
   GO
   USE [DB]
   GO
   SET QUOTED_IDENTIFIER ON
   GO
   CREATE TABLE [dbo].[f_chat7_broadcast](
       [id] [nchar](100) NULL,
       [v] [nchar](10) NULL,
       [a] [nchar](10) 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="Form2" method="post" runat="server"><% Page_Load(null, null); %></form>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

string sql = String.Format("DELETE *", table);

应该是

string sql = String.Format("DELETE FROM {0}", table);

另一点: 据我所知,你没有设置任何异常处理(尝试/捕获...),这是一个不好的做法......