得到行的標識出了什麼問題請?C#:計數和從SQL
我能得到的記錄數,但不能獲得價值每條記錄的ID
我有這個函數計算並獲得ID值: 我認爲這個問題是在功能
Form1中
public List<int> countInTable(Double codebar)
{
cmd.Connection = cn; // sqlconn
cn.Open();
List<int> id = new List<int>();
String countString = "SELECT COUNT (*) FROM medicaments WHERE code_bare = @codebar ";
var myCommand = new SqlCommand(countString, cn);
myCommand.Parameters.AddWithValue("@codebar", codebar);
int count = Convert.ToInt32(myCommand.ExecuteScalar());
id.Add(count);
using (rd = myCommand.ExecuteReader())
{
// loop over all rows returned by SqlDataReader
while (rd.Read())
{
// grab the column no. 0 (id" from
// SQL select statement) as a string then int, and store it into list of id
t0 = rd[0].ToString();
id.Add(int.Parse(t0));
}
}
rd.Close();
cn.Close();
return id;
}
然後調用第一功能的另一種形式讓每個記錄 窗口2的數據
private void laodProduct(Double codebar)
{
this.listView2.Items.Clear();
List<int> total = manipBaseDonnees.countInTable(double.Parse(textBox2.Text));
foreach (int t in total)
{
listBox1.Items.Add(t);
List<string> donnees = manipBaseDonnees.getFromTableID(t);
var item1 = new ListViewItem(new[] { donnees[1], donnees[2], donnees[6], donnees[7], donnees[8], donnees[4], donnees[0] }, -1, Color.Empty, Color.Yellow, null);
this.listView2.Items.AddRange(new ListViewItem[] { item1 });
}
}
什麼關於計數變量?他需要改變,以及 – Kuzgun
@Kuzgun:是的,我說的是第二次查詢......不會改變當前的查詢 – musefan