本人在做一个添加管理员页面时出现:SQL插入错误: 列名或所提供值的数目与表定义不匹配.
前台代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="添加管理员.aspx.cs" Inherits="添加管理员" %>
<!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>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 438px;
height: 143px;
}
.style3
{
height: 68px;
}
.style4
{
width: 438px;
text-align: right;
}
.style5
{
height: 143px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="form">
<br />
<br />
<br />
<table align="center" cellspacing="5" class="style1">
<tr>
<td colspan="3" class="style3">
<div><h2 style="text-align: center">添加管理员</h2></div></td>
</tr>
<tr>
<td class="style4">
用户名:</td>
<td>
<asp:TextBox ID="name" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style4">
密码:</td>
<td>
<asp:TextBox ID="pwd" runat="server" TextMode="Password" ></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="req2" runat="server" ForeColor="red" ControlToValidate="name" ErrorMessage="(密码不能为空)"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
确认密码:</td>
<td>
<asp:TextBox ID="re_pwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td>
<asp:CompareValidator ID="comp1" runat="server" ControlToCompare="pwd" ControlToValidate="re_pwd" Display="Dynamic" ErrorMessage="两次密码输入不一样哦" Text="两次密码输入不一样哦" ForeColor="red"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ForeColor="red" ControlToValidate="re_pwd" ErrorMessage="(密码不能为空)"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
类别:</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>看客</asp:ListItem>
<asp:ListItem>游客</asp:ListItem>
<asp:ListItem>一般管理员</asp:ListItem>
<asp:ListItem>超级管理员</asp:ListItem>
</asp:DropDownList></td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style5">
<asp:Button ID="add" runat="server" Text="添加" onclick="add_Click" />
<input id="Reset1" type="reset" value="取消" /></td>
<td class="style5">
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
后台代码web.config代码:
<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="sql11" connectionString="Server=(local);Database=sql;integrated security=true;"
/>
<!--配置好connectionString节点uid=sa pwd=123;-->
</connectionStrings>
</configuration>
添加管理员页面后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration; //导入命名空间
using System.Data.SqlClient;//导入命名空间
using System.Data;
public partial class 添加管理员 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void add_Click(object sender, EventArgs e)
{
string web_config = ConfigurationManager.ConnectionStrings["sql11"].ConnectionString; //读取 web.config的连接字符串
using (SqlConnection conn11 = new SqlConnection(web_config)) //创建连接对象
{
conn11.Open();
// string cmd11 = "insert into admins values(@name,@pwd,@type,default)";
//原来的代码
string cmd11 = "insert into admins(aName,aPwd,aType,aLastLogin) values(@name,@pwd,@type,default)";
SqlCommand cmd = new SqlCommand(cmd11,conn11); //基于连接和T-sql语句创建命令对象
SqlParameter [] ps =
{
new SqlParameter("@name",name.Text),
new SqlParameter("@pwd",pwd.Text),
new SqlParameter("@type",DropDownList1.SelectedValue)
};
cmd.Parameters.AddRange(ps);
if (cmd.ExecuteNonQuery()>0)
Response.Write("添加成功");
else
Response.Write("添加失败");
}
}
}
数据库:
代码写完之后一切正常,运行添加管理员就出现SQL插入错误: 列名或所提供值的数目与表定义不匹配!
我查询了网上很多资料解决方法如下:
1.第一步
sql server设置ID字段为自增字段!!
- ①打开要设置的数据库表,点击要设置的字段,比如id,这时下方会出现id的列属性表
②列属性中,通过设置“标识规范”的属性可以设置字段自增,将“是标识”的值改为是,即成功设置id字段为自增字段
③注意! 能够设置ID字段自增的字段必须是可自增的,比如int,bigint类型,而varchar类型是不可自增的。
④设置“是标识”的值是“是”之后,可以看到下面的属性会默认写上值
⑤可以设置标识增量和标识种子。标识增量是字段每次自动增加的值,比如1,则字段每次增加1;标识种子是字段的初始值,比如1,则第一条记录的该字段值是1
⑥设置完成后,别忘记点击“保存(Ctrl + S)”
- 第二步
// string cmd11 = "insert into admins values(@name,@pwd,@type,default)";
//原来的代码
string cmd11 = "insert into admins(aName,aPwd,aType,aLastLogin) values(@name,@pwd,@type,default)";
//修改之后的代码
INSERT INTO 语句用于向表格中插入新的行。
1、语法:INSERT INTO 表名称 VALUES (值1, 值2,…)
这里我们需要指定所要插入数据的列:
INSERT INTO table_name (列1, 列2,…) VALUES (值1, 值2,…)
把代码修改成这样之后完美解决!
我们可以看到数据库代码插入成功!