I'm trying to create multiple rows at the same time, each row implements previous rows Primary key. Is there a way i can make all this happen in one query instead of creating multiple query's for doing it, like shown in code sample?
public void CreateMessage(int profileId, String text, int chatId)
{
string stmt = "INSERT INTO Activity (profileID, timeStamp) OUTPUT INSERTED.activityID values (" + profileId + ", '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "')";
SqlDataReader reader = new SqlCommand(stmt, con).ExecuteReader();
reader.Read();
stmt = "INSERT INTO Text(activityID, message) OUTPUT INSERTED.textID values(" + Int32.Parse(reader["activityID"].ToString()) + ", (select cast('" + text + "' as varbinary(max))))";
reader.Close();
reader = new SqlCommand(stmt, con).ExecuteReader();
reader.Read();
stmt = "INSERT INTO Message (textID, chatID) values (" + Int32.Parse(reader["textID"].ToString()) + ", " + chatId + ")";
reader.Close();
new SqlCommand(stmt, con).ExecuteNonQuery();
}