我正在將XML節點寫入數據庫列,但是我希望在將XML再次寫入表格之前從數據庫中刪除所有(*)。在將XML數據寫入表之前從表中刪除所有表格
現在,如果用戶使用userid'100'插入到數據庫中,代碼不會檢查模糊,並且我將在數據庫中使用超過1個userid'100's。我需要檢查列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>
你爲什麼打電話給頁面_Load(null,null)? Page_Load會自動觸發(這是頁面生命週期的一部分)。 – Tim
表是爲了在一段時間內保持數據,還是更多的臨時表?如果前者在從XML文檔加載數據之前刪除表中的數據,以避免重複的用戶id,則會導致我懶惰。 – Tim
以及蒂姆,你有解決方案來檢查「id」節點對'id'列嗎?如果表中存在的id僅更新a和v列,如果不存在,則將XML中的id,a和v數據插入表中? – Patriotec