- try
- {
- SqlConnection conn = new SqlConnection(@"Data Source=LENOVO-PC\LOVEXIAO;Initial Catalog=lovexiao;User ID=sa;Password=love.xiao");
- conn.Open();
- SqlCommand cmd = conn.CreateCommand();
- cmd.CommandText = "insert into Person(name,sex,age,salary,leve,email) values('测试','无',11,5000,12,'lovexiao@sina.com')";
- cmd.ExecuteNonQuery();
- MessageBox.Show("打开数据库连接成功");
- conn.Close();
- conn.Dispose();
- MessageBox.Show("关闭数据库连接成功");
- }
- catch
- {
- MessageBox.Show("打开数据库失败!");
- }
复制代码 和- using (SqlConnection conn = new SqlConnection(@"Data Source=LENOVO-PC\LOVEXIAO;Initial Catalog=lovexiao;User ID=sa;Password=love.xiao"))
- {
- try
- {
- conn.Open();
- SqlCommand cmd = conn.CreateCommand();
- cmd.CommandText = "insert into Person(name,sex,age,salary,leve,email) values('测试','无',11,5000,12,'lovexiao@sina.com')";
- cmd.ExecuteNonQuery();
- MessageBox.Show("打开数据库连接成功");
- conn.Close();
- conn.Dispose();
- MessageBox.Show("关闭数据库连接成功");
- }
- catch
- {
- MessageBox.Show("打开数据库失败!");
- }
- }
复制代码 一个没加Using一个加的有Using,但是执行的结果一样,有什么区别吗? |