tags:

views:

151

answers:

6

I have a query

 insert into carsdescription
 (description,idCar) 
 value (@description,@idCar)
 where idCar=@idCar;
 select nameOfCar
 from Cars 
 where idCar=@idCar";

How to in one sqlCommand execute this??

using(SqlConnection conn=new SqlConnection(connString))
{
    using(sqlCommand cmd=new SqlCommand())
    {
        cmd.Parameteres, conn etc... //this is a peanut

        // your proposition    
    }
}

table schema:

Carsdescription :

 ID (PK, int, autoincrement)
idcar(FK, int)
Description(varchar)

Cars

Id(int, PK, autoincrement)
Name(nvarchar(255)
+2  A: 
insert into carsdescription (description) value (@description) where idCar=@idCar;

INSERT ... WHERE ??

(not that I know what you're actually trying to do, but that query won't work in the first place)

Select0r
I've corrected it
I think his comment was referring to the fact that a Where Clause on an Insert Statement is nonsense.
Jacob G
+2  A: 

You cannot. Not in a single statement. Why do you even want to?? What's your idea, or your requirement??

You have to use a cmd.ExecuteNonQuery call first to insert the new data, and then you need a second call using cmd.ExecuteReader or filling a DataTable using the SqlDataAdapter to retrieve the data again.

using(SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    string cmdInsert = "insert into carsdescription(description) value (@description)";

    using(sqlCommand cmd = new SqlCommand(cmdInsert, conn))
    {
        cmd.Parameters.AddWithValue("@description", description);
        cmd.ExecuteNonQuery();
    }

    string selectStmt = "select nameOfCar from dbo.Cars where idCar = @idCar";

    using(sqlCommand cmd2 = new SqlCommand(selectStmt, conn))
    {
        cmd2.Parameters.AddWithValue("@idCar", idCar);

        string resultValue = cmd2.ExecuteScalar().ToString();
    }

    conn.Close();
}
marc_s
why not? i am sure it is possible!
Andrey
ok, that's answer is good for me :)
I'm sorry Marc, but you're wrong. See my answer below.
Jacob G
@Jacob G: sorry, actually, **you** are wrong - see my comment to your answer. Your answer is **NOT** one statement
marc_s
This is not necessarily a fruitful avenue of discussion (http://xkcd.com/386/) however, the original questioner was asking about a single sqlcommand.
Jacob G
@Jacob G: ok, granted - you could interpret the OP's question as how to do these two steps in one SqlCommand - then your answer is right on. I was interpreting it as how to do this in one single T-SQL statement, and that isn't possible
marc_s
@marc_s: Fully agreed.
Jacob G
A: 
    using (SqlCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = your_query;
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add(new SqlParameter("@description", description));
        cmd.Parameters.Add(new SqlParameter("@idCar", idCar));
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            //reader here
        }
    }
Andrey
+3  A: 

Are you trying to copy data from one table to another?

Something like

INSERT INTO CarsDescription (description)
    SELECT nameOfCar
        FROM Cars
        WHERE idCar = @idCar

That will copy all the 'nameOfCar' values into the CarsDescription table. If you don't want duplicates change the SELECT to SELECT DISTINCT

Chad
that's one possibility - the original question isn't quite clear on this, really.
marc_s
@marc_s, yeah, I know... I wasn't sure if he was trying to return the rows that were just inserted, or trying to insert from a select.
Chad
+2  A: 

Create a stored procedure that takes Description and IDCar as parameters and returns either a scalar NameOfCar or else a result set from the Cars table. You can then call that stored procedure with a single command from your C# code.

Larry Lustig
+1  A: 

This is perfectly valid to execute in a single call. It might not be recommended, but it is possible.

using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=JunkBox;Integrated Security=SSPI;"))
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO JunkSO(Id, Name) VALUES(@Id, @Description)  SELECT * FROM JunkSO", conn);
                cmd.Parameters.AddWithValue("@Id", 10);
                cmd.Parameters.AddWithValue("@Description", "TestDescription");
                conn.Open();
                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    if (rd.HasRows)
                    {
                        while (rd.Read())
                        {
                            MessageBox.Show(rd[0].ToString() + "  " + rd[1].ToString());
                        }
                    }
                }
            }
Jacob G