本帖最后由 Li_升杰 于 2014-10-20 22:02 编辑
正在看.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));
}
|
|