将行插入在ASP.NET数据库数据库、ASP、NET

2023-09-04 05:32:43 作者:假装不想要

我是新来的.NET,我似乎无法弄清楚的东西,应该是简单的。我想插入一行到表,并与未绑定到数据控件的值初始化。具体而言,我有一个页面上的CreateUserWizard控件。在CreatedUser方法,我想插入一行到一个DATABSE我创建了名为用户,其中包含的用户名和CreateUserWizard控件的电子邮件地址,然后输入新用户的创建日期,从DateTime.Now功能。目前似乎没有一种方法来设置参数SqlDataSource控件的方式,他们可以访问未绑定到数据控件的数据。谁能解释办法为我做到这一点?

I am new to .NET, and I'm can't seem to figure out something that should be simple. I want to insert a row into a table, and initialize it with values that aren't bound to data controls. Specifically, I have a CreateUserWizard control on a page. In the CreatedUser method, I want to insert a row into a databse I created called "users" that contains the Username and the Email address from the CreateUserWizard control, and the date the new user was created, from the DateTime.Now function. There doesn't seem to be a way to set the Parameters in the sqlDataSource control in a way that they can access data that is not bound to data controls. Can anyone explain a way for me to do this?

推荐答案

你是在错误的轨道上 - 你不会使用SqlDataSource控制的。你会去从code数据库中的更为直接的互动。例如:

You are on the wrong track - you won't use the sqlDataSource control at all. You'll go for a more direct interaction with the database from code. For example:

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmnd = new SqlCommand("Insert Into Table (P1, P2) Values (@P1, @P2)", conn);
cmnd.Parameters.AddWithValue("@P1", P1Value);
cmnd.Parameters.AddWithValue("@P2", P2Value);
cmnd.ExecuteNonQuery();
conn.Close();

现在,与所有这样说,你要学习的SqlCommand和SqlDataReader的一些细节(它们是pretty的直观),然后将注意力转移到构建一个数据访问层是对你有意义。你会很高兴,你没有。

Now, with that all being said, you'll want to learn the SqlCommand and SqlDataReader in some detail (they are pretty straightforward) and then turn your attention to building a Data Access Layer that makes sense to you. You'll be glad that you did.