很多時候,我們都會習慣將資料庫連線的初始化過程交給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:
<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" />
</form>>
經過這樣修改後,我們才能在真正意義上實現資料的增加!但我也試了資料的刪除和更新,在頁面裝載時直接賦於其初始化DB屬性卻沒有問題,不知道是什麼原因,知道的人給提個醒哦!謝謝:)