当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET中使用数据处理插入数据注意的问题

ASP.NET
不同映射模式下的直线输出的效果问题
ASP.NET开发下的MVC设计模式的实现
ASP.NET编写应用程序的十大技巧
ASP.NET中使用AJAX的简单方法
ASP.NET MVC实现自己的视图引擎
认识asp.net会话状态
ASP.NET实现页面传值的几种方法
.NET中容易混淆的几组重要概念
详解.NET中的动态编译技术
如何使用ASP.Net加密Cookie
ASP.NET 2.0跨网页提交的三种方法
ASP.NET 2.0创建母版页引来的麻烦
.Net整合其他平台的一些探讨
ASP.NET编程经验技巧10则
最佳实践 ADO.NET实用经验无保留曝光
在.NET上执行多线程操作要考虑的两大因素
.Net开发 细说Visual Basic.Net
ASP.NET网络编程中经常用到的27个函数集
ASP.NET防止用户多次登录的方法
对ASP.NET MVC项目中的视图做单元测试

ASP.NET中使用数据处理插入数据注意的问题


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 52 ::
收藏到网摘: n/a

很多时候,我们都会习惯将数据库连接的初始化过程交给Page_Load去做,其实这样子有好处也有坏处,好处是单边问题的时候,这种方法很实用,坏处就是遇到多边的问题时,就种情况这不太好用了!例如下面的例子:


///ForExample:
<scriptlanguage="C#"runat="server">
SqlConnectionmySqlCon;
protectedvoidPage_Load(ObjectSrc,EventArgsE)
{
mySqlCon=newSqlConnection("server=localhost;uid=sa;pwd=sa;database=pubs");//初始化过程
if(!IsPostBack)
BindGrid();
}
publicvoidAddPublisher(Objectsender,EventArgsE)
{
StringmyinsertCmd="insertintopublishers(pub_id,pub_name,city,state,country)values(@pubid,@pubname,@city,@state,@country)";
SqlCommandmySqlCom=newSqlCommand(myinsertCmd,mySqlCon);//初始化命令调用
//实现配套
mySqlCom.Parameters.Add(newSqlParameter("@pubid",SqlDbType.Char,4));
mySqlCom.Parameters["@pubid"].Value=Pub_Id.Text;
mySqlCom.Parameters.Add(newSqlParameter("@pubname",SqlDbType.VarChar,40));
mySqlCom.Parameters["@pubname"].Value=Pub_Name.Text;
mySqlCom.Parameters.Add(newSqlParameter("@city",SqlDbType.VarChar,20));
mySqlCom.Parameters["@city"].Value=City.Text;
mySqlCom.Parameters.Add(newSqlParameter("@state",SqlDbType.Char,2));
mySqlCom.Parameters["@state"].Value=State.Text;
mySqlCom.Parameters.Add(newSqlParameter("@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();
}
//子函数调用
publicvoidBindGrid()
{
SqlDataAdaptermySqlCom=newSqlDataAdapter("select*frompublisherswherepub_idlike'99%'",mySqlCon);
DataSetmyds=newDataSet();
mySqlCom.Fill(myds,"publishers");
dgMyGrid.DataSource=myds.Tables["publishers"].DefaultView;dgMyGrid.DataBind();
}
</script>
<h2>添加一个新的发行者:</h2>
<br/>
发行者ID应以99打头,并包含4位数字<br/>
发行者ID:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:textboxid="Pub_Id"runat="server"/>姓名:&nbsp;&nbsp;
<asp:textboxid="Pub_Name"runat="server"/>
城市:&nbsp;
<asp:textboxid="City"runat="server"/>
<br/>
省:&nbsp;
<asp:textboxid="State"runat="server"/>
国家:&nbsp;
<asp:textboxid="Country"runat="server"/>
<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:buttonText="提交"OnClick="AddPublisher"runat="server"ID="Button1"/><br/>
<spanid="Message"runat="server"/>
<br/>
<asp:DataGridid="dgMyGrid"runat="server"/>

这样的例子初初看起来没有问题,调试也没报错,但在生成的页面添加数据后提交时就会报错,说什么属性不配套之类的话。是什么原因造成的呢!其实,这就是初始化过程在页面装载时造成的,但这里有个问题我始终没能搞清楚,就是既然是在页面初始化过程已经初始化过DB实例了,按道理来讲应该可以直接生成套用的啊,但好像没有!还是要把初始化过程放到具体的函数里面才能实现!看下面:


<<scriptlanguage="C#"runat="server">
protectedvoidPage_Load(ObjectSrc,EventArgsE)
{
//页面装载过程中直接使用IF语句,其实什么也不加!
if(!IsPostBack)
BindGrid();
}
publicvoidAddPublisher(Objectsender,EventArgsE)
{
stringstrprovider="server=localhost;uid=sa;pwd=sa;database=pubs";//构造初始化过程
SqlConnectionmySqlCon=newSqlConnection(strprovider);
stringmyinsertCmd="insertintopublishers(pub_id,pub_name,city,state,country)values(@pubid,@pubname,@city,@state,@country)";
SqlCommandmySqlCom=newSqlCommand(myinsertCmd,mySqlCon);//初始化过程实现
mySqlCom.Parameters.Add(newSqlParameter("@pubid",SqlDbType.Char,4));
mySqlCom.Parameters["@pubid"].Value=Pub_Id.Text;
mySqlCom.Parameters.Add(newSqlParameter("@pubname",SqlDbType.VarChar,40));
mySqlCom.Parameters["@pubname"].Value=Pub_Name.Text;
mySqlCom.Parameters.Add(newSqlParameter("@city",SqlDbType.VarChar,20));
mySqlCom.Parameters["@city"].Value=City.Text;
mySqlCom.Parameters.Add(newSqlParameter("@state",SqlDbType.Char,2));
mySqlCom.Parameters["@state"].Value=State.Text;
mySqlCom.Parameters.Add(newSqlParameter("@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();
}
publicvoidBindGrid()子函数调用时同样也要初始化DB连接
{
stringstrprovider="server=dev;uid=sa;pwd=pauperboyboy;database=pubs";
SqlConnectionmySqlCon=newSqlConnection(strprovider);
SqlDataAdaptermySqlCom=newSqlDataAdapter("select*frompublisherswherepub_idlike'99%'",mySqlCon);
DataSetmyds=newDataSet();
mySqlCom.Fill(myds,"publishers");
dgMyGrid.DataSource=myds.Tables["publishers"].DefaultView;
dgMyGrid.DataBind();
}
</script>
<h2>添加一个新的发行者:</h2>
<br>
发行者ID应以99打头,并包含4位数字<br>
发行者ID:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:textboxid="Pub_Id"runat="server"/>姓名:&nbsp;&nbsp;
<asp:textboxid="Pub_Name"runat="server"/>
城市:&nbsp;
<asp:textboxid="City"runat="server"/>
<br>
省:&nbsp;
<asp:textboxid="State"runat="server"/>
国家:&nbsp;
<asp:textboxid="Country"runat="server"/>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:buttonText="提交"OnClick="AddPublisher"runat="server"ID="Button1"/><br>
<spanid="Message"runat="server"/>
<br>
<asp:DataGridid="dgMyGrid"runat="server"/>
</form>>

经过这样修改后,我们才能在真正意义上实现数据的增加!