2013-03-26 72 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

相關問題