带有 sql 的动态网页

本文关键字:网页 动态 sql 带有 | 更新日期: 2023-09-27 18:34:39

我正在尝试使用 SQL 数据库构建一个具有更改控件的动态网页。我无法插入每个控件的 sql,因为它不会在 SQL 表上加起来,因为我还需要表和div。 基本上,我试图复制整个html并将其放在一行中,但这似乎不起作用,我无法将其作为纯文本取出。也许对此有一些建议?

我正在研究任何对象 SQL,因此我可以将整个表单作为字节或任何其他方式插入。C#上有OSQL库吗?(我想做这个服务器端(或者关于如何正确执行此操作的任何提示?

我试图将 HTML 代码插入 SQL 以备将来使用,但这给了我错误

    StringWriter sw = new StringWriter();
    HtmlTextWriter w = new HtmlTextWriter(sw);
    Form1.RenderControl(w);
    string s = sw.GetStringBuilder().ToString();
 string command = "INSERT INTO `htmltables`(`Company`, `Type`, `HTML`) VALUES  ('TestCompany','TestType','" + s + "')";

无论如何,我真的很想插入对象而不是纯 HTML,因为这将简化我将来的工作。

提前谢谢你。

带有 sql 的动态网页

尝试修复您的查询:

"INSERT INTO `htmltables`(`Company`, `Type`, `HTML`) VALUES 
('TestCompany','TestType','" + s + "')"
  1. 尝试从字符串中删除字符。

    "INSERT INTO htmltables(Company, Type, HTML) VALUES ('TestCompany','TestType','" + s + "')"

  2. 并尝试使用参数,例如:

    "INSERT INTO htmltables(Company, Type, HTML) VALUES (@0,@1,@2)", "The Company", "TestType", s

并提出查询。

对我来说,只有这个错误。

将div 添加到数据库

这不是问题,问题可能是服务器不允许这些角色认为他们可能是潜在的。为此,您可以尝试以下操作:

Request.Unvalidated["name"];

这样,服务器将获取要插入的div或任何其他 HTML 标记。

基本上,我

试图复制整个html并将其排成一行,但这似乎不起作用,我无法将其作为纯文本取出。

当你使用我建议的术语时,它应该有效,但是等等,你说它似乎不起作用,所以你怎么能得到文本?除非有一些数据,否则不会提供任何内容。你的问题有点令人困惑。对此感到抱歉。

参考:

http://technet.microsoft.com/en-us/library/aa214012(v=sql.80(.aspx

MSDN 将是你在这个问题上最好的伙伴! :)

您的 html 文本可能包含不正确的 SQL 字符。 最安全的方法是使用参数。

例如:

        string connectionString = "";
        string html = "<div id=''loremIpsum''><div/>";
        using (SqlCommand command = new SqlConnection(connectionString).CreateCommand())
        {
            command.CommandText = "insert into YourTable (YourColumn) values(@yourParameter)";
            command.Parameters.Add(new SqlParameter("yourParameter", html));
            try
            {
                command.Connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }

序列化你的类并将 xml 对象放在数据库中,下面是一个序列化类和函数的示例,用于在对象/xml 之间进行序列化:

VB.net

Imports System.Runtime.Serialization
Imports System.Xml
Imports System.IO
Imports System.Text
Namespace NamespaceGoesHere
    <DataContract()> _
    Public Class ClassNameGoesHere
        'All your properties go here, for example:
        <DataMember> Property PropertyName As String = "test"
        'Note the use of <DataMember> and <DataContract()>
        #Region "Serialisation"
            Public Function Serialise() As String
                Dim s As New System.IO.MemoryStream
                Dim x As New DataContractSerializer(GetType(ClassNameGoesHere))
                x.WriteObject(s, Me)
                s.Position = 0
                Dim sw As New StreamReader(s)
                Dim str As String
                str = sw.ReadToEnd()
                Return str
            End Function
            Public Shared Function DeSerialise(xmlDocument As String) As ClassNameGoesHere
                Dim doc As New XmlDocument
                Dim ser As New DataContractSerializer(GetType(ClassNameGoesHere))
                Dim stringWriter As New StringWriter()
                Dim xmlWriter As New XmlTextWriter(stringWriter)
                doc.LoadXml(xmlDocument)
                doc.WriteTo(xmlWriter)
                Dim stream As New MemoryStream(Encoding.UTF8.GetBytes(stringWriter.ToString()))
                stream.Position = 0
                Dim reader As XmlDictionaryReader = XmlDictionaryReader.CreateTextReader(stream, New XmlDictionaryReaderQuotas())
                Return DirectCast(ser.ReadObject(reader, True), ClassNameGoesHere)
            End Function
        #End Region
    End Class
End Namespace

C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Xml;
using System.IO;
using System.Text;
namespace NamespaceGoesHere
{
    [DataContract()]
    public class ClassNameGoesHere
    {
        //All your properties go here, for example:
        [DataMember()]
        public string PropertyName { get; set; }
        //Note the use of [DataMember()] and [DataContract()]
        #region "Serialisation"
        public string Serialise()
        {
            System.IO.MemoryStream s = new System.IO.MemoryStream();
            DataContractSerializer x = new DataContractSerializer(typeof(ClassNameGoesHere));
            x.WriteObject(s, this);
            s.Position = 0;
            StreamReader sw = new StreamReader(s);
            string str = null;
            str = sw.ReadToEnd();
            return str;
        }
        public static ClassNameGoesHere DeSerialise(string xmlDocument)
        {
            XmlDocument doc = new XmlDocument();
            DataContractSerializer ser = new DataContractSerializer(typeof(ClassNameGoesHere));
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
            doc.LoadXml(xmlDocument);
            doc.WriteTo(xmlWriter);
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(stringWriter.ToString()));
            stream.Position = 0;
            XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());
            return (ClassNameGoesHere)ser.ReadObject(reader, true);
        }
        #endregion
    }
}

创建类后,可以使用 serialise 函数将其序列化为 xml 字符串。将其存储在数据库中(我会使用 XML 数据类型,但您可以将其存储为 NVARCHAR(MAX(。从数据库返回时,只需调用传入字符串的反序列化函数,即可再次获取对象。