很多時候,我們都會習慣將數據庫連接的初始化過程交給Page_Load去做,其實這樣子有好處也有壞處,好處是單邊問題的時候,這種方法很實用,壞處就是遇到多邊的問題時,就種情況這不太好用了!例如下面的例子:
/// For Example:
<script language="C#" runat="server">
SqlConnection MySQLCon;
protected void Page_Load(Object Src,EventArgs E)
{
MySQLCon=new SqlConnection("server=localhost;uid=sa;pwd=sa;database=pubs"); //初始化過程
if (!IsPostBack)
BindGrid();
}
public void AddPublisher(Object sender, EventArgs E)
{
String myinsertCmd = "insert into publishers ( pub_id, pub_name, city, state, country ) values (@pubid,@pubname,@city,@state,@country)";
SqlCommand mySqlCom = new SqlCommand(myinsertCmd, MySQLCon); //初始化命令調用
//實現配套
MySQLCom.Parameters.Add(new SqlParameter("@pubid", SqlDbType.Char, 4));
MySQLCom.Parameters["@pubid"].Value = Pub_Id.Text;
MySQLCom.Parameters.Add(new SqlParameter("@pubname", SqlDbType.VarChar, 40));
MySQLCom.Parameters["@pubname"].Value = Pub_Name.Text;
MySQLCom.Parameters.Add(new SqlParameter("@city", SqlDbType.VarChar, 20));
MySQLCom.Parameters["@city"].Value = City.Text;
MySQLCom.Parameters.Add(new SqlParameter("@state", SqlDbType.Char, 2));
MySQLCom.Parameters["@state"].Value = State.Text;
MySQLCom.Parameters.Add(new SqlParameter("@country",SqlDbType.VarChar, 30));
MySQLCom.Parameters["@country"].Value = Country.Text;
//打開DB
MySQLCom.Connection.Open();
MySQLCom.ExecuteNonQuery();
Message.InnerHtml = "<b>已添加記錄</b><br/>";
MySQLCom.Connection.Close();
Pub_Id.Text = "";
Pub_Name.Text = "";
City.Text= "";
State.Text = "";
Country.Text = "";
BindGrid();
}
//子函數調用
public void BindGrid()
{
SqlDataAdapter mySqlCom = new SqlDataAdapter("select * from publishers where pub_id like '99%'", MySQLCon);
DataSet myds = new DataSet();
MySQLCom.Fill(myds, "publishers");
dgMyGrid.DataSource = myds.Tables["publishers"].DefaultVIEw; dgMyGrid.DataBind();
}
</script>
<h2>添加一個新的發行者:</h2>
<br/>
發行者 ID 應以 99 打頭,並包含 4 位數字<br/>
發行者 ID:
<ASP:textbox id="Pub_Id" runat="server" />姓名:
<ASP:textbox id="Pub_Name" runat="server" />
城市:
<ASP:textbox id="City" runat="server" />
<br/>
省:
<ASP:textbox id="State" runat="server" />
國家:
<ASP:textbox id="Country" runat="server" />
<br/>
<br/>
<ASP:button Text="提交" OnClick="AddPublisher" runat="server" ID="Button1" /><br/>
<span id="Message" runat="server" />
<br/>
<ASP:DataGrid id="dgMyGrid" runat="server" />
這樣的例子初初看起來沒有問題,調試也沒報錯,但在生成的頁面添加數據後提交時就會報錯,說什麼屬性不配套之類的話。是什麼原因造成的呢!其實,這就是初始化過程在頁面裝載時造成的,但這裡有個問題我始終沒能搞清楚,就是既然是在頁面初始化過程已經初始化過DB實例了,按道理來講應該可以直接生成套用的啊,但好像沒有!還是要把初始化過程放到具體的函數裡面才能實現!看下面:
<<script language="C#" runat="server">
protected void Page_Load(Object Src,EventArgs E)
{
//頁面裝載過程中直接使用IF語句,其實什麼也不加!
if (!IsPostBack)
BindGrid();
}
public void AddPublisher(Object sender, EventArgs E)
{
string strprovider="server=localhost;uid=sa;pwd=sa;database=pubs"; //構造初始化過程
SqlConnection MySQLCon=new SqlConnection(strprovider);
string myinsertCmd ="insert into publishers ( pub_id, pub_name, city, state, country ) values (@pubid,@pubname,@city,@state,@country)";
SqlCommand mySqlCom = new SqlCommand(myinsertCmd,MySQLCon); //初始化過程實現
MySQLCom.Parameters.Add(new SqlParameter("@pubid", SqlDbType.Char, 4));
MySQLCom.Parameters["@pubid"].Value = Pub_Id.Text;
MySQLCom.Parameters.Add(new SqlParameter("@pubname", SqlDbType.VarChar, 40));
MySQLCom.Parameters["@pubname"].Value = Pub_Name.Text;
MySQLCom.Parameters.Add(new SqlParameter("@city", SqlDbType.VarChar, 20));
MySQLCom.Parameters["@city"].Value = City.Text;
MySQLCom.Parameters.Add(new SqlParameter("@state", SqlDbType.Char, 2));
MySQLCom.Parameters["@state"].Value = State.Text;
MySQLCom.Parameters.Add(new SqlParameter("@country",SqlDbType.VarChar, 30));
MySQLCom.Parameters["@country"].Value = Country.Text;
MySQLCom.Connection.Open();
MySQLCom.ExecuteNonQuery();
Message.InnerHtml = "<b>已添加記錄</b><br>";
MySQLCom.Connection.Close();
Pub_Id.Text = "";
Pub_Name.Text = "";
City.Text= "";
State.Text = "";
Country.Text = "";
BindGrid();
}
public void BindGrid() 子函數調用時同樣也要初始化DB連接
{
string strprovider="server=dev;uid=sa;pwd=pauperboyboy;database=pubs";
SqlConnection MySQLCon=new SqlConnection(strprovider);
SqlDataAdapter mySqlCom = new SqlDataAdapter("select * from publishers where pub_id like '99%'",MySQLCon);
DataSet myds = new DataSet();
MySQLCom.Fill(myds, "publishers");
dgMyGrid.DataSource = myds.Tables["publishers"].DefaultVIEw;
dgMyGrid.DataBind();
}
</script>
<h2>添加一個新的發行者:</h2>
<br>
發行者 ID 應以 99 打頭,並包含 4 位數字<br>
發行者 ID: