2013-03-26 73 views
1

我对于使用数据集,适配器,数据表等有点新了......所以我认为这可以作为一个noob问题。为什么我的Datatable没有任何结果进来?

我使用可视化设计器(带有.xsd扩展名的文件)在visual studio中创建了数据集。我在那里定义了一个我命名为“课程”的DataTable。我通过使用sql server查询设计器填充此表,所以我知道我的数据连接正确。

在我的代码中,我正在测试如何创建DataTable,然后将其中的列映射到本地类,我称其为Course.cs。下面是方法:

MyContext ctx = new MyContext(); 

SqlConnection conn = 
    new SqlConnection("Data Source=****;Initial Catalog=****;User ID=****;Password=****"); 
SqlCommand command = new SqlCommand(@"select * from stuff", conn); 

SqlDataAdapter da = new SqlDataAdapter(); 
MyDataSet ds = new MyDataSet(); 
ds.Locale = CultureInfo.InvariantCulture; 
da.Fill(ds); 
DataTable courses = ds.Courses; 

IEnumerable<DataRow> query = 
    from course in courses.AsEnumerable() 
    select course; 
foreach (var course in query) 
{ 
    Course localCourse = new Course(); 
    localCourse.CourseCode = course.Field<string>("CourseCode"); 
    ctx.Courses.Add(localCourse); 
} 
ctx.SaveChanges(); 

我没有得到任何错误,它会运行的代码很好,但自从“查询”变量不中它有什么,它不是使新对象我想要的映射值。

我在这里错过了什么?

编辑

感谢您的快速反应!当我添加的代码行:

da.Fill(ds); 

它给我这个例外:

{"The SelectCommand property has not been initialized before calling 'Fill'."} 

其迷惑我,因为我不想创建一个选择命令,我虽然这已经在我的数据表?

编辑两个

我做了什么建议,这是有道理的什么,我需要做的,但我仍然得到任何结果(没有错误了,但..)。如果我在相同的连接字符串上运行相同的查询,我可以得到结果,任何想法?

编辑三!

这里是我用来填充数据表的原始和丑陋的查询。就像我说的,如果我在SSMS中做的话,结果会很好。

select 
s.AdClassSchedId, 
rtrim(s.code) + 
CASE WHEN (isnull(s.section,'') <> '') 
THEN '-S' + s.section 
ELSE '' END as CourseCode, 
s.descrip + 
CASE WHEN (isnull(s.section,'') <> '') 
THEN ' - Section ' + s.section 
ELSE '' END as CourseName, 
s.AdTeacherId, 
s.StartDate, 
s.EndDate, 
s.DateLstMod as ClassSchedLastModified, 
t.AdTermId 
from adclasssched s 
inner join adclassschedterm t on s.adclassschedid = t.adclassschedid 
where s.active = 1 
+2

你没有填写数据表。请阅读SQLDataAdapter。这里有一个有用的链接http://www.codeproject.com/Tips/462730/Five-different-overloads-of-the-DataAdapter-Fill-m – Dhawalk 2013-03-26 13:44:59

+0

是你的工作? – 2013-03-26 14:06:23

+0

@PranayRana我欣赏你的彻底的答案,但我仍然没有看到我的查询变量中的数据...张贴另一个编辑我的问题。 – ledgeJumper 2013-03-26 14:08:00

回答

2

WAY1

如果您正在使用的SqlCommand比你需要打开和这样

SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind"); 

SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn); 
selectCMD.CommandTimeout = 30; 

SqlDataAdapter custDA = new SqlDataAdapter(); 
custDA.SelectCommand = selectCMD; 

nwindConn.Open(); 

DataSet custDS = new DataSet(); 
custDA.Fill(custDS, "Customers"); 

nwindConn.Close(); 

Populating a DataSet from a DataAdapter


紧密结合Way2

填写DataSet对象

更新您

// Assumes that connection is a valid SqlConnection object. 
string queryString = 
    "SELECT CustomerID, CompanyName FROM dbo.Customers"; 
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection); 

DataSet customers = new DataSet(); 
adapter.Fill(customers, "Customers"); 

Populating a DataSet from a DataAdapter


更新

之前填写您的数据集对象,不是在这里,你的代码

MyDataSet ds = new MyDataSet(); 
ds.Locale = CultureInfo.InvariantCulture; 
//fill datase object ds 
//DataAdapter.Fill(ds) 
DataTable courses = ds.Courses; 

此错误

{"The SelectCommand property has not been initialized before calling 'Fill'."} 

做如下,作为该组的错误选择命令

SqlDataAdapter adapter = new SqlDataAdapter(); 
SqlCommand command = new SqlCommand("SELECT * FROM table", connection); 
adapter.SelectCommand = command; 

完整的源:SqlDataAdapter.SelectCommand Property

+0

检查我的编辑在这一个.. – ledgeJumper 2013-03-26 13:50:00

+0

@davidisawesome - 检查我的更新为您的错误,并访问链接的更多细节... – 2013-03-26 13:53:07

+0

知道了!感谢勤奋的回应,这样的代码有点丑陋,但它的工作原理! – ledgeJumper 2013-03-26 14:33:24

4

我看不到你在任何地方调用适配器?

应该有一行代码在那里,看起来像某个地方......

SqlDataAdapter da = new SqlDataAdapter("select * from courses", mySqlConnection); 

da.Fill(ds); 

此外,您需要将数据适配器的select命令设置为你想要从运行SQL语句数据库。

+0

检查我的编辑在这一个.. – ledgeJumper 2013-03-26 13:49:27

+0

因此,即使我的数据表是使用SQL查询创建的,我需要在数据适配器上运行相同的查询? – ledgeJumper 2013-03-26 13:53:16

+0

在设计时创建表与在运行时设置连接和sql语句不同。如果您在设计时使用该向导生成数据集,则该sql语句仅用于构建数据集的结构。您需要将设计器上的select命令文本属性设置为相同的SQL。 – 2013-03-26 14:00:09

相关问题