using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace _2210._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} SqlConnection myConnection = new SqlConnection();
private void Form1_Load(object sender, EventArgs e)
{
string sConString;
sConString = "Data Source =(local);Initial Catalog =Master;Integrated Security=SSPI";
myConnection.ConnectionString = sConString;
button1.Enabled = (myConnection.State == ConnectionState.Closed);
button2.Enabled = (myConnection.State == ConnectionState.Open);
label1.Text = " ";
label2.Text = " ";
} private void button1_Click(object sender, EventArgs e)
{
myConnection.Open(); } private void button2_Click(object sender, EventArgs e)
{
myConnection.Close(); }
private void myConnection_StateChange(object sender, StateChangeEventArgs e)
{ label1.Text = "当前连接状态:" + e.CurrentState.ToString();
label2.Text = "连接状态变更前的状态:" + e.OriginalState.ToString();
button1.Enabled = (myConnection.State == ConnectionState.Closed);
button2.Enabled = (myConnection.State == ConnectionState.Open);
}
}
}
这是C#的代码,用于观察连接数据库的状态,但是我在调试的时候,出现异常,在myConnection.Open();中 ,说是“未处理 InvalidOperationException”。请大虾指点迷经......
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace _2210._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} SqlConnection myConnection = new SqlConnection();
private void Form1_Load(object sender, EventArgs e)
{
string sConString;
sConString = "Data Source =(local);Initial Catalog =Master;Integrated Security=SSPI";
myConnection.ConnectionString = sConString;
button1.Enabled = (myConnection.State == ConnectionState.Closed);
button2.Enabled = (myConnection.State == ConnectionState.Open);
label1.Text = " ";
label2.Text = " ";
} private void button1_Click(object sender, EventArgs e)
{
myConnection.Open(); } private void button2_Click(object sender, EventArgs e)
{
myConnection.Close(); }
private void myConnection_StateChange(object sender, StateChangeEventArgs e)
{ label1.Text = "当前连接状态:" + e.CurrentState.ToString();
label2.Text = "连接状态变更前的状态:" + e.OriginalState.ToString();
button1.Enabled = (myConnection.State == ConnectionState.Closed);
button2.Enabled = (myConnection.State == ConnectionState.Open);
}
}
}
这是C#的代码,用于观察连接数据库的状态,但是我在调试的时候,出现异常,在myConnection.Open();中 ,说是“未处理 InvalidOperationException”。请大虾指点迷经......
private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = (myConnection.State == ConnectionState.Closed);
button2.Enabled = (myConnection.State == ConnectionState.Open);
label1.Text = " ";
label2.Text = " ";
}
conn.ConnectionString =@"Data source=(local);Initial Catalog=TestDB;User ID=sa;Password=1234";
SqlConnection myConnection = new SqlConnection();
myConnection.open();
.....执行操作;
myConnection.close();