[英]How to move customer information from ASP.NET database to your database?
我正在使用CreateUserWizard創建用戶,但是asp.net會自動將用戶添加到ASP.NET數據庫中,我想在數據庫和客戶表中添加用戶。 我已經在下面的同行中嘗試了這些代碼,但沒有任何反應
private MembershipUser _CurrentUser = null;
private MembershipUser CurrentUser
{
get
{
if (_CurrentUser == null)
{
_CurrentUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
}
return _CurrentUser;
}
}
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);
p.FirstName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
p.LastName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
p.ContactNumber = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ContactNumber")).Text;
// Save profile - must be done since we explicitly created it
p.Save();
}
請幫助我找出我的問題
您需要查看您的web.config文件,並為默認成員資格提供程序提供一個不同的連接字符串。 試試這個,但是用你的連接字符串。
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=myServer;Initial Catalog=myDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
請注意,您必須將所有成員資格和角色表從“ ASP.NET數據庫”復制到數據庫中,以進行登錄等操作,然后才能繼續工作。
您應該使用CreatedUser事件並調用將新客戶插入到customers表中的方法。
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
CreateCustomer(CreateUserWizard1.UserName, CreateUserWizard1.Email);
// Your code here
}
然后實現創建客戶的方法:
public void CreateCustomer(string userName, string email)
{
const string insertCustomerCommand = "INSERT INTO Customers (userName, email) VALUES (@userName, @email)";
var connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"];
var sqlConnection = new SqlConnection(connectionString.ToString());
var sqlCommand = new SqlCommand(insertCustomerCommand, sqlConnection);
sqlCommand.Parameters.Add("@userName", SqlDbType.NVarChar).Value = userName;
sqlCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = email;
// Eventually wrap in a try catch statement to handle any sql exceptions.
sqlCommand.ExecuteNonQuery();
}
按照您的代碼,應該是這樣的:
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
var customer = new Customer();
customer.InsertCustomer(CUSTOMER_ID, // Provide a unique customer Id
TextboxFirstName.Text, // Provider the control that holds the first name
TextboxLastName.Text, // Provider the control that holds the last name
TextboxContactNumber.Text, // Provider the control that holds the contact number
CreateUserWizard1.Email,
CreateUserWizard1.UserName);
}
您必須提供有效的客戶ID。 Sql Helper類中是否有任何方法可以生成此方法?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.