string con = "server=" + textBox1.Text + ";database=" + textBox2.Text + ";uid=" + textBox3.Text + ";" + textBox4.Text + ";trusted_connection=no";
SqlConnection con1 = new SqlConnection(con);
con1.Open();
MessageBox.Show("数据库连接成功");
con1.State = true;
button1.Text = "断开连接";连接数据库的时候,点“连接”按钮,成功连接后,按钮上的字就会变成“断开连接”。可是在同一个按钮中,怎样区分它们的具体功能?我现在只是改变按钮上的文字,但是功能还试“连接”的功能啊。。晕哦。哪位大哥给点指示?最好有具体代码,感激不尽。。

解决方案 »

  1.   

    if (button1.Text == "断开连接")
                {
                    button1.Text == "连接";
                }
                else {
                    string con = "server=" + textBox1.Text + ";database=" + textBox2.Text + ";uid=" + textBox3.Text + ";" + textBox4.Text + ";trusted_connection=no";
                    SqlConnection con1 = new SqlConnection(con);
                    con1.Open();
                    MessageBox.Show("数据库连接成功");
                    con1.State = true;
                    button1.Text = "断开连接";
                }
      

  2.   

    你可以在 程序里面 设个标志 flag =1 表示 连接数据库
    flag=0 表示断开执行 click的时候 判断 标志 进行操作
      

  3.   

    在页面当中添加两个按钮,首先是“连接”的按钮显示出来,“断开连接”的按钮display:none,当你连接成功的时候将“连接”的按钮隐藏然后show“断开连接”,事件在各自的按钮之前就可以了
      

  4.   


    if(button1.Text == "断开")
    {
       con1.Close();
       button1.Text == "连接"
    }
    else if(button1.Text == "连接"))
    {
       con1.Open();
       button1.Text == "断开"
    }
      

  5.   

    错误 1 无法将类型“string”隐式转换为“bool” C:\abc\WindowsApplication2\WindowsApplication2\Form1.cs 30 21 WindowsApplication2
      

  6.   

    if(button1.Text.ToString.Trim()== "断开")
    {
         button1.Text == "连接"
    }
    else 
    {
         button1.Text == "断开"
    }
      

  7.   

                string con = "server=" + textBox1.Text + ";database=" + textBox2.Text + ";uid=" + textBox3.Text + ";" + textBox4.Text + ";trusted_connection=no";
                SqlConnection con1 = new SqlConnection(con);
                if (con1.State == false)
                {
                    con1.Open();
                    MessageBox.Show("数据库连接成功");
                    con1.State = true;
                    button1.Text = "断开连接";
                }
                else
                {
                    con1.Close();
                    MessageBox.Show("数据库已断开");
                    con1.State = false;
                    button1.Text = "连接";
      

  8.   

    错误 1 运算符“==”无法应用于“System.Data.ConnectionState”和“bool”类型的操作数 C:\abc\WindowsApplication2\WindowsApplication2\Form1.cs 30 21 WindowsApplication2
    错误 2 无法对属性或索引器“System.Data.Common.DbConnection.State”赋值 -- 它是只读的 C:\abc\WindowsApplication2\WindowsApplication2\Form1.cs 35 21 WindowsApplication2
    错误 3 无法将类型“bool”隐式转换为“System.Data.ConnectionState” C:\abc\WindowsApplication2\WindowsApplication2\Form1.cs 35 33 WindowsApplication2
    错误 4 无法对属性或索引器“System.Data.Common.DbConnection.State”赋值 -- 它是只读的 C:\abc\WindowsApplication2\WindowsApplication2\Form1.cs 46 21 WindowsApplication2
    错误 5 无法将类型“bool”隐式转换为“System.Data.ConnectionState” C:\abc\WindowsApplication2\WindowsApplication2\Form1.cs 46 33 WindowsApplication2
      

  9.   


    不好意思,直接手写的,没运行过,不过可以试着在成员变量里面定义一个bool型
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            bool constate = false;
            private void button1_Click(object sender, EventArgs e)
            {
                string con = "server=" + textBox1.Text + ";database=" + textBox2.Text + ";uid=" + textBox3.Text + ";" + textBox4.Text + ";trusted_connection=no";
                SqlConnection con1 = new SqlConnection(con);
                if (constate == false)
                {
                    con1.Open();
                    MessageBox.Show("数据库连接成功");
                    constate = true;
                    button1.Text = "断开连接";
                }
                else
                {
                    con1.Close();
                    MessageBox.Show("数据库已断开");
                    constate = false;
                    button1.Text = "连接";
                }
            }
        }这样应该没什么问题了吧
      

  10.   

    增加个静态bool标志变量
    呵呵
      

  11.   

    if(button1.Text.ToString()== "断开")
    {
       con1.Close();
       button1.Text = "连接"
    }
    else if(button1.Text.ToString() == "连接"))
    {
       con1.Open();
       button1.Text = "断开"
    }
    这样是可以的。楼上的那个是因为他多打了一个=号
      

  12.   

    if(button1.Text == "断开")
    {
       con1.Close();
       button1.Text == "连接"
    }
    else if(button1.Text == "连接"))
    {
       con1.Open();
       button1.Text == "断开"
    }
    简单的啊
      

  13.   

    if(button1.Text.ToString.Trim()== "断开")
    {
        button1.Text == "连接"
    }
    else
    {
        button1.Text == "断开"
    }
      

  14.   

    一定要设置标记flag  靠他来改变文字
    因为 你点击链接了  不一定能连上啊
      

  15.   

    if(button1.Text == "断开")
    {
      con1.Close();
      con1.State = false;
      MessageBox.Show("数据库已断开");
      button1.Text = "连接";}
    else if(button1.Text == "连接"))
    {
      string con = "server=" + textBox1.Text + ";database=" +  textBox2.Text+ ";uid="+textBox3.Text + ";" 
    + textBox4.Text+ ";trusted_connection=no";
     SqlConnection con1 = new SqlConnection(con);
      con1.Open();
      con1.State = true;
      MessageBox.Show("数据库连接成功");
      button1.Text = "断开连接";
    }
      

  16.   

    if()
    {
    }
    else
    {
    }
      

  17.   

    直接使用connection的State属性,状态为ConnectionState.Closed或Open则进行相反的操作就可以了
      

  18.   


     public Form19()
            {
                InitializeComponent();
                this.button1.Click+=new EventHandler(button1_Click);        }
            private void button1_Click(object sender,EventArgs e)
            {
                MessageBox.Show("你好");
                this.button1.Click -= new EventHandler(button1_Click);
                this.button1.Click+= new EventHandler(button2_Click);
     
            }
            private void button2_Click(object sender, EventArgs e)
            {
                MessageBox.Show("大家好");
                this.button1.Click -= new EventHandler(button2_Click);
                this.button1.Click += new EventHandler(button1_Click);
                
            }
      

  19.   

    用个条件语句来判断,button的text属性是什么,如果是断开连接,就执行其他方法。