2015-08-22 43 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

感谢您的帮助。我不是那个意思。我想我不能明确地抱怨我的问题。 –