2

I have this stored procedure in my SQL Server;

CREATE PROC GetChild
    @Child_ID int
AS
    SELECT * FROM Children WHERE Child_ID = @Child_ID

I am calling this stored procedure from C#.

I would like to know, if it is possible to call just one column from this table instead of the whole record from C#.?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
IT_info
  • 707
  • 4
  • 16
  • 36

5 Answers5

2

You have only have three choices.

  1. Rewrite the Stored procedure to just return the columns you want.

    e.g. SELECT foo from Children Where Child_id = @Child_ID

  2. Use a DataReader and just get the columns you want from that

    Using a reader directly

    while (reader.Read())

      `Console.WriteLine("{0}", reader.GetInt32(0));`
    

    Using the Linq extension methods which allows you to filter and sort the results as well as getting just the columns you want.

    var List = rdr.Cast<IDataRecord>() .Select(s => s.GetInt32(0)).ToList();

  3. Abandon the stored procedure and write Select statements against the table. See Pranay's answer

Community
  • 1
  • 1
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
2

Assuming you mean return one column, if this is what your stored procedure looks like then no. It will always return all columns back to the client.

You can simply ignore the returned columns that you do not need. Or you can change the stored procedure to only return one column. But as is, it always returns all of them.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1

just write below query

select columnname from Children where Child_ID = @Child_ID

columnname- is name of the column you want to retrive

Code for you

SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText ="select columnname from Children where Child_ID = @Child_ID";

 mySqlCommand .Parameters.Add("@Child_ID", SqlDbType.Int);
 mySqlCommand .Parameters["@Child_ID"].Value = idvalue;
 mySqlConnection.Open();

 SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.SingleRow);


while (mySqlDataReader.Read()){
  Console.WriteLine("mySqlDataReader[\" columnname\"] = " +
    mySqlDataReader["columnname"]);
}

mySqlDataReader.Close();
mySqlConnection.Close();
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
0

Use a SqlDataReader for this:

SqlConnection DbConn = new SqlConnection(YourConnStringHere);
SqlCommand ExecStoredProc = new SqlCommand();
ExecStoredProc.CommandType = CommandType.StoredProcedure;
ExecStoredProc.CommandText = "GetChild";
ExecStoredProc.Connection = DbConn;
ExecStoredProc.Parameters.AddWithValue("@ChildID", YourChildId);

using (DbConn)
{
    DbConn.Open();
    using (SqlDataReader sdr = ExecStoredProc.ExecuteReader())
    {
        while(sdr.Read())
            // reference your column name like this:
            // sdr.GetString(sdr.GetOrdinal("YourColumnName"));
    }
}

You can reference any column returned by the SqlDataReader.Read() method. Likewise, if you are looking for an integer value:

int someInt = sdr.GetInt32(sdr.GetOrdinal("YourColumnName"));
0

From this thread ( Insert results of a stored procedure into a temporary table ), you might want to try OPENROWSET.

First, configure your DB,

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO

then, depending on your connection :

SELECT  yourcolumnname
  FROM    
OPENROWSET(
      'SQLNCLI', 
      'server=yourservername;database=yourdbname;uid=youruserid;pwd=youruserpwd',
      'EXEC [GetChild] yourchildid'
       )

or

SELECT  yourcolumnname
  FROM    
OPENROWSET(
      'SQLNCLI', 
      'server=yourservername;database=yourdbname;Trusted_Connection=yes',
      'EXEC [GetChild] yourchildid')

I wouldn't use this solution when retrieving only one line. Performance would be really bad. For retrieving a great number of lines, this should do the job.

Community
  • 1
  • 1
jbl
  • 15,179
  • 3
  • 34
  • 101