简体   繁体   English

单击按钮时如何在文本框中显示不同的sql数据

[英]How to display different sql data in a text box when a button is clicked

My MySql database consists of a column id which contains numbers from 1 to 30. I want to display the number 1 to 30 in a text box each time a button is pressed. MySql数据库由一个列id组成,该列id包含1到30的数字。我想在每次按下按钮时在文本框中显示1到30的数字。 However, my code displays only first row that is 1. 但是,我的代码仅显示第一行。

I have tried following code: 我尝试了以下代码:

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();

I suspect the reason is you have all that code in your button click event. 我怀疑原因是您的按钮单击事件中包含了所有代码。 So every time you click the button, it creates a new connection, executes the query and shows you the first record. 因此,每次您单击按钮时,它都会创建一个新的连接,执行查询并显示第一条记录。

I recommend you reorganize your code. 我建议您重新组织代码。

For example, call the SetupData() function once(maybe on load time or anywhere as long as it is executed only once) then click the button click event will only contain the if statement. 例如,一次调用SetupData()函数(可能在加载时或只要执行一次就可以在任何地方调用),然后单击按钮click事件将仅包含if语句。

SqlDataReader DR1;
public void SetupData(){
     SqlConnection Conn = new SqlConnection("Data Source=SUMIT;Initial          Catalog=Project;Integrated Security=True"); 
     SqlCommand Comm1 = new SqlCommand("Select * from id", Conn); 
     Conn.Open(); 
     DR1 = Comm1.ExecuteReader();
}

 void OnButtonClick(object sender, EventArgs e)
 {
            if (DR1.Read()) {
                textBox3.Text = DR1.GetValue(0).ToString();
 }

There are few ways of doing this. 这样做的方法很少。

First Using SQL 首先使用SQL

Keep the count of the number outside the button click function 将数字计数保持在按钮单击功能之外

int BtnCount = 0;

and in Button Click 然后在按钮中单击

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;
}

Secondly you can call all the data in the collection / list / array which will also enhance the speed of your system 其次,您可以调用集合/列表/数组中的所有数据,这也将提高系统速度

Create the list of strings 创建字符串列表

List<string> ListData = new List<string>();

Populate the list at the constructor like this 像这样在构造器中填充列表

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();

And on Button Click show the content of the list by BtnCount 然后在按钮上单击以显示BtnCount列表的BtnCount

Note : It is just to give you the idea I havent run the program to test. 注意 :只是给您一个想法,我还没有运行程序进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM