0

I have a problem with the following code. Please help.

What I am trying to do is pass a sqlcommand to a function which then returns a dataset.

The function 'Get data' takes sqlcommand as a parameter. This function is in class 'DatabaseUtilities'

    //Initializing sql connection
    static SqlConnection _Connection = new SqlConnection("Data Source=(local);Initial Catalog=db_Test;Integrated Security=True");

    //Connection property
    public static SqlConnection Connection
    {
        get {return _Connection;}
    }

    //The class that takes sqlcommand as parameter
    public static DataSet GetData(SqlCommand Command)
    {
        _Connection.Open();

        SqlDataAdapter Adapter = new SqlDataAdapter();
        Adapter.SelectCommand = Command;

        DataSet Table = new DataSet();

        Adapter.Fill(Table);

        _Connection.Close();

        return Table;
    }

This is how sqlcommand is passed into the above function. This function is from a different class.

public DataSet GetLogByDate(string SearchValue)
    {
        Command.CommandType = CommandType.StoredProcedure;
        Command.Connection = DatabaseUtilities.Connection;

        Command.CommandText = "sp_GetLogByDate";
        Command.Parameters.AddWithValue("@LogDate", SearchValue);

        return GetData(Command);
    }

This code throws the fllowing error.

Invalid object name 'sp_GetLogByDate'.

I do have the above stored procedure in my database. I have no idea why this happened. Could anyone help?

4

1 回答 1

2

您必须Command连接Connection

//The class that takes sqlcommand as parameter
public static DataSet GetData(SqlCommand Command)
{
    Command.Connection = _Connection;
于 2013-03-14T17:21:19.473 回答