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 = "断开连接";连接数据库的时候,点“连接”按钮,成功连接后,按钮上的字就会变成“断开连接”。可是在同一个按钮中,怎样区分它们的具体功能?我现在只是改变按钮上的文字,但是功能还试“连接”的功能啊。。晕哦。哪位大哥给点指示?最好有具体代码,感激不尽。。
SqlConnection con1 = new SqlConnection(con);
con1.Open();
MessageBox.Show("数据库连接成功");
con1.State = true;
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 = "断开连接";
}
flag=0 表示断开执行 click的时候 判断 标志 进行操作
if(button1.Text == "断开")
{
con1.Close();
button1.Text == "连接"
}
else if(button1.Text == "连接"))
{
con1.Open();
button1.Text == "断开"
}
{
button1.Text == "连接"
}
else
{
button1.Text == "断开"
}
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 = "连接";
错误 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
不好意思,直接手写的,没运行过,不过可以试着在成员变量里面定义一个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 = "连接";
}
}
}这样应该没什么问题了吧
呵呵
{
con1.Close();
button1.Text = "连接"
}
else if(button1.Text.ToString() == "连接"))
{
con1.Open();
button1.Text = "断开"
}
这样是可以的。楼上的那个是因为他多打了一个=号
{
con1.Close();
button1.Text == "连接"
}
else if(button1.Text == "连接"))
{
con1.Open();
button1.Text == "断开"
}
简单的啊
{
button1.Text == "连接"
}
else
{
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 = "断开连接";
}
{
}
else
{
}
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);
}