2015-08-22 46 views
-4

嗨,大家好,我正在開發一個項目。我的項目將c#中的課程名稱,學分和成績保存到sql中。這是好的。我處理它,但我無法將數據傳輸到計算機的平均等級上。我想array.could你幫我請我怎樣才能將數據從sql導入到c#

+0

請添加更多的信息,喜歡什麼樣的SQL語句來把我的成績在字符串數據,你有什麼實際的目標和你有什麼成就,直到現在。 – PhilMasterG

回答

1

試試這個

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.SqlClient; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      string connStr = "Enter your conneciton string here"; 
      string SQL = "Enter your SQL here"; 

      //uncomment lines below 
      //SqlDataAdapter adapter = new SqlDataAdapter(SQL, connStr); 
      //DataTable dt = new DataTable(); 
      //adapter.Fill(dt); 

      //simulated table 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("Name", typeof(string)); 
      dt.Columns.Add("Age", typeof(int)); 
      dt.Columns.Add("City", typeof(string)); 

      dt.Rows.Add(new object[] { "John", 22, "NY" }); 
      dt.Rows.Add(new object[] { "Mary", 27, "Boston" }); 
      dt.Rows.Add(new object[] { "Paul", 18, "Chicago" }); 


      int count = 0; 
      foreach (DataRow row in dt.AsEnumerable()) 
      { 
       Console.WriteLine("Row {0} : {1}", (count++).ToString(), string.Join(",", row.ItemArray.Select(x => x.ToString()).ToArray())); 
      } 
      Console.ReadLine(); 

     } 
    } 
} 
+0

雖然這個答案可能是正確和有用的,但如果你[包括一些解釋一起](http://meta.stackexchange.com/q/114762/159034)來解釋它如何幫助解決問題,它是首選。如果存在導致其停止工作並且用戶需要了解其曾經工作的變化(可能不相關),這在未來變得特別有用。 –

+0

感謝您的幫助。我不是那個意思。我想我不能明確地抱怨我的問題。 –