1
Answer

ExecuteQuery vs ExecuteReader

Photo of Fon

Fon

15y
11.3k
1
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)

0
Photo of Administrator
Admin 2.3k 1.3m 22y
But if You use sorting or filtering in DataGrid use DataView instead of DataTable. The simple solution (not tested, when it works): myDataGrid.DefaultView[myRow].Row[myField] or more complicated - create Your own property in object inherited from DataGrid: public DataView GridView { get { try { return (DataView)(this.ListManager.List); } catch { return null; } } } and use it: myDataGrid.GridView[myRow].Row[myField]
0
Photo of Administrator
Admin 2.3k 1.3m 22y
Los_Calibra First get the Row number of the selected row: int gridRow = dataGrid1.CurrentCell.RowNumber Next get the DateSource : DataTable dt = ((DataSet)dataGrid1.DataSource).Tables["My_Table"]; Now get the value: dt.Rows[gridRow]["Column_Name"] Hope this Helps
0
Photo of Administrator
Admin 2.3k 1.3m 22y
When You select only one row (standard in DataGrid) it's quite good. Next - you may create inherited DataGrid and override select methods (with "new" keyword; usual overriding is impossible). Now I'm trying to do this. I use four methods: public new void Select(int row){ ... } public new void UnSelect(int row){ ... } public new bool IsSelected(int row){ ... } protected new void ResetSelection(){ ... } It works, but not always - it's necessary to find another methods/properties to override I think. You may create your own event OnSelection etc. in these methods (or create your own delegate with another parameters): public event EventHandler OnSelection; and use it: if(OnSelection!=null) OnSelection(this);
0
Photo of Administrator
Admin 2.3k 1.3m 22y
At this point I have this problem: How can I get the data object behind the currently selected row of a DataGrid WinControl. How can I retrive the value of a specific field what corespond to that selected row - for example value for IDField who does'nt have a reprezentative column in that DataGrid. A C# example will be good!
0
Photo of Administrator
Admin 2.3k 1.3m 22y
CurrentCellChanged CurrentCell.RowNumber