执行存储过程并在C#中返回数据表的方法

时间:2012-01-21 03:31:27

标签: c# vb.net stored-procedures datatable

我有一些VB代码,如

  Dim data As DataTable = DataAccess.ExecuteDataSet("AuthenticateWebServiceClient" _
                                                , New SqlParameter("@ClientID", ClientId) _
                                                , New SqlParameter("@Password", Password) _
                                                , New SqlParameter("@WebServiceID", WebServiceID)).Tables(0)

具有此功能的DataAccess类

 Public Shared Function ExecuteDataSet(ByVal storedProcName As String, ByVal ParamArray parameters As SqlParameter()) As DataSet
        'Try
        Dim command As SqlCommand = PrepareSPCommand(storedProcName, parameters)
        Dim adapter As New SqlDataAdapter(command)
        Dim ds As New DataSet
        adapter.Fill(ds)
        Return ds
        'Catch ex As Exception
        'ex.ToString()
        'End Try
    End Function

当我尝试通过转换器将其转换为C#时,我得到

private DataSet ExecuteDataSet(string storedProcName, SqlParameter[] parameters)
{
   SqlCommand command = PrepareSPCommand(storedProcName, parameters);
   SqlDataAdapter adapter = new SqlDataAdapter(command);
   DataSet ds = new DataSet();
   adapter.Fill(ds);
   return ds;
}

除了PrepareSPCommand不被VS识别。有谁知道将此函数转换为C#的正确方法。

2 个答案:

答案 0 :(得分:2)

下面,

public SqlCommand PrepareSPCommand(string storedProcName, SqlParameter[] parameters, string connectionString){
    SqlCommand command = new SqlCommand(storedProcName,new SqlConnection(connectionString));
    command.CommandType=CommandType.StoredProcedure;
    command.Parameters.AddRange(parameters);

    return command;
}

答案 1 :(得分:0)

不确定。与VB.net相比,C#没有任何神秘感。

DataTable data = DataAccess.ExecuteDataSet("AuthenticateWebServiceClient", New SqlParameter("..."), etc).Tables[0];