[英]Passing parameter value (sometimes get null value) into stored procedure
我想将参数值有时传递给存储过程为空值,我的存储过程看起来像这样
USE [Stock]
GO
ALTER Proc [dbo].[Add_ItemQte]
@ID_Item int,
@ID_Supplier int,
@Qantity_ItemQte int,
@Date_ItemQte date,
@Lenght_ItemQte int,
@Widht_ItemQte int,
@WightT_ItemQte float,
@ID_Location int,
@ID_Project int=null
AS
INSERT INTO ItemQuantity
([ID_Item]
,[ID_Supplier]
,[Qantity_ItemQte]
,[Date_ItemQte]
,[Lenght_ItemQte]
,[Widht_ItemQte]
,[WightT_ItemQte]
,[ID_Location]
,[ID_Project])
VALUES
(@ID_Item
,@ID_Supplier
,@Qantity_ItemQte
,@Date_ItemQte
,@Lenght_ItemQte
,@Widht_ItemQte
,@WightT_ItemQte
,@ID_Location
,@ID_Project)
我的C#代码看起来像这样
public void Add_ItemQte(int Name, int Supplier, int Qantity, DateTime Date, int Lenght,int Widht,double WightT,int Location, Nullable<int> Project)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.Open();
SqlParameter[] param = new SqlParameter[9];
param[0] = new SqlParameter("@ID_Item", SqlDbType.Int);
param[0].Value = Name;
param[1] = new SqlParameter("@ID_Supplier", SqlDbType.Int);
param[1].Value = Supplier;
param[2] = new SqlParameter("@Qantity_ItemQte", SqlDbType.Int);
param[2].Value = Qantity;
param[3] = new SqlParameter("@Date_ItemQte", SqlDbType.Date);
param[3].Value = Date;
param[4] = new SqlParameter("@Lenght_ItemQte", SqlDbType.Int);
param[4].Value = Lenght;
param[5] = new SqlParameter("@Widht_ItemQte", SqlDbType.Int);
param[5].Value = Widht;
param[6] = new SqlParameter("@WightT_ItemQte", SqlDbType.Float);
param[6].Value = WightT;
param[7] = new SqlParameter("@ID_Location", SqlDbType.Int);
param[7].Value = Location;
param[8] = new SqlParameter("@ID_Project", SqlDbType.Int);
param[8].Value = Project;
DAL.ExcuteCommande("Add_ItemQte", param);
DAL.Close();
}
我用这段代码保存我的数据
for (int i=0;i<gridView1.RowCount;i++)
{
prd.Add_ItemQte(Convert.ToInt32(gridView1.GetRowCellValue(i, "IDNom")), Convert.ToInt32(gridView1.GetRowCellValue(i, "IDFournisseur")),
Convert.ToInt32(gridView1.GetRowCellValue(i, "Quantité")), Convert.ToDateTime(gridView1.GetRowCellValue(i, "Date")),
Convert.ToInt32(gridView1.GetRowCellValue(i, "Longueur")), Convert.ToInt32(gridView1.GetRowCellValue(i, "Largeur")),
Convert.ToDouble(gridView1.GetRowCellValue(i, "Poids Total")), Convert.ToInt32(gridView1.GetRowCellValue(i, "IDLocalisation")),
Convert.ToInt32(gridView1.GetRowCellValue(i, "IDProjet")));
}
当IDProjet列具有值时,代码运行时没有错误,但是当它不包含任何值时,我得到此错误:无法将对象从DBNull强制转换为其他类型。如果值为null,我不想保存0,因为我会这样做根据该列进行一些过滤,我如何解决此问题,在此先感谢我对之前的代码感到抱歉。
您不能将DBNull
转换为int
因此应使用以下表达式作为第二个参数:
txtDescription.Text == DBNull.Value ? txtDescription.Text : Convert.ToInt32(txtDescription.Text)
您的问题在这里:
param[1].Value = DBNull.Value.Equals(description)? 0 : description;
您不能将null值分配给整数数据类型。
或另一个选项将int更改为varchar:
Create Proc Add_Grade
@Name varchar (10),
@Description varchar (10)
如果txtDescription为null,则可以将其设置为“ 0”。 它将转换为0
prd.Add_Grade(txtName.Text, Convert.ToInt32(txtDescription?.Text ?? "0"));
首先更改循环,以从网格中读取每个项目的值
for (int i=0;i<gridView1.RowCount;i++)
{
object value = gridView1.GetRowCellValue(i, "IDProjet");
int? prjID = null;
if(value != null && Int32.TryParse(value.ToString(), out int temp))
prjID = temp;
prd.Add_ItemQte(...., prjID);
}
接下来,将参数的创建更改为更明确
param[8] = new SqlParameter("@ID_Project", SqlDbType.Int);
param[8].Value = Project.HasValue ? Project : (object)DbNull.Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.