我是 Umbraco 的新手。我正在尝试使用 dot net 用户控件在 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);
}
}
}
}
}