我正在尝试使用参数化查询插入数据库。但它产生了一个例外:
列名或提供的值数与表不匹配 定义
CDCObj = new ClassDatabaseConnection();
CDCObj.openDatabaseConnection();
CDCObj.cmd_Obj = new SqlCommand("insert into TableCreateAccount values (@c_id,@c_fnm,@c_middlenm,@c_lnm,@c_addline1,@c_addline2,@c_city,@c_state,@c_off_add,@c_emailid,@c_telphone,@c_mono,@c_dob,@c_religion,@c_password,@c_age,@c_usertype,@c_fathernm,@c_mothernm)", CDCObj.con_Obj);
CDCObj.cmd_Obj.CommandType = CommandType.Text;
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_id", ACObj.c_id));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_fnm",ACObj.c_fnm));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_middlenm", ACObj.c_mnm));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_lnm", ACObj.c_lnm));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_addline1", ACObj.addline1));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_addline2", ACObj.addline2));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_city", ACObj.c_city));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_state", ACObj.c_state));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_off_add", ACObj.off_address));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_off_contact", ACObj.c_off_contact));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_emailid", ACObj.email_id));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_telphone", ACObj.tel_phoneno));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_mono", ACObj.mobile_no));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_dob", ACObj.d_o_b));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_gender", ACObj.c_gender));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_religion", ACObj.c_religion));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_age", ACObj.c_age));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_password", ACObj.c_password));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_fathernm", ACObj.father_nm));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_mothernm", ACObj.mother_nm));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_usertype",usertype));
int flag = CDCObj.cmd_Obj.ExecuteNonQuery();
if (flag > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('You have registered successfully')", true);
Response.Redirect("Login.aspx");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('Error occured! Please try to register again..')", true);
}
这是我的ClassDatabaseConnection
课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public class ClassDatabaseConnection
{
public SqlConnection con_Obj;
public SqlCommand cmd_Obj;
DataTable data_table_Obj;
SqlDataReader data_reader_Obj;
SqlDataReader data_adapter_Obj;
public SqlDataAdapter adp;
public DataSet ds;
public ClassDatabaseConnection()
{
con_Obj = new SqlConnection();
con_Obj.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\SEM-5-Final\Project_Civil\App_Data\dbCivilRegistry.mdf;Integrated Security=True";
}
public void openDatabaseConnection()
{
try
{
if(con_Obj.State==ConnectionState.Closed)
{
con_Obj.Open();
}
}
catch (Exception e)
{
Console.WriteLine("An error occurred: '{0}'", e);
}
}
public bool ToAuthenticate(SqlCommand param_Command)
{
Object count = param_Command.ExecuteScalar();
int flag = (int)count;
if(flag >= 1)
{
return true;
}
else
{
return false;
}
}
public int registered(SqlCommand param_Command)
{
Object count = param_Command.ExecuteScalar();
int flag = (int)count;
return flag;
}
public bool register_update(SqlCommand param_command)
{
int i = param_command.ExecuteNonQuery();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
public void getData()
{
cmd_Obj = new SqlCommand("select * from TableAppArrived", con_Obj);
cmd_Obj.CommandType = CommandType.Text;
adp = new SqlDataAdapter(cmd_Obj);
ds = new DataSet("Proj");
adp.Fill(ds, "app_Proj");
}
}
我的数据库设计:
CREATE TABLE [dbo].[TableCreateAccount] (
[CitizenId] VARCHAR (15) NOT NULL,
[First_Name] VARCHAR (15) NULL,
[Middle_Name] VARCHAR (10) NULL,
[Last_Name] VARCHAR (15) NULL,
[Fathername] VARCHAR (30) NULL,
[Mothername] VARCHAR (30) NULL,
[Residential_Addressline1] VARCHAR (25) NULL,
[Residential_Addressline2] VARCHAR (25) NULL,
[City] VARCHAR (20) NULL,
[State] VARCHAR (20) NULL,
[Office_Address] VARCHAR (60) NULL,
[Email_id] VARCHAR (15) NULL,
[Telephone_Contact] VARCHAR (10) NULL,
[Mobile_number] VARCHAR (10) NULL,
[Date_Of_Birth] VARCHAR (10) NULL,
[Gender] VARCHAR (6) NULL,
[Religion] VARCHAR (10) NULL,
[Password] VARCHAR (15) NULL,
[Age] INT NULL,
[Usertype] VARCHAR (7) NULL,
CONSTRAINT [PK_TableCreateAccount] PRIMARY KEY CLUSTERED ([CitizenId] ASC));
答案 0 :(得分:1)
您的表定义由20列组成,您尝试在Insert语句中传递19个参数。您没有在插入声明中声明Office Contact。
此外,您还尝试为Office contact
传递重复参数值。
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_off_contact", ACObj.c_off_contact));
CDCObj.cmd_Obj.Parameters.Add(new SqlParameter("@c_telphone", ACObj.tel_phoneno));
在Insert Statement中定义20个参数,仅声明20个参数值
答案 1 :(得分:0)
使用Values
而未在insert
语句中指定列名时,应根据列在数据库中的顺序插入。例如,@c_fathernm
应该出现在@c_lnm
列之后....所以它应该是这样的:
CDCObj.cmd_Obj = new SqlCommand("insert into TableCreateAccount values (@c_id,@c_fnm,@c_middlenm,@c_lnm,@c_fathernm,@c_mothernm,...)", CDCObj.con_Obj);