检索有关存储过程数据类型的问题

时间:2014-11-07 09:53:18

标签: c# sql-server-2008 stored-procedures

  1. 我编写了一个存储过程
  2. 然后我写了C#代码

    ds.Tables[0].Rows[0][0].ToString().Split(',')
    

    ds是我的数据集,其中包含从ImportData(列SQLQueryName)上面检索的存储过程)

  3. 问题是:我为strMendetory使用了字符串数组,我需要从strMendetory中找出存储过程参数的数据类型。当我为它实现代码时,它像往常一样返回System.string(即使它包含DATETIME)。

    请给我同样的建议。

    存储过程代码:

    CREATE PROCEDURE AddDepartments
            @DepartmentName VARCHAR(100),
            @DepartmentCode VARCHAR(100),
            @CompanyLocationID INT,
            @Details VARCHAR(100),
            @Flag BIT,
            @CreatedDateTime DATETIME,
            @MarkDeleted BIT,
            @AccessGroupMasterID INT
    AS
    BEGIN
        INSERT INTO Departments(DepartmentName, DepartmentCode, CompanyLocationID,  
                                Details, Flag, CreatedDateTime, MarkDeleted, 
                                AccessGroupMasterID)
        VALUES(@DepartmentName, @DepartmentCode, @CompanyLocationID, 
               @Details, @Flag, @CreatedDateTime, @MarkDeleted,
               @AccessGroupMasterID)    
    END
    GO
    
    INSERT INTO ImportData(ImportItemName, ReferenceTableName, ImportFileName, 
                           SQLQueryTypeID, SQLQueryName, MarkDeleted)
    VALUES('Department', 'Departments', 'ImportDepartments.xls',
           2, 'AddDepartments, @DepartmentName, @DepartmentCode, @CompanyLocationID, @Details, @Flag, @CreatedDateTime, @MarkDeleted, @AccessGroupMasterID', 0)
    GO
    

    C#代码:

    String[] strMendetory = ds.Tables[0].Rows[0][0].ToString().Split(',');
    string columnValue="MynewValuToInsertinProcedure";
    
    for (int columCounter = 1; columCounter < strMendetory.Count(); columCounter++)
    {
         cmd.Parameters.AddWithValue(strMendetory[columCounter], columnValue);
    }
    

1 个答案:

答案 0 :(得分:0)

使用sqlparameter,可以指定sql数据类型。这是代码示例:

            SqlParameter[] param = {
                                        new SqlParameter("@param1", SqlDbType.VarChar, 100),
                                        new SqlParameter("@param2", SqlDbType.Int),
                                        new SqlParameter("@param3", SqlDbType.DateTime)
                                   };

            //@param1, @param2, @param3 should match your parameters name in stored procedure

            param[0].Value = "test"; //pass the value to your parameter here
            param[1].Value = 2;
            param[2].Value = DateTime.Now;

            SqlCommand sqlcmd = new SqlCommand();

            foreach (SqlParameter x in param)
            {
                sqlcmd.Parameters.Add(x);
            }