Fon

Fon

  • NA
  • 1
  • 0

ExecuteQuery vs ExecuteReader

May 26 2009 11:00 PM
I don't know why when I use select with ExecuteQuery my result will have repeated the lastes line.
And my friend adviced me that I should use ExecuteReader now it's work
What's the different ???

here is my code !!


public void Add()
{
if (File.Exists("" + info + ".db"))
{
sql_con = new SQLiteConnection("Data Source= " + info + ".db;Version=3;New=False;Compress=True;");
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = "drop table par ;";
sql_cmd.ExecuteNonQuery();
}
else
{
sql_con = new SQLiteConnection("Data Source= " + info + ".db;Version=3;New=True;Compress=True;");
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
}
sql_cmd.CommandText = "create table par (Date nvarchar(20), Time nvarchar(10), Linein NVARCHAR(20), Lineout NVARCHAR(20), Number NVARCHAR(20), Status nvarchar(10),Duration int(20), Cost nvarchar(20), Detail nvarchar(10), Network nvarchar(10));";
sql_cmd.ExecuteNonQuery();
StreamReader reader = new StreamReader(inputfilename);
string line = reader.ReadLine();
while (line != null)
{
string NNDuration = line.Replace("'", "''");
string Date = line.Substring(12, 8);
if (line[11] == '=')
{
string Time = line.Substring(21, 5);
string Linein = line.Substring(27, 10);
string Lineout = line.Substring(38, 1);
if (string.Compare(Lineout, "0") >= 0 && string.Compare(Lineout, "9") <= 0)
{
string Number = line.Substring(40, 20);
string Status = line.Substring(61, 3);
string NDuration = NNDuration.Substring(65, 10);
string b = NDuration.Substring(2, 2);
string c = NDuration.Substring(6, 2);
int y = Convert.ToInt32(b);
int l = Convert.ToInt32(c);
int Duration = y + l;
string OCost = line.Substring(73, 5);
string Cost = OCost.Trim();
string Detail = line.Substring(79, 3);
string Network = line.Substring(82, 8);
sql_cmd.CommandText = "insert into par values('" + Date + "','" + Time + "','" + Linein + "','" + Lineout + "','" + Number + "','" + Status + "','" + Duration + "','" + Cost + "','" + Detail + "','" + Network + "');";
sql_cmd.ExecuteNonQuery();

textBox14.Text = textBox14.Text + "\r\n" + Date + " ";
}
else
{
textBox5.Text = textBox5.Text + "\r\n" + line;
}
}
else
{
textBox5.Text = textBox5.Text + "\r\n" + line;
}
line = reader.ReadLine();
}

}
private void LoadData()
{
string CommandText = "select * from par "; //??????????????
sql_cmd.ExecuteReader();
DB = new SQLiteDataAdapter(CommandText, sql_con); //???????????????????????????????????
DS.Reset(); //?? dataset ????????????????? reset ????????????
DB.Fill(DS); //?????????????????????????????????????????????? ????????? Dataset
DT = DS.Tables[0]; //??????????????????????????????? datatable
dataGridView1.DataSource = DT; //???????? datatable ??????????????? dataGridViews
sql_con.Close();

}

Answers (1)