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

ASP.NET
十天学会ASP.net之第二天
十天学会ASP.net之第四天
十天学会ASP.net之第五天
十天学会ASP.net之第六天
十天学会ASP.net之第七天
十天学会ASP.net之第八天
十天学会ASP.net之第九天
十天学会ASP.net之第十天
在.net中Oracle日期类型的处理
ASP.Net的6大焦点问题
关于Web站点不同,共享Session的问题
判断浏览器是否接受Cookies
DataGrid的多行提交
C#中连接两个DataTable,相当于Sql的InnerJoin
ASP.Net常用功能整理--生成图片的缩略图
在程序中书写SQL语句
正则表达式的3种匹配模式
ASP.NET的高级调试技巧
基于C#的接口基础教程之七
ASP.NET对IIS中的虚拟目录进行操作

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 35 ::
收藏到网摘: 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>>

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