2012-11-28 28 views
-7

Possible Duplicate:
Stored procedure return into DataSet in C# .Net調用存儲過程中錯誤使用C#

我如何可以調用使用C#我的項目的存儲過程SQL服務器?我需要將該存儲過程的輸出轉換爲數據表,但出現錯誤。

代碼:

private string connectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=blablalbla;"; 
    private DataTable dtProductPrijsActueel = new DataTable(); 
    public DataTable productPrijsActueel() 
    { 
     SqlConnection conn = new SqlConnection(connectionString); 
     conn.Open(); 
     SqlCommand comm = conn.CreateCommand(); 
     comm.CommandType = CommandType.StoredProcedure; 
     comm.CommandText = "procSelectPrijsActueel"; 

     dtProductPrijsActueel.Load(comm.ExecuteReader()); 
     conn.Close(); 
     conn.Dispose(); 

     return dtProductPrijsActueel; 
    } 

錯誤:

ERROR : KEYWORD NOT SUPPORTED : 'PROVIDER' 
+1

您需要向我們展示您到目前爲止所嘗試的內容。 –

+1

通常的方法,您只需將'SqlCommand'對象的'CommandType'屬性設置爲'StoredProcedure'。 – user17753

+0

周圍有很多文檔,您可以先嚐試搜索,然後在出現問題時再回到特定的代碼/問題。 – Andrew

回答

1

這是真的東西應該剛剛被GOOGLE了,但你要找長相一般的代碼像這樣:

string connect = System.Configuration.ConfigurationManager.AppSettings["conn"]; 
    SqlConnection connection = new SqlConnection(connect); 
    string spName = "TheSpName"; 

    SqlCommand spCmd = new SqlCommand(spName, connection); 
    spCmd.CommandType = CommandType.StoredProcedure; 

    spCmd.Parameters.Add("@SomeParam", SqlDbType.String).Value = "Some Parameter"; 

    connection.Open(); 

    var dataTableReader = spCmd.ExecuteReader();