积极答复者
新人问题求解答。

问题
-
正在看.net的ADO.Net的项目视频,遇到这个问题!!!!
调用 Insert() 方法时在 ExecuteNonQuery() 方法中的 cmd.ExecuteNonQuery(); 语句出错了。
错误代码:
变量名 '@Number' 已声明。变量名在查询批次或存储过程内部必须唯一。
必须声明标量变量 "@DivisionId"。
代码:
public static object ExecuteNonQuery(string sql,params SqlParameter[] parameter)
{
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using(SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameter);
return cmd.ExecuteNonQuery();
}
}
}
public void Insert(Employee employee)
{
SqlHelper.ExecuteNonQuery(@"Insert into T_Employee(Id,Number,Name,BirthDay,InDate,MarriageId
,PartyStatusId,Nationality,NativeAddr,EducationId,Major,School,Address,BaseSalary,Email
,IdNum,TelNum,EmergencyContact,DivisionId,Position,ContractStartDay
,ContractEndDay,Resume,Remarls,IsDeleted,GenderId) VALUES
(newid(),@Number, @Name, @BirthDay, @InDate, @MarriageId, @PartyStatusId, @Nationality, @NativeAddr, @EducationId, @Major, @School
, @Address, @BaseSalary, @Email, @IdNum,@TelNum, @EmergencyContact, @DivisionId, @Position, @ContractStartDay
, @ContractEndDay, @Resume, @Remarls, 0, @GenderId)",
new SqlParameter("@Number",employee.Number),
new SqlParameter("@Name",employee.Name),
new SqlParameter("@BirthDay",employee.BirthDay),
new SqlParameter("@InDate",employee.InDate),
new SqlParameter("@MarriageId",employee.MarriageId),
new SqlParameter("@PartyStatusId",employee.PartyStatusId),
new SqlParameter("@Nationality",employee.Nationality),
new SqlParameter("@NativeAddr",employee.NativeAddr),
new SqlParameter("@EducationId",employee.EducationId),
new SqlParameter("@Major",employee.Major),
new SqlParameter("@School",employee.School),
new SqlParameter("@Address",employee.Address),
new SqlParameter("@BaseSalary",employee.BaseSalary),
new SqlParameter("@Email",employee.Email),
new SqlParameter("@IdNum",employee.IdNum),
new SqlParameter("@TelNum",employee.TelNum),
new SqlParameter("@EmergencyContact",employee.EmergencyContact), new SqlParameter("@Number",employee.Number),
new SqlParameter("@DivisionId",employee.DivisionId),
new SqlParameter("@Position",employee.Position),
new SqlParameter("@ContractStartDay",employee.ContractStartDay),
new SqlParameter("@ContractEndDay",employee.ContractEndDay),
new SqlParameter("@Resume",employee.Resume),
new SqlParameter("@Remarls",employee.Remarls),
new SqlParameter("@GenderId",employee.GenderId));
}
答案
-
@Number变量重复了,删掉第二个应该就没有问题了
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 信念的力量 2014年10月20日 13:48
全部回复
-
-
你是否用过事务?
从代码看来,没有问题。
我怀疑你没有立即执行提交命令,导致下一次循环到此命令的时候参数仍然存在。
【尝试解决方法】
public static int ExecuteNonQuery(string sql,params SqlParameter[] parameter)
{
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
int result = 0;
using(SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameter);
result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
return result;
}ASP.NET Forum
Other Discussion Forums
FreeRice Donate
Issues to report
Free Tech Books Search and Download -
@Number变量重复了,删掉第二个应该就没有问题了
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 信念的力量 2014年10月20日 13:48