0
我有以下代码:SQL数据适配器不取数据
string q = "SELECT 1";
using (var sqlCommand = new SqlCommand())
{
sqlCommand.CommandText = q;
sqlCommand.Connection = new SqlConnection(DataAccess.ConnectionString);
DataTable dt = new DataTable();
using (SqlConnection connection = new SqlConnection(DataAccess.ConnectionString))
{
try
{
connection.Open();
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter())
{
sqlAdapter.SelectCommand = sqlCommand;
sqlAdapter.Fill(dt);
}
}
catch (Exception ex)
{
dt = null;
}
finally
{
connection.Close();
}
}
}
被改变代码结构,并尝试调试,但还是没能得到的数据进行每一次取出。到底dt
值始终{}
。连接字符串是正确的,因为已使用备用解决方案SqlDataReader ExecuteReader()
尝试过,数据获取没有问题。
试图字符串'结果= dt.Rows [0 ] [0] .ToString();'它也显示1。但是在调试过程中,当我将鼠标悬停在'dt'上时,我无法理解它总是显示'{}' –
@Amigo ..那么您需要点击小放大镜在调试时查看DataTable中的数据。确保你没有得到你在上面的代码中吞咽的异常。 – user3185569
明白了,谢谢大家。 –