locked
Stored Procedure dies at call from C# code RRS feed

  • Question

  • User-718146471 posted

    Hey folks, I'm generally a logical person and this error would make sense if I wasn't providing a value for the proc parameter. The error I get is:

    Cannot bulk load. The file "' + @FilePath + '" does not exist.

    However, the value of the parameter is a file path that I am providing; it does have the expected string value. So I'm completely baffled as to what it wants. Here's my c# Stored Proc code:

                string KickOff = "sp_ImportData";
                SqlConnection connProc = new SqlConnection(_csCVEMIDB);
                SqlCommand cmdProc = new SqlCommand();
                connProc.Open();
                cmdProc.Connection = connProc;
                cmdProc.CommandTimeout = 0;
                cmdProc.CommandType = System.Data.CommandType.StoredProcedure;
                cmdProc.Parameters.AddWithValue("@FilePath", csvPath.ToString()); // csvPath has the correct path in debugger
                cmdProc.CommandText = KickOff.ToString();
                cmdProc.ExecuteNonQuery();
                connProc.Close();
    

    And the stored proc:

    ALTER PROCEDURE [dbo].[sp_ImportData]
    (
    	@FilePath nvarchar(MAX)
    )
    AS
    BEGIN
    	-- SET NOCOUNT ON added to prevent extra result sets from
    	-- interfering with SELECT statements.
    	SET NOCOUNT ON;
    
    	CREATE TABLE #ImportData
    	(
                 ... -- Taking out sensitive bits - there are table vars here
    	)
    
    DECLARE ... -- sensitive bits scrubbed - trust me this part of the proc works fine if I can pass a variable to it
    
        -- Insert statements for procedure here with parameter @FilePath
    	BULK INSERT #ImportData from ''' + @FilePath + ''' WITH
    	(
    		FIELDTERMINATOR = ',',
    		ROWTERMINATOR='0x0a',
    		FIRSTROW = 2
    	)
    

    So am I missing something? Did I wire this up wrong?

    Friday, October 21, 2016 3:29 PM

Answers

  • User535082756 posted

    hi

    I am not sure whether this is the problem. Just a thought. May be the file doesnot exist in the database hard disk? the file should be physically present in the db for this to be updated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 21, 2016 3:33 PM

All replies

  • User535082756 posted

    hi

    I am not sure whether this is the problem. Just a thought. May be the file doesnot exist in the database hard disk? the file should be physically present in the db for this to be updated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 21, 2016 3:33 PM
  • User-718146471 posted

    Never mind, I figured it out. I was specifying the path but not the filename in addition. Brain fart Friday I guess LOL

    Friday, October 21, 2016 3:41 PM