nuser nuser - 4 months ago 7
SQL Question

.Net SQL Server connection simple class with procedure

I need write simple class in C#. I have 2 tables. But for understanding I use just 1 table.

public string[] GetCustOrders(int CustomerCODE)
{
SqlConnection myConn = new SqlConnection("server=(local);Initial Catalog=dbName;Integrated Security=True");

SqlDataAdapter myData = new SqlDataAdapter("CustOrdersOrdersDetails", myConn);
myData.SelectCommand.CommandType = CommandType.StoredProcedure;
myData.SelectCommand.Parameters.Add(new SqlParameter("@CustomerCODE", SqlDbType.Int, 0));
myData.SelectCommand.Parameters["@CustomerCODE"].Value = CustomerCODE;

// string[] as = string[6];
string[] as1 = string[3];
// string[] as2 = string[3];

DataSet ds = new DataSet();
myData.Fill(ds);

return as;
}


And my stored procedure code:

CREATE PROCEDURE CustOrdersOrdersDetails
@CustomerCODE int
AS
SELECT
Name,
Action,
Comments
FROM
System2
WHERE
Code = @CustomerCODE


The stored procedure will select just 1 row from the table. But I don
t know how from
SqlDataAdapter` get each value of cell of this one row and give this value for my array as1.

Ex., "as[1]=" and after = I need give value of Action from selected row.

I know this simple, but I think... and think, that I can do this just give all values of my row to the DataSet and after that give each value of each cell to each string from my array. But I tray and cant finish this class...

Can anyone help?

Answer
int i = 0;    
// For each table in the DataSet, print the row values.
foreach(DataTable table in ds.Tables)
{
    foreach(DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            as[i]=row[column]);
            i++;
        }
    }
}