0

我正在尝试编写将值插入表中的过程,但值未插入表中以供参考我给出了我尝试过的代码......谁能帮助我

插入我试试这段代码

public int Visitor_Insert(visitor_Master visitor_obj, common_Class comm_obj)
{
    DB_Connection();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "INSERT_VISITOR";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@VISITOR_NAME", visitor_obj.VisitorName);
    cmd.Parameters.AddWithValue("@VISITOR_CONTACT", visitor_obj.VisitorContact);
    cmd.Parameters.AddWithValue("@VISITOR_ADDRESS", visitor_obj.VisitorAddress);
    cmd.Parameters.AddWithValue("@VISITOR_AGE", visitor_obj.VisitorAge);
    cmd.Parameters.AddWithValue("@VISITOR_VEHICLENO", visitor_obj.VisitorVehicleNO);
    //cmd.Parameters.AddWithValue("@VISITOR_AGE ", visitor_obj.VisitorAge);
    int i = cmd.ExecuteNonQuery();
    return i;
}

对于程序,我写了这段代码

USE [VPMS]
GO
/****** Object:  StoredProcedure [dbo].[INSERT_VISITOR]    Script Date: 07/24/2013 16:01:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[INSERT_VISITOR]
    @VISITOR_NAME NVARCHAR(30),
    @VISITOR_CONTACT NVARCHAR(10),
    @VISITOR_ADDRESS NVARCHAR(40),
    @VISITOR_AGE NVARCHAR(2),
    @VISITOR_VEHICLENO NVARCHAR(20),
    @VISITOR_IMAGE IMAGE,
    @VISITOR_CREATEDBY NVARCHAR(20),
    @VISITOR_CREDTEDON NVARCHAR(20),
    @VISITOR_MODIFIEDBY NVARCHAR(20),
    @VISITOR_MODIFIEDON NVARCHAR(20)
    AS
    DECLARE @VISITOR_ID INT,

BEGIN
    SET NOCOUNT ON;
    SELECT @VISITOR_ID= MAX(Visitor_Id) FROM Visitor_Master 
    IF (@VISITOR_ID > 0 )
    BEGIN
        SET @VISITOR_ID=@VISITOR_ID+1
    END
    ELSE
    BEGIN
        SET @VISITOR_ID=10
    END
    INSERT INTO Visitor_Master( Visitor_Id, VisitorName, Visitor_Contact, Visitor_Address, Visitor_age, 
    Visitor_VehicleNo, Visitor_Image, Created_By, Created_On, Modify_By, Modify_On)
    VALUES(@VISITOR_ID,@VISITOR_NAME,@VISITOR_CONTACT,@VISITOR_ADDRESS,@VISITOR_AGE,@VISITOR_VEHICLENO,@VISITOR_IMAGE,@VISITOR_CREATEDBY,
    @VISITOR_CREDTEDON,@VISITOR_MODIFIEDBY,@VISITOR_MODIFIEDON)

END
4

2 回答 2

0

因为您没有传递过程中定义的所有参数。它会抛出异常。

在这里,我已null根据您的要求分配给您未指定分配值的其他参数

ALTER PROCEDURE [dbo].[INSERT_VISITOR]
    @VISITOR_NAME NVARCHAR(30),
    @VISITOR_CONTACT NVARCHAR(10),
    @VISITOR_ADDRESS NVARCHAR(40),
    @VISITOR_AGE NVARCHAR(2),
    @VISITOR_VEHICLENO NVARCHAR(20),
    @VISITOR_IMAGE IMAGE = null,
    @VISITOR_CREATEDBY NVARCHAR(20) = null,
    @VISITOR_CREDTEDON NVARCHAR(20) = null,
    @VISITOR_MODIFIEDBY NVARCHAR(20) = null,
    @VISITOR_MODIFIEDON NVARCHAR(20) = null
     -- rest of code
于 2013-07-24T11:47:41.373 回答
0

您的过程中有@VISITOR_IMAGE,@VISITOR_CREATEDBY,@VISITOR_CREDTEDON,@VISITOR_MODIFIEDBY,@VISITOR_MODIFIEDON参数,但尚未将值传递给上述参数。要么将值传递给所有参数,要么允许 null

BEGIN
set @VISITOR_IMAGE = null,
set @VISITOR_CREATEDBY = null,
set @VISITOR_CREDTEDON = null,
set @VISITOR_MODIFIEDBY = null,
set @VISITOR_MODIFIEDON = null
........
于 2013-07-24T11:54:29.047 回答