页面生命周期,数据库连接是否进入Page_Load
目前我有以下代码:
void Page_Load(object sender, System.EventArgs e)
{
string connectionString = "server=abc;database=abc;uid=abc;pwd=1234";
SqlConnection mySqlConnection = new SqlConnection(connectionString);
string procedureString = "Callin_Insert";
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = procedureString;
mySqlCommand.CommandType = CommandType.StoredProcedure;
mySqlCommand.Parameters.Add("@LVDate", SqlDbType.DateTime).Value = DateTime.Now;
mySqlCommand.Parameters.Add("@LVTime", SqlDbType.DateTime).Value = DateTime.Now;
mySqlCommand.Parameters.Add("@CuID", SqlDbType.Int).Value = CustID;
mySqlCommand.Parameters.Add("@Type", SqlDbType.Int).Value = Keypress;
mySqlConnection.Open();
mySqlCommand.ExecuteNonQuery();
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
mySqlConnection.Close();
}
基本上,我在 page_load 期间打开与数据库的连接。我也在 page_load 中关闭该连接。我的部分问题是 CustID &按键不会被传递,因为它们发生在页面生命周期的后期。打开连接、获取两个变量(当我输入它们时由用户输入)、将它们传递到数据库并关闭连接的最佳方法是什么。
我尝试过运行它_OnLoad。但这也不起作用。
任何想法或建议,我们将不胜感激。
Currently I have The following code:
void Page_Load(object sender, System.EventArgs e)
{
string connectionString = "server=abc;database=abc;uid=abc;pwd=1234";
SqlConnection mySqlConnection = new SqlConnection(connectionString);
string procedureString = "Callin_Insert";
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = procedureString;
mySqlCommand.CommandType = CommandType.StoredProcedure;
mySqlCommand.Parameters.Add("@LVDate", SqlDbType.DateTime).Value = DateTime.Now;
mySqlCommand.Parameters.Add("@LVTime", SqlDbType.DateTime).Value = DateTime.Now;
mySqlCommand.Parameters.Add("@CuID", SqlDbType.Int).Value = CustID;
mySqlCommand.Parameters.Add("@Type", SqlDbType.Int).Value = Keypress;
mySqlConnection.Open();
mySqlCommand.ExecuteNonQuery();
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
mySqlConnection.Close();
}
Basically, I am opening a connection to the Database during the page_load. I am also closing that connection in page_load. Part of my problem is that the CustID & Keypress are not getting passed, because they occur later in the page life cycle. What is the best way to open the connection, get the 2 variables (when I they are entered by the user), pass them to the database, and close the connection.
Somethings I have tried is running it _OnLoad. But this didn't work either.
Any thoughts or suggestion, are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,SqlConnection 是 IDisposible 接口,这意味着使用这样的 using 语句包装代码要安全得多。
其次,我建议您使用 SqlDataSourceObject 控件,这将使处理像您这样的情况变得更加容易。
它会知道如何处理您尚未实现但应该实现的 Page.IsPostBack、分页和您需要的其他内容。
First of SqlConnection is of IDisposible interface means it is much safer to wrap your code with using statement like this.
Second of all i offer you to use SqlDataSourceObject control which will make much easier to work with cases like yours.
It will know how to deal with Page.IsPostBack that you haven't implemented but should, paging and other stuff you need.
认为这里存在一些问题......首先,为什么要运行查询然后将其传递给数据适配器?数据适配器将在查询时运行 select 命令。
我建议在您的 aspx 页面上创建一个 SqlDataSource(不是隐藏代码)并将您的控件绑定到它。然后连接选择事件并在其中填充您的参数。这应该在页面生命周期的后期发生,以便随后设置您的参数值。
Think there are a few problems here... first of all why are you running your query and then passing it to a data adapter? The data adapter will run the select command when queried.
I suggest creating a SqlDataSource on your aspx page (not code behind) and bind your control to it. Then hook up the Selecting event and in there populate your parameters. That should happen later in the page lifecycle so your parameter values will then be set.