我的MySql
数据库由一个id
包含1到30的数字的列组成。我想在每次按下按钮时在文本框中显示1到30的数字。但是,我的代码仅显示第一行。
我尝试了以下代码:
SqlConnection Conn = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True");
SqlCommand Comm1 = new SqlCommand("Select * from id", Conn);
Conn.Open();
SqlDataReader DR1 = Comm1.ExecuteReader();
if (DR1.Read()) {
textBox3.Text = DR1.GetValue(0).ToString();
}
Conn.Close();
这样做的方法很少。
首先使用SQL
将数字计数保持在按钮单击功能之外
int BtnCount = 0;
然后在按钮中单击
SqlConnection Conn = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True");
SqlCommand Comm1 = new SqlCommand("Select * from id WHERE ID = " + BtnCount , Conn);
Conn.Open();
SqlDataReader DR1 = Comm1.ExecuteReader();
if (DR1.Read()) {
textBox3.Text = DR1.GetValue(0).ToString();
}
Conn.Close();
BtnCount ++;
if(BtnCount>30)
{
BtnCount = 0;
}
其次,您可以调用集合/列表/数组中的所有数据,这也将提高系统速度
创建字符串列表
List<string> ListData = new List<string>();
像这样在构造器中填充列表
SqlConnection Conn = new SqlConnection("Data Source=SUMIT;Initial Catalog=Project;Integrated Security=True");
SqlCommand Comm1 = new SqlCommand("Select * from id WHERE ID = " + BtnCount , Conn);
Conn.Open();
SqlDataReader DR1 = Comm1.ExecuteReader();
while(DR1.Read())
{
ListData.Add(DR1.GetValue(0).ToString());
}
Conn.Close();
并在按钮上单击以显示列表内容 BtnCount
注意:只是给您一个想法,我还没有运行程序进行测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句