【发布时间】:2011-06-15 14:25:07
【问题描述】:
目前我有以下代码:
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 和 Keypress 没有通过,因为它们发生在页面生命周期的后期。打开连接的最佳方法是什么,获取 2 个变量(当用户输入它们时),将它们传递给数据库,然后关闭连接。
我尝试过运行它_OnLoad。但这也不起作用。
非常感谢任何想法或建议。
【问题讨论】:
-
because they occur later in the page life cycle当所有数据都可用时,为什么不在页面生命周期的后期执行所有这些操作? -
我已经尝试过,通过在 OnLoad 部分添加所有这些,但没有运气。您建议在页面生命周期的哪个位置添加它?
标签: c# asp.net database-connection parameter-passing page-lifecycle