使用ASP.NET+C#向SQL Server插入数据无响应,数据未插入求助
问题:ASP.NET点击Add按钮无响应,数据无法插入SQL Server表
我用ASP.NET搭建Bootstrap页面,通过C#后端代码往SQL Server的emp_table2表插入数据。点击Add按钮后页面无任何反应,数据也未成功插入表中,检查代码未发现错误,页面也无报错信息。第一次提问,希望能解决该问题。
后端C#代码
protected void addbtn_Click(object sender, EventArgs e) { String constr = WebConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString; SqlConnection con = new SqlConnection(constr); try { con.Open(); SqlCommand com = new SqlCommand("insert into emp_table2(firstName, lastName, age, pos, date) values(@firstName, @lastName, @age, @pos, date)", con); com.Parameters.AddWithValue("@firstName", txtfname.Text); com.Parameters.AddWithValue("@lastName", txtlname.Text); com.Parameters.AddWithValue("@age", txtage.Text); com.Parameters.AddWithValue("@pos", postxt.Text); com.Parameters.AddWithValue("@date", date.Text); com.ExecuteNonQuery(); con.Close(); Response.Write("<script>alert('Employee added successfully.')</script>"); } catch (Exception ex) { Response.Write(ex.ToString()); } }
前端HTML代码
<div class="col-md-6 offset-md-3"> <table class="table"> <tr> <th>First Name</th> <td><asp:TextBox class="form-control" ID="txtfname" runat="server"></asp:TextBox></td> </tr> <tr> <th>Last Name</th> <td> <asp:TextBox class="form-control" ID="txtlname" runat="server"></asp:TextBox></td> </tr> <tr> <th>age</th> <td><asp:TextBox class="form-control" ID="txtage" runat="server"></asp:TextBox></td> </tr> <tr> <th>position</th> <td><asp:TextBox class="form-control" ID="postxt" runat="server"></asp:TextBox></td> </tr> <tr> <th>Start-date</th> <td><asp:TextBox class="form-control" ID="date" runat="server"></asp:TextBox></td> </tr> <tr> <td> <asp:Button class="btn btn-success" ID="addbtn" runat="server" Text="Add" OnClick="addbtn_Click"/> </td> <td> <asp:Button class="btn btn-danger" ID="cancbtn" runat="server" Text="Cancel" /> </td> </tr> </table> </div>
解决方案
1. 修复SQL语句参数错误
你写的INSERT语句中,values部分的date未加@符号,数据库会将其识别为关键字而非参数,直接导致语法错误。修改后的SQL语句:
SqlCommand com = new SqlCommand("insert into emp_table2(firstName, lastName, age, pos, date) values(@firstName, @lastName, @age, @pos, @date)", con);
2. 规范资源释放逻辑
当前代码未用using包裹SqlConnection和SqlCommand,容易造成连接泄漏,甚至导致后续操作失败。改用using自动释放资源:
protected void addbtn_Click(object sender, EventArgs e) { String constr = WebConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString; using(SqlConnection con = new SqlConnection(constr)) { try { con.Open(); using(SqlCommand com = new SqlCommand("insert into emp_table2(firstName, lastName, age, pos, date) values(@firstName, @lastName, @age, @pos, @date)", con)) { com.Parameters.AddWithValue("@firstName", txtfname.Text); com.Parameters.AddWithValue("@lastName", txtlname.Text); // age字段为数值类型时,需转换为int避免类型不匹配 com.Parameters.AddWithValue("@age", int.Parse(txtage.Text)); com.Parameters.AddWithValue("@pos", postxt.Text); // date字段为日期类型时,转换为DateTime com.Parameters.AddWithValue("@date", DateTime.Parse(date.Text)); com.ExecuteNonQuery(); Response.Write("<script>alert('员工添加成功。')</script>"); } } catch (Exception ex) { // 改用弹窗显示错误,更易排查问题 Response.Write($"<script>alert('错误:{ex.Message}')</script>"); } } }
3. 解决数据类型不匹配问题
- 若
emp_table2表中age是int类型,直接传入字符串会导致类型错误,需转换为int; - 若
date是DateTime类型,需将输入字符串转为DateTime,建议添加前端日期格式验证,避免无效输入。
4. 验证连接字符串配置
确认Web.config中dbconnection连接字符串的服务器地址、用户名、密码正确,且该数据库用户拥有emp_table2表的插入权限。
内容的提问来源于stack exchange,提问作者naimish manzar
相关产品推荐
相关产品推荐

