[英]Add html content to Microsoft SQL Server database asp net
I want to add html content into a SQL Server database. 我想将html内容添加到SQL Server数据库中。
For example, on a label it is shows location coordinates, and after button click it will be added in SQL Server. 例如,在标签上显示位置坐标,单击按钮后将其添加到SQL Server中。 I have gone so far but every time I click on the button it is added in the database empty, not coordinates. 我已经走了很远,但是每次我单击按钮时,它都会被添加到数据库中,而不是坐标。
I will give the source code... 我会给出源代码...
Default.aspx Default.aspx的
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="tripmeter" runat="server">
<p runat="server">
Starting Location (lat, lon):<br />
<span id="startLat" runat="server"></span>°, <span id="startLon" runat="server"></span>°
</p>
<p runat="server">
Current Location:<br />
<label id="currentLat" runat="server"></label>°, <span id="currentLon" runat="server"></span>°
</p>
<p runat="server">
Distance from starting location:<br />
<span id="distance" runat="server">0</span> km
</p>
<input id="Button1" type="button" value="button" runat="server" onserverclick="MySubmitHandler" />
</div>
<script>
window.onload = function () {
var startPos;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
startPos = position;
document.getElementById("startLat").innerHTML = startPos.coords.latitude;
document.getElementById("startLon").innerHTML = startPos.coords.longitude;
}, function (error) {
alert("Error occurred. Error code: " + error.code);
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from locaton provider)
// 3: timed out
});
navigator.geolocation.watchPosition(function (position) {
document.getElementById("currentLat").innerHTML = position.coords.latitude;
document.getElementById("currentLon").innerHTML = position.coords.longitude;
document.getElementById("distance").innerHTML =
calculateDistance(startPos.coords.latitude, startPos.coords.longitude,
position.coords.latitude, position.coords.longitude);
});
}
};
function calculateDistance(lat1, lon1, lat2, lon2) {
var R = 6371; // km
var dLat = (lat2 - lat1).toRad();
var dLon = (lon2 - lon1).toRad();
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
}
Number.prototype.toRad = function () {
return this * Math.PI / 180;
}
</script>
</form>
</body>
</html>
Default.aspx.cs Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void MySubmitHandler(object sender, EventArgs args)
{
try
{
SqlConnection lidhje = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
lidhje.Open();
string fut = "insert into Test (lat) values (@lat)";
SqlCommand komande = new SqlCommand(fut, lidhje);
komande.Parameters.AddWithValue("@lat", currentLat.InnerText);
komande.ExecuteNonQuery();
Response.Write("Success");
lidhje.Close();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
}
Set datatype Text in your database; 在数据库中设置数据类型为“文本”;
eg: 例如:
create table temp(
linkId int(10) PRIMARY KEY,
htmlValue Text(200) NULL); // this will store your data in HTML format.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.