明輝手游網(wǎng)中心:是一個免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

ASP.NET中使用數(shù)據(jù)處理插入數(shù)據(jù)注意的問題

[摘要]很多時候,我們都會習(xí)慣將數(shù)據(jù)庫連接的初始化過程交給Page_Load去做,其實這樣子有好處也有壞處,好處是單邊問題的時候,這種方法很實用,壞處就是遇到多邊的問題時,就種情況這不太好用了!例如下面的例子:/// For Example:<script language="C#&quo...

    很多時候,我們都會習(xí)慣將數(shù)據(jù)庫連接的初始化過程交給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);  //初始化命令調(diào)用
//實現(xiàn)配套
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();
}
//子函數(shù)調(diào)用
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>添加一個新的發(fā)行者:</h2>
<br/>
發(fā)行者 ID 應(yīng)以 99 打頭,并包含 4 位數(shù)字<br/>
發(fā)行者 ID:&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
<asp:textbox id="Pub_Id" runat="server" />姓名:&nbsp;&nbsp;
<asp:textbox id="Pub_Name" runat="server" />
城市:&nbsp;
<asp:textbox id="City" runat="server" />
<br/>
。&nbsp;
<asp:textbox id="State" runat="server" />
國家:&nbsp;
<asp:textbox id="Country" runat="server" />
<br/>
<br/>
&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:button Text="提交" OnClick="AddPublisher" runat="server" ID="Button1" /><br/>
<span id="Message" runat="server" />
<br/>
<asp:DataGrid id="dgMyGrid" runat="server" />

這樣的例子初初看起來沒有問題,調(diào)試也沒報錯,但在生成的頁面添加數(shù)據(jù)后提交時就會報錯,說什么屬性不配套之類的話。是什么原因造成的呢!其實,這就是初始化過程在頁面裝載時造成的,但這里有個問題我始終沒能搞清楚,就是既然是在頁面初始化過程已經(jīng)初始化過DB實例了,按道理來講應(yīng)該可以直接生成套用的啊,但好像沒有!還是要把初始化過程放到具體的函數(shù)里面才能實現(xiàn)!看下面:
<<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"; //構(gòu)造初始化過程
   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);   //初始化過程實現(xiàn)
   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()  子函數(shù)調(diào)用時同樣也要初始化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>添加一個新的發(fā)行者:</h2>
   <br>
   發(fā)行者 ID 應(yīng)以 99 打頭,并包含 4 位數(shù)字<br>
   發(fā)行者 ID:&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
   <asp:textbox id="Pub_Id" runat="server" />姓名:&nbsp;&nbsp;
   <asp:textbox id="Pub_Name" runat="server" />
   城市:&nbsp;
   <asp:textbox id="City" runat="server" />
   <br>
   。&nbsp;
   <asp:textbox id="State" runat="server" />
   國家:&nbsp;
   <asp:textbox id="Country" runat="server" />
   <br>
   <br>
   &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <asp:button Text="提交" OnClick="AddPublisher" runat="server" ID="Button1" /><br>
   <span id="Message" runat="server" />
   <br>
   <asp:DataGrid id="dgMyGrid" runat="server" />
  </form>>

經(jīng)過這樣修改后,我們才能在真正意義上實現(xiàn)數(shù)據(jù)的增加!但我也試了數(shù)據(jù)的刪除和更新,在頁面裝載時直接賦于其初始化DB屬性卻沒有問題,不知道是什么原因,知道的人給提個醒哦!謝謝 :)