IF和Select语句 [英] IF and Select statement

查看:123
本文介绍了IF和Select语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好





希望你们一切顺利!





i有两张桌子

- 书

包含书ID



- ss

包含书籍ID和Snumber



我想要用户当他在文本框中插入Snumber时从图书ID获取表格



这是代码



hello


hope you all are doing well !


i have two tables
- book
contain book id
and
- ss
contain book id and Snumber

I want the user to get the form from the book id when he insert the Snumber in textBox1

this is the code

private void button1_Click(object sender, EventArgs e)
        {
            


            String constring = "Data Source=V-PC;Initial Catalog=SMA;Integrated Security=True";
            SqlConnection con1 = new SqlConnection(constring);
           

            String Search = "Select bookID from ss where Snumber='" + this.textBox1.Text + "'";
           
            SqlCommand SearchS = new SqlCommand(Search, con1);
            SqlDataReader myreader;
            

            try
            {
                con1.Open();
                myreader = (SearchS.ExecuteReader());
                


                if (Search =="1")
                {
                    this.Hide();
                    S1 d1 = new S1();
                    d1.ShowDialog();
                }
                else if (Search == "2")
                {
                       this.Hide();
                   S2 e3 = new S2();
                    e3.ShowDialog();
                }
                /*else if   ( Search == "3")
                {
                       this.Hide();
                   S3 e3 = new S3();
                    e3.ShowDialog();
                }*/

                

                while (myreader.Read())
                {
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            con1.Close();
        }
        }







没有错误

但在调试时我按下包含此代码的按钮



按钮什么都不做!!







我做错了什么或忘记了什么?




there is no error
but in debugging when I press the button that contain this code

the button do nothing !!



Did I do something wrong or forget something ?

推荐答案

好吧,我是真的很惊讶!让我们把不相关的代码拿走:

Well, I'm not really surprised! Lets just take "irrelevant" code away:
private void button1_Click(object sender, EventArgs e)
        {
...
            String Search = "Select bookID from ss where Snumber='" + this.textBox1.Text + "'";
...           
                if (Search =="1")
                {
...
                }
                else if (Search == "2")
                {
...
                }
...
        }

由于搜索永远不会等同于它对它无效的价值。



你想要做的就是使用 myreader 返回数据以做出这些决定......

Since Search will never equal either value it does nothing with it.

Probably what you want to do is to use the values in the myreader returned data to make those decisions...


int x /*define it global*/
while (myreader.read())
{
    x = int.parse(myreader[0].ToString());
}
if (x =="1")
                {
                    this.Hide();
                    S1 d1 = new S1();
                    d1.ShowDialog();
                }



我已经给你一个解决方案了解我的问题

希望这会对你有所帮助





代码块固定,拼写[/ edit]


I have given you a solution as much I have understood your question
hope this will help you


[edit]Code block fixed, spelling[/edit]


这篇关于IF和Select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆