读书人

参数化查询 amp;#x27;(@Name nvarchar(三

发布时间: 2013-01-11 11:57:35 作者: rapoo

参数化查询 '(@Name nvarchar(3)需要参数 '@TypeNum',但未提供该参数。
往sqlserver里面导入EXCEL数据

代码为:

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可以传递过去。

[解决办法]
你报错的这是哪里的sql,和你贴出来的不一致,你看看报错时的 cmdText是什么,估计你没添加参数

读书人网 >C#

热点推荐