【问题标题】:Allow Null values in parameterised SQL query?在参数化 SQL 查询中允许 Null 值?
【发布时间】:2013-04-29 19:21:51
【问题描述】:

我有以下 SQL 查询,它根据参数将记录插入数据库。它运行良好但不允许空值,如果有空值则返回错误。

该表已设置为允许 Null。

SqlConnection dbconnection = new SqlConnection(@"Data Source=LEWIS;Initial Catalog=CustomPC;Integrated Security=True");


SqlCommand InsertPC = new SqlCommand("INSERT INTO [Custom_PC] ([CPU_ID], [Motherboard_ID],[Graphics_Card_ID],[PSU_ID],[RAM_ID],[Case_ID],[Primary_Storage_Drive_ID],[Secondary_Storage_Drive_ID],[Primary_Optical_Drive_ID],[Secondary_Optical_Drive_ID]) VALUES (@CPU_ID, @Motherboard_ID, @Graphics_Card_ID, @PSU_ID, @RAM_ID, @Case_ID, @Primary_Storage_Drive_ID, @Secondary_Storage_Drive_ID, @Primary_Optical_Drive_ID, @Secondary_Optical_Drive_ID) SET @ReturnedID = Scope_Identity();", dbconnection);

SqlParameter CPU_IDParam = InsertPC.Parameters.Add("@CPU_ID", SqlDbType.Int);
CPU_IDParam.Value = Session["DATA"];

SqlParameter Motherboard_IDParam = InsertPC.Parameters.Add("@Motherboard_ID", SqlDbType.Int);
Motherboard_IDParam.Value = Session["MotherboardID"];

SqlParameter Graphics_Card_IDParam = InsertPC.Parameters.Add("@Graphics_Card_ID", SqlDbType.Int);
Graphics_Card_IDParam.Value = Session["GraphicsID"];

SqlParameter PSU_IDParam = InsertPC.Parameters.Add("@PSU_ID", SqlDbType.Int);
PSU_IDParam.Value = Session["PSUID"];

SqlParameter RAM_IDParam = InsertPC.Parameters.Add("@RAM_ID", SqlDbType.Int);
RAM_IDParam.Value = Session["RAMID"];

SqlParameter Case_IDParam = InsertPC.Parameters.Add("@Case_ID", SqlDbType.Int);
Case_IDParam.Value = Session["CaseID"];

SqlParameter Primary_Storage_Drive_IDParam = InsertPC.Parameters.Add("@Primary_Storage_Drive_ID", SqlDbType.Int);
Primary_Storage_Drive_IDParam.Value = Session["HDD1ID"];

SqlParameter Secondary_Storage_Drive_IDParam = InsertPC.Parameters.Add("@Secondary_Storage_Drive_ID", SqlDbType.Int);
Secondary_Storage_Drive_IDParam.Value = Session["HDD2ID"];

SqlParameter Primary_Optical_Drive_IDParam = InsertPC.Parameters.Add("@Primary_Optical_Drive_ID", SqlDbType.Int);
Primary_Optical_Drive_IDParam.Value = Session["OpticalDrive1ID"];

SqlParameter Secondary_Optical_Drive_IDParam = InsertPC.Parameters.Add("@Secondary_Optical_Drive_ID", SqlDbType.Int);
Secondary_Optical_Drive_IDParam.Value = Session["OpticalDrive2ID"];

SqlParameter ReturnedIDParam = InsertPC.Parameters.Add("@ReturnedID", SqlDbType.Int);
ReturnedIDParam.Direction = ParameterDirection.Output;


dbconnection.Open();

InsertPC.ExecuteNonQuery();

Session["PCNum"] = (ReturnedIDParam.Value);

Response.Redirect("~/CustomPC_Details.aspx");

【问题讨论】:

标签: c# sql sql-server


【解决方案1】:

您需要检查对象是否为null,并将您的参数设置为DBNull.Value

SqlParameter Secondary_Optical_Drive_IDParam = InsertPC.Parameters.Add("@Secondary_Optical_Drive_ID", SqlDbType.Int);
Secondary_Optical_Drive_IDParam.Value = (object)Session["OpticalDrive2ID"] ?? (object)DBNull.Value;

请注意,您需要转换为 object,因为使用 ?? 运算符,两个可能的值必须具有相同的类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多