我正在使用VS2012和SQL Server Express2008。我已将连接/查询简化为试图找出为什么我的DataSet未被填充的原因。连接成功完成,并且没有引发任何异常,但是适配器未填充DataSet。从中提取的数据库位于同一台PC上,并且使用localhost\SQLEXPRESS不会更改结果。感谢您的输入!

SqlConnection questionConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
questionConnection.Open();
String sql = "SELECT * FROM HRA.dbo.Questions";
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlCommand command = new SqlCommand(sql, questionConnection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
questionConnection.Close();


连接字符串:

<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=CODING-PC\SQLEXPRESS;Initial Catalog=HRA;User ID=sa; Password= TEST" />

最佳答案

尝试这个:

SqlConnection questionConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
questionConnection.Open();
DataSet ds = new DataSet();
String sql = "SELECT * FROM HRA.dbo.Questions";
SqlDataAdapter adapter = new SqlDataAdapter(sql, questionConnection);
adapter.Fill(ds);

adapter.Dispose();
command.Dispose();
questionConnection.Close();

09-26 23:30