代码为:
public partial class formCustMgs : Form
{
public formCustMgs()
{
InitializeComponent();
}
private void btnImportFromExcel_Click(object sender, EventArgs e)
{
using (FileStream stream = File.OpenRead(@"c:\khzl.xls"))
{
HSSFWorkbook workbook = new HSSFWorkbook(stream);
HSSFSheet sheet = workbook.GetSheetAt(0);
for (int i = 1; i <= sheet.LastRowNum; i++)
{
T_customers cust = new T_customers();
HSSFRow row = sheet.GetRow(i);
HSSFCell cellName = row.GetCell(0);
cust.Name = cellName.StringCellValue;
HSSFCell telPhone = row.GetCell(1);
if (telPhone == null)
{
cust.TelPhone = "";
}
else if (telPhone.CellType == HSSFCell.CELL_TYPE_NUMERIC)
{
cust.TelPhone = row.GetCell(1).NumericCellValue.ToString();
}
else
{
cust.TelPhone = row.GetCell(1).StringCellValue;
}
HSSFCell mobilePhone = row.GetCell(2);
if (mobilePhone == null)
{
cust.MobilePhone = "";
}
else if (mobilePhone.CellType == HSSFCell.CELL_TYPE_NUMERIC)
{
cust.MobilePhone = row.GetCell(2).NumericCellValue.ToString();
}
else
{
cust.MobilePhone = row.GetCell(2).StringCellValue;
}
HSSFCell postCode=row.GetCell(3);
cust.PostCode = postCode.StringCellValue;
cust.Address = "";
cust.Email = "";
cust.Address = "";
HSSFCell dataTime = row.GetCell(6);
cust.BuyDate = dataTime.DateCellValue;
HSSFCell carNum=row.GetCell(7);
cust.CarNum = carNum.StringCellValue;
cust.BracktNum = row.GetCell(8).StringCellValue;
cust.Brand = "";
cust.Remarks = "";
cust.Suggestion = "";
new T_customersBLL().AddNew(cust);
}
}
MessageBox.Show("录入成功");
BLL层为:
partial class T_customersBLL
{
public int AddNew(T_customers model)
{
return new T_customersDAL().AddNew(model);
}
public bool Delete(int id)
{
return new T_customersDAL().Delete(id);
}
public bool Update(T_customers model)
{
return new T_customersDAL().Update(model);
}
public T_customers Get(int id)
{
return new T_customersDAL().Get(id);
}
public IEnumerable<T_customers> ListAll()
{
return new T_customersDAL().ListAll();
}
}
DAL层为:
public int AddNew(T_customers model)
{
string sql = "insert into T_customers (Name,TelPhone,MobilePhone,PostCode,Email,Address,BuyDate,CarNum,BracktNum,Brand,TypeNum,Suggestion,Remarks) output inserted.id values(@Name,@TelPhone,@MobilePhone,@PostCode,@Email,@Address,@BuyDate,@CarNum,@BracktNum,@Brand,@TypeNum,@Suggestion,@Remarks)";
int id = (int)SqlHelper.ExecuteScalar(sql, new SqlParameter("Name", model.Name) , new SqlParameter("TelPhone", model.TelPhone)
, new SqlParameter("MobilePhone", model.MobilePhone)
, new SqlParameter("PostCode", model.PostCode)
, new SqlParameter("Email", model.Email)
, new SqlParameter("Address", model.Address)
, new SqlParameter("BuyDate", model.BuyDate)
, new SqlParameter("CarNum", model.CarNum)
, new SqlParameter("BracktNum", model.BracktNum)
, new SqlParameter("Brand", model.Brand)
, new SqlParameter("TypeNum", model.TypeNum)
, new SqlParameter("Suggestion", model.Suggestion)
, new SqlParameter("Remarks", model.Remarks)
);
return id;
}
sqlHelper为:
public static object ExecuteScalar(string cmdText,
params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = cmdText;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteScalar();
}
}
}
问题:
程序读到return cmd.ExecuteScalar();报错。
报错代码:
参数化查询 '(@Name nvarchar(3),@TelPhone nvarchar(7),@MobilePhone nvarchar(4' 需要参数 '@TypeNum',但未提供该参数。
各位高手求帮帮我,弄了一下午,没搞定,不知道哪里错了。MODEL取到值了,通过BLL可以传递过去。
7 个解决方案
#1
你报错的这是哪里的sql,和你贴出来的不一致,你看看报错时的 cmdText是什么,估计你没添加参数
#2
我看看啊,cmdtext
为:
insert into T_customers (Name,TelPhone,MobilePhone,PostCode,Email,Address,BuyDate,CarNum,BracktNum,Brand,TypeNum,Suggestion,Remarks) output inserted.id values(@Name,@TelPhone,@MobilePhone,@PostCode,@Email,@Address,@BuyDate,@CarNum,@BracktNum,@Brand,@TypeNum,@Suggestion,@Remarks)
,不明白啊,菜鸟,折腾了一下午了。
为:
insert into T_customers (Name,TelPhone,MobilePhone,PostCode,Email,Address,BuyDate,CarNum,BracktNum,Brand,TypeNum,Suggestion,Remarks) output inserted.id values(@Name,@TelPhone,@MobilePhone,@PostCode,@Email,@Address,@BuyDate,@CarNum,@BracktNum,@Brand,@TypeNum,@Suggestion,@Remarks)
,不明白啊,菜鸟,折腾了一下午了。
#3
在线等啊。谢谢大家了
#4
new SqlParameter(" @Name", model.Name) , new SqlParameter(" @TelPhone", model.TelPhone).........
#5
谢谢,试了还是一样的错误。
#6
解决了,检查不仔细,数据库与excel表不一致的缘故,加上NULL值没有正确处理。
#7
请问楼主你是怎么处理NULL值的?
#1
你报错的这是哪里的sql,和你贴出来的不一致,你看看报错时的 cmdText是什么,估计你没添加参数
#2
我看看啊,cmdtext
为:
insert into T_customers (Name,TelPhone,MobilePhone,PostCode,Email,Address,BuyDate,CarNum,BracktNum,Brand,TypeNum,Suggestion,Remarks) output inserted.id values(@Name,@TelPhone,@MobilePhone,@PostCode,@Email,@Address,@BuyDate,@CarNum,@BracktNum,@Brand,@TypeNum,@Suggestion,@Remarks)
,不明白啊,菜鸟,折腾了一下午了。
为:
insert into T_customers (Name,TelPhone,MobilePhone,PostCode,Email,Address,BuyDate,CarNum,BracktNum,Brand,TypeNum,Suggestion,Remarks) output inserted.id values(@Name,@TelPhone,@MobilePhone,@PostCode,@Email,@Address,@BuyDate,@CarNum,@BracktNum,@Brand,@TypeNum,@Suggestion,@Remarks)
,不明白啊,菜鸟,折腾了一下午了。
#3
在线等啊。谢谢大家了
#4
new SqlParameter(" @Name", model.Name) , new SqlParameter(" @TelPhone", model.TelPhone).........
#5
谢谢,试了还是一样的错误。
#6
解决了,检查不仔细,数据库与excel表不一致的缘故,加上NULL值没有正确处理。
#7
请问楼主你是怎么处理NULL值的?