-1

Consider this code:

public void Insertindb(int chnlNm, string Date, string Time, string Progname)
{
    DbConnection db = new DbConnection();
    db.connectionOpen();
    SqlCommand cmd = new SqlCommand("INSERT INTO dbo.SchedulProgram (ChannelNumber,Date,Time,ProgramName) VALUES(@chnlNm,@date,@time,@progname)", db.con);

    cmd.Parameters.Add(new SqlParameter("@chnlNm", chnlNm));
    cmd.Parameters.Add(new SqlParameter("@date",Date));
    cmd.Parameters.Add(new SqlParameter("@time", Time));
    cmd.Parameters.Add(new SqlParameter("@progname", Progname));
    Console.WriteLine("{0} {1} {2} {3}",chnlNm,Date,Time,Progname);

    cmd.ExecuteNonQuery();
    db.connectionClose();
}

I use this code to insert data in a table. It inserts data in a table. When I call the function multiple times,

sdb.Insertindb(channelno, date, time, programname)

It randomly inserts data in the table not in sequence. I want it to insert data in sequence. How is it possible?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

0

I don't know why the commenters says that there is no "sequence" or "order" in the data. Yes, there IS a physical order in the tables, and its defined by the CLUSTERED INDEX of the table.

A clustered index determines the physical order of data in a table.

Check this fiddle: http://www.sqlfiddle.com/#!3/d7e14/2

thepirat000
  • 12,362
  • 4
  • 46
  • 72
  • 1
    The clustered index does exist, yes, but it does **not** guarantee anything about the order in which rows are returned from a table. On *small* tables, and performing single queries, the results will *usually* be returned in the clustered index order, but that is by no means guaranteed - without an `ORDER BY` clause, the system is free to return rows in whatever order is most convenient. – Damien_The_Unbeliever Mar 13 '14 at 13:15
  • See e.g. [this article](http://sqlblog.com/blogs/hugo_kornelis/archive/2006/12/31/Beatles-vs-Stones.aspx) for a demonstration where the results (without an `ORDER BY`) are returned in an order different to the clustered index order. – Damien_The_Unbeliever Mar 13 '14 at 13:19
0

since your inserting the date and time, is that the order of what your refer to as 'row after row'?

if so, the simple solution would be to select ordered by those columns?

select chnlNm, Date, Time, Progname from dbo.SchedulProgram order by date, time
C J
  • 144
  • 8