我正在尝试建立一个使用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,因为这将使我将来的工作变得轻松。
提前谢谢你。
发布于 2013-10-10 19:23:27
尝试修复您的查询:
"INSERT INTO `htmltables`(`Company`, `Type`, `HTML`) VALUES
('TestCompany','TestType','" + s + "')""INSERT INTO htmltables(Company, Type, HTML) VALUES ('TestCompany','TestType','" + s + "')"
"INSERT INTO htmltables(Company, Type, HTML) VALUES (@0,@1,@2)", "The Company", "TestType", s
并进行查询。
对我来说,只有这个错误。
将div添加到数据库
这不是问题,问题可能是服务器不允许这样的角色认为他们可能是潜在的。要做到这一点,你可以试试这个:
Request.Unvalidated["name"];通过这种方式,服务器将获得要插入的div或任何其他HTML标记。
基本上,我试图复制整个
,并把它放在一行中,但似乎行不通,我也不能把它作为纯文本输出。
当你使用我建议的术语时,它应该是有效的,但是等等,你说它似乎不起作用,那么你如何获得文本呢?除非有一些数据,否则什么都不会提供。你的问题有点令人困惑。对此我很抱歉。
参考资料:
http://technet.microsoft.com/en-us/library/aa214012(v=sql.80).aspx
在这一期中,MSDN将是你最好的伙伴!:)
发布于 2013-10-10 19:34:36
您的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();
}
}发布于 2013-10-10 21:22:06
序列化您的类并将xml对象放入数据库中,以下是序列化的类和函数的示例,用于序列化object/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 NamespaceC#
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)。当从数据库返回时,只需调用反序列化函数,传入字符串,您将再次获得对象。
https://stackoverflow.com/questions/19291312
复制相似问题