我是Umbraco的新手。我正在尝试使用点网用户控件在umbraco中开发自定义注册页面。为此,我在umbraco数据库中创建了一个名为“registerTable”的自定义表。我只想使用Usercontrol将数据插入到该表中。 。连接字符串“CM_Connection”位于Webconfig文件中。
这是代码
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;
namespace thesis
{
public partial class test : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CM_Connection"].ConnectionString))
{
SqlCommand cmd = new SqlCommand();
Guid guid;
guid = Guid.NewGuid();
string sql = "INSERT INTO registerTable (Firstname) VALUES (@Name)";
cmd.Parameters.AddWithValue("@Name", TextBox1.Text.Trim() );
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
try
{
cmd.ExecuteNonQuery();
Label1.Text = "Registered successfully.";
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
}
答案 0 :(得分:2)
这将让你前进 - 这段代码是Umbraco术语中的MACRO。 IT只获取当前的数据库大小 - 但您可以根据需要更改查询。
@using Umbraco.Core
<div>
@{
var context = ApplicationContext.Current;
var databaseContext = context.DatabaseContext;
databaseContext.Database.OpenSharedConnection();
var db = ApplicationContext.Current.DatabaseContext.Database;
var sql = "SELECT (SUM(reserved_page_count) * 8192) FROM sys.dm_db_partition_stats";
var result = db.ExecuteScalar<int>(sql);
}
@if (databaseContext.Database.Connection.State == System.Data.ConnectionState.Open)
{
<span> Database is Open </span>
<span> Size: @result</span>
}
else
{
<span> Database is Closed</span>
}
基本上你所做的只是
你需要使用Umbraco.Core。