简体   繁体   English

如何将客户信息从ASP.NET数据库移动到数据库?

[英]How to move customer information from ASP.NET database to your database?

I'm using CreateUserWizard for creating an user but asp.net automatically add user to ASP.NET database I want to add user in my database and in my customer table. 我正在使用CreateUserWizard创建用户,但是asp.net会自动将用户添加到ASP.NET数据库中,我想在数据库和客户表中添加用户。 I have tried these code as peer below but nothing happened 我已经在下面的同行中尝试了这些代码,但没有任何反应

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();
    }

Please help me to figure out my problem 请帮助我找出我的问题

You need to look at your web.config file and provide a different connection string for the default membership provider. 您需要查看您的web.config文件,并为默认成员资格提供程序提供一个不同的连接字符串。 Try this, but with your connection string. 试试这个,但是用你的连接字符串。

<connectionStrings>
    <remove name="LocalSqlServer"/>
    <add name="LocalSqlServer" connectionString="Data Source=myServer;Initial Catalog=myDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

Note that you will have to copy the all membership and role tables from the "ASP.NET database" to your database for your login etc. to continue working. 请注意,您必须将所有成员资格和角色表从“ ASP.NET数据库”复制到数据库中,以进行登录等操作,然后才能继续工作。

You should use the CreatedUser event and call a method to insert a new customer to your customers table. 您应该使用CreatedUser事件并调用将新客户插入到customers表中的方法。

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    CreateCustomer(CreateUserWizard1.UserName, CreateUserWizard1.Email);
    // Your code here
}

And then implement a method to create the customer: 然后实现创建客户的方法:

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();
}

Following your code, it should be something like this: 按照您的代码,应该是这样的:

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);
}

You must provide a valid customer Id. 您必须提供有效的客户ID。 Do have any method in your Sql Helper class to generate this? Sql Helper类中是否有任何方法可以生成此方法?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将信息从数据库加载到 Asp.Net 中的标签? - How to load information from database to a Label in Asp.Net? 如何使用ASP.NET MVC将数据从一个数据库表移动到同一数据库的另一表? - How to move data from one database table to another table of same database using ASP.NET MVC? 将信息从ASP.NET网页插入数据库 - Inserting information from ASP.NET web page to Database 在 ASP.NET Core MVC 中通过控制器从数据库中读取信息 - Reading information from database by controller in ASP.NET Core MVC 使用ASP.NET成员资格提供程序数据库和您自己的数据库 - Using the ASP.NET membership provider database with your own database? 如何在 ASP.NET MVC 中记录鼠标悬停的工具提示中显示来自数据库字段的附加信息 - How to display additional information from a database field in tool tip on Mouse Over of record in ASP.NET MVC 如何仅从选定的文本框中更新数据库以获取选择性信息? (asp.net C#) - How to update database for selective information from selected textboxes only? (asp.net c#) ASP.Net 如何将与下拉列表值相关的信息从数据库显示到文本框中? - ASP.Net how can I display information related to dropdown list value into a textbox from database? 如何在asp.net c#中从SQL Server数据库表中移动下一条/上一条记录 - How to move next/previous record from SQL Server database table in asp.net c# 如何将LocalDb数据库移动到ASP.NET MVC托管服务器上的SQL Server数据库? - How to move LocalDb database to SQL Server database on the hosting server for ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM