我有以下代码:
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()
的备用解决方案进行了尝试,数据获取没有问题。