ASP.NET中使用数据处理插入数据注意的问题
发表时间:2024-01-25 来源:明辉站整理相关软件相关文章人气:
[摘要]很多时候,我们都会习惯将数据库连接的初始化过程交给Page_Load去做,其实这样子有好处也有坏处,好处是单边问题的时候,这种方法很实用,坏处就是遇到多边的问题时,就种情况这不太好用了!例如下面的例子:/// For Example:<script language="C#&quo...
很多时候,我们都会习惯将数据库连接的初始化过程交给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属性却没有问题,不知道是什么原因,知道的人给提个醒哦!谢谢 :)