2015-05-06 63 views
0

這是我的代碼:填充命令不能使用

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.SessionState; 
//db con 
using System.Data; 
using System.Data.OleDb; 
using System.Data.SqlClient; 
using System.Configuration; 

namespace SOMaintenance.App_Code { 
    public class readdata { 
     string sConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
     public void selectdata(DataSet dataset1, string sqlcommand) { 
      string strConString = ConfigurationManager.ConnectionStrings["SOConnectionString"].ConnectionString; 
      OleDbConnection conn = new OleDbConnection(strConString); 
      OleDbCommand cmd = new OleDbCommand("SELECT * FROM USERMASTER", conn); 
      cmd.Fill(dataset1); 
      conn.Open(); 
      OleDbDataReader dr; 
     } 
    } 
} 

我想知道爲什麼cmd.Fill(dataset1);不能使用。它告訴我要添加system.data.oledb.command,但是當我添加問題仍然是一樣的。 任何人都可以幫忙嗎?

+0

你可以叫'con.Open()''之前fill' –

回答

0

試試這個:

DataTable dataTable = new DataTable("USERMASTER"); 
using (var conn = new OleDbConnection(sConnectionString)) 
{ 
    conn.Open(); 
    var queryString = "SELECT * FROM USERMASTER"; 
    using (var comm = new OleDbCommand(queryString, conn)) 
    { 
     using (OleDbDataAdapter da = new OleDbDataAdapter (comm)) 
     { 
      da.Fill(dataTable); 
      return dataTable; 
     } 
    } 
    conn.Close(); 
}