很多时候,我们都会习惯将数据库连接的初始化过程交给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属性却没有问题,不知道是什么原因,知道的人给提个醒哦!谢谢 🙂
|