.net连接未关闭。连接的当前状态为已打开。

来源:百度知道 编辑:UC知道 时间:2024/05/25 07:17:29
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}

private void button1_Click(object sender, EventArgs e)
{
string sql="select zhanghao mima from students";
//try
//{

SqlCommand com = new SqlCommand(sql, lei.con);
lei.con.Open();
SqlDataReader sdr = com.ExecuteReader();<

SqlCommand com = new SqlCommand(sql, lei.con);
lei.con.Open();

这里打开了连接,
可以修改为com.Open();
com.Close();
或 lei.con.Close();

数据和表现要分开,先判断是否进入成功,返回一个布尔值,在来根据这个BOOL值弹出对话框提示成功还是失败