How to insert data in a table in SQL in sequence

1.1k views Asked by At

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?

2

There are 2 answers

0
C J On

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
2
thepirat000 On

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