您好我正在尝试从文本框中显示的数据库中获取数据。这样做我创建了三个类:Dal,Controller和TestForm。问题是,我真的不知道在哪里打开连接,也不知道在哪里关闭它。这就是我所做的。
在Dal级,我有:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data.Odbc;
namespace TestingDatabaseConnection
{
class Dal
{
private SqlConnection connection = new SqlConnection();
public SqlConnection GetConnection()
{
if (connection == null)
{
connection.ConnectionString = "Server=Mnemonics-DAT;Database=mem; Integrated Security = true;";
}
return connection;
}
public SqlDataReader GetData()
{
SqlDataReader sqlReads = null;
SqlCommand sqlCommand = new SqlCommand("select * from table_name", GetConnection());
sqlReads = sqlCommand.ExecuteReader();
return sqlReads;
}
}
}
在类控制器中我有:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestingDatabaseConnection
{
class Controller
{
private Dal dal = new Dal();
public SqlDataReader GetData()
{
return dal.GetData();
}
}
}
最后以表格形式:
public partial class TestForm : Form
{
Controller controll;
public TestForm()
{
InitializeComponent();
controll = new Controller();
}
private void showBtn_Click(object sender, EventArgs e)
{
try
{
SqlDataReader sqlReader = controll.GetData();
while (sqlReader.Read())
{
infTxtBox.Text = sqlReader.ToString();
}
}
catch (Exception e1)
{
MessageBox.Show("Something is wrong: " + e1);
}
}
}
我收到的消息说“出错了:ExecuteReader需要一个开放且可用的连接。连接的当前状态已关闭。
我试图解决问题(在Dal类中):
创建一个获取连接值的属性:
public SqlConnection Connect
{
get
{
return connection;
}
}
然后在GetData()方法中使用它:
public SqlDataReader GetData()
{
SqlDataReader sqlReads = null;
try
{
//I call the method Open() here
Connect.Open();
SqlCommand sqlCommand = new SqlCommand("select * from table_name", GetConnection());
sqlReads = sqlCommand.ExecuteReader();
}
catch (Exception e)
{
Console.WriteLine("Error is: " + e);
}
finally
{
//and close it here
Connect.Close();
}
return sqlReads;
}
我现在收到的错误消息是:“出现问题:读取器关闭时无效尝试调用Read” 在引用TestForm类时。
答案 0 :(得分:2)
问题出在这里:
if (connection == null)
{
connection.ConnectionString = "Server=Mnemonics-DAT;Database=mem; Integrated Security = true;";
}
您的连接不能为null,因为您在调用GetConnection
方法之前初始化它。请不要检查您的连接字符串:
if(connection.ConnectionString == "")
{
connection.ConnectionString = "Server=Mnemonics-DAT;Database=mem; Integrated Security = true;";
}
如果您使用using statements
可能会更好,它会在作业完成时自动Dispose
您的Connection
对象,只需定义一个连接字符串变量然后使用:
string connString = "Server=Mnemonics-DAT;Database=mem; Integrated Security = true";
using(var conn = new SqlConnection(connString))
using(var sqlCommand = new SqlCommand("select * from table_name", conn))
{
conn.Open();
sqlReads = sqlCommand.ExecuteReader();
conn.Close();
}
答案 1 :(得分:0)
问题:在Dal
类文件中,您在阅读数据之前未打开连接对象connection
。
解决方案:在阅读数据之前,您需要使用SqlConnection
方法打开Open()
对象。
试试这个:
public SqlDataReader GetData()
{
SqlDataReader sqlReads = null;
SqlCommand sqlCommand = new SqlCommand("select * from table_name", GetConnection());
connection.Open(); //Open your connection object here.
sqlReads = sqlCommand.ExecuteReader();
return sqlReads;
}
答案 2 :(得分:0)
因为你试图在一个封闭的连接上读取你的SqlDataReader。
不要在getData()方法中调用connection.close(),而是在Dal类中添加一个方法 像这样:
public void CloseConnection()
{
connect.close()
}
然后在通过DataReader循环后调用closeConnection方法:
SqlDataReader sqlReader = controll.GetData();
while (sqlReader.Read())
{
infTxtBox.Text = sqlReader.ToString();
}
control.CloseConnection();