积极答复者
求教asp.net访问access数据库不能捕获异常!!!

问题
-
OleDbConnection conn = dbconn.conn(); OleDbCommand cmd = new OleDbCommand(); int check; if (this.RbN.Checked) check = 0; else check = 1; cmd.CommandText = "insert into Admins(AdmName,AdmPassword,NickName,IsClocked,CreateTime,RoleID) values('" + this.TbAdminname.Text.Trim() + "','" + this.TbPassword.Text.Trim() + "','" + this.TbNickName.Text.Trim() + "'," +check.ToString()+",'" + DateTime.Now.Date.ToShortDateString() + "'," + this.RolesDlist.SelectedValue + ")"; cmd.Connection = conn; try { conn.Open(); cmd.ExecuteNonQuery(); this.Lbtip.Visible = true; } catch (OleDbException err) { Response.Write("<script>alert('" + err.Message + "');</script>"); } finally { if (conn.State == ConnectionState.Open) conn.Close(); }
上面是我的代码,当我把"conn.open()"语句去掉后,按理说是会抛出异常而被"catch"语句捕获的,但是却不能捕获该异常,请问是哪里出错了呢?还有JS语句也没效.- 已移动 Sheng Jiang 蒋晟Moderator 2010年1月23日 19:22 System.Data (发件人:ASP.NET 与 AJAX)
答案
-
你好!具体的问题以及建议都在注释中,请看下面的示例。
/* * 1 建议使用 using * 2 与数据库交互建议使用参数的方式,避免注入。 * 3 你的问题可能是产生的异常并不是 OleDbException,增加 Exception 的 catch 试试。 */ try { using (OleDbConnection conn = dbconn.conn()) { OleDbCommand cmd = new OleDbCommand(); int check = (this.RbN.Checked) ? 0 : 1; cmd.CommandText = "insert into Admins(AdmName,AdmPassword,NickName,IsClocked,CreateTime,RoleID) values('" + this.TbAdminname.Text.Trim() + "','" + this.TbPassword.Text.Trim() + "','" + this.TbNickName.Text.Trim() + "'," + check.ToString() + ",'" + DateTime.Now.Date.ToShortDateString() + "'," + this.RolesDlist.SelectedValue + ")"; cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); this.Lbtip.Visible = true; } } catch (OleDbException ex1) { this.Lbtip.Text = String.Format("OleDbException:{0}", ex1.Message); this.Lbtip.Visible = true; } catch (Exception ex2) { this.Lbtip.Text = String.Format("Exception:{0}", ex1.Message); this.Lbtip.Visible = true; }
知识改变命运,奋斗成就人生!- 已标记为答案 谐音 2010年1月20日 18:26
全部回复
-
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data.OleDb; public partial class admins_addadmin : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsCallback) { this.Lbtip.Visible = false; DataTable dt = new DataTable(); OleDbDataAdapter oda = new OleDbDataAdapter("select * from Roles", dbconn.conn()); oda.Fill(dt); this.RolesDlist.DataSource = dt; this.RolesDlist.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { OleDbConnection conn = dbconn.conn(); OleDbCommand cmd = new OleDbCommand(); int check; if (this.RbN.Checked) check = 0; else check = 1; cmd.CommandText = "insert into Admins(AdmName,AdmPassword,NickName,IsClocked,CreateTime,RoleID) values('" + this.TbAdminname.Text.Trim() + "','" + this.TbPassword.Text.Trim() + "','" + this.TbNickName.Text.Trim() + "'," +check.ToString()+",'" + DateTime.Now.Date.ToShortDateString() + "'," + this.RolesDlist.SelectedValue + ")"; cmd.Connection = conn; try { conn.Open(); cmd.ExecuteNonQuery(); this.Lbtip.Visible = true; } catch (OleDbException err) { this.Lbtip.Text = "添加失败!"; this.Lbtip.Visible = true; } finally { if (conn.State == ConnectionState.Open) conn.Close(); } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="addadmin.aspx.cs" Inherits="admins_addadmin" %> <%@ Register src="guide.ascx" tagname="guide" tagprefix="uc1" %> <%@ Register assembly="Codefan.Controls" namespace="Codefan.Controls" tagprefix="Codefan" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> <link href="css/subweb.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <uc1:guide ID="guide1" runat="server" /> <div style="width:828px; border:solid 1px #5BAAED; margin:0px auto; padding:0px 0px 0px 0px;"> <div style="width:826px; height:21px; border-top:solid 1px white; font-size:14px; border-left:solid 1px white; border-right:solid 1px white; margin:0px auto; padding:5px 0px 0px 0px; background-color:#afcef4; text-align:center;"> 添加管理员<span style="color:Red;">(带“*”为必填项)</span></div> <table style="width:828px; border:solid 1px white; margin:0px auto; padding:0px 0px 0px 0px; border-collapse:collapse;" cellpadding="0" cellspacing="0" class="addadmin"> <tr><td style="width:250px; height:25px; text-align:right; background-color:#D3E4F9;"> 管理员名:</td><td style=" text-align:left; margin:0px 0px 0px 0px;"> <asp:TextBox ID="TbAdminname" runat="server" Width="130px"></asp:TextBox><span class="mustputin">*</span> </td></tr> <tr><td style="width:250px;height:25px; text-align:right; background-color:#D3E4F9;"> 登录密码:</td><td style="text-align:left; margin:0px 0px 0px 0px;"> <asp:TextBox ID="TbPassword" runat="server" TextMode="Password" Width="130px"></asp:TextBox><span class="mustputin">*</span> </td></tr> <tr><td style="width:250px; height:25px; text-align:right; background-color:#D3E4F9;"> 确认密码:</td><td style=" text-align:left; margin:0px 0px 0px 0px;"> <asp:TextBox ID="TbPassword2" runat="server" TextMode="Password" Width="130px"></asp:TextBox><span class="mustputin">*</span> </td></tr> <tr><td style="width:250px;height:25px; text-align:right; background-color:#D3E4F9;"> 角色设置:</td><td style="text-align:left; margin:0px 0px 0px 0px;"> <asp:DropDownList ID="RolesDlist" runat="server" Width="100px" DataMember="Roles" DataTextField="RoleName" DataValueField="RoleID"> </asp:DropDownList><span class="mustputin">*</span> </td></tr> <tr><td style="width:250px; height:25px; text-align:right; background-color:#D3E4F9;"> 是否锁定:</td><td style=" text-align:left; margin:0px 0px 0px 0px;"> <asp:RadioButton ID="RbN" runat="server" GroupName="islock" Text="否" Checked="True" /> <asp:RadioButton ID="RbY" runat="server" GroupName="islock" Text="是" /> </td></tr> <tr><td style="width:250px;height:25px; text-align:right; background-color:#D3E4F9;"> 昵称:</td><td style="text-align:left; margin:0px 0px 0px 0px;"> <asp:TextBox ID="TbNickName" runat="server" Width="130px"></asp:TextBox><span class="mustputin">*</span> </td></tr> </table> <table style="width:828px; height:30px; border:solid 1px white;"><tr><td style="width:50%; text-align:right; padding:8px 20px 0px 0px;"> <asp:Button ID="Button1" runat="server" Text="确认添加" onclick="Button1_Click" /> </td><td style="padding:8px 0px 0px 0px;"> <asp:Button ID="Button2" runat="server" Text="取消并返回" /> </td></tr></table> <table style="width:828px; height:30px; border:solid 1px white;"><tr><td style="width:100%; text-align:center; padding:8px 20px 0px 0px;"> <asp:Label ID="Lbtip" runat="server" Text="添加成功!" Font-Size="30px" ForeColor="Red"></asp:Label> </td></tr></table> </div> </form> </body> </html>
就这些代码. -
你好!具体的问题以及建议都在注释中,请看下面的示例。
/* * 1 建议使用 using * 2 与数据库交互建议使用参数的方式,避免注入。 * 3 你的问题可能是产生的异常并不是 OleDbException,增加 Exception 的 catch 试试。 */ try { using (OleDbConnection conn = dbconn.conn()) { OleDbCommand cmd = new OleDbCommand(); int check = (this.RbN.Checked) ? 0 : 1; cmd.CommandText = "insert into Admins(AdmName,AdmPassword,NickName,IsClocked,CreateTime,RoleID) values('" + this.TbAdminname.Text.Trim() + "','" + this.TbPassword.Text.Trim() + "','" + this.TbNickName.Text.Trim() + "'," + check.ToString() + ",'" + DateTime.Now.Date.ToShortDateString() + "'," + this.RolesDlist.SelectedValue + ")"; cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); this.Lbtip.Visible = true; } } catch (OleDbException ex1) { this.Lbtip.Text = String.Format("OleDbException:{0}", ex1.Message); this.Lbtip.Visible = true; } catch (Exception ex2) { this.Lbtip.Text = String.Format("Exception:{0}", ex1.Message); this.Lbtip.Visible = true; }
知识改变命运,奋斗成就人生!- 已标记为答案 谐音 2010年1月20日 18:26
-
string commandText = "SELECT * FROM [Users] WHERE [Name] = ? AND [Password] = ?"; OleDbCommand command = new OleDbCommand(queryString, connection); // 参数值的添加顺序要与 SQL 中一致 command.Parameters.AddWithValue("@p1", "X.X.Y"); command.Parameters.AddWithValue("@p2", "123456");
知识改变命运,奋斗成就人生!