c# how do you return dataset from sqldatareader?

前端 未结 6 1233
[愿得一人]
[愿得一人] 2020-12-29 05:13

I have this in a public class:

SqlConnection myConnection = new SqlConnection(\"Data Source=hermes;database=qcvalues; Integrated Security=SSPI;\");
myConnect         


        
6条回答
  •  醉梦人生
    2020-12-29 05:40

    the below snippet works fine for me on SqlServer:

    public DataSet executeProcedure(String procudureName, params SqlParameter[] sqlParameters)
    {
        return executeSqlCommand(procudureName, CommandType.StoredProcedure, sqlParameters);
    }
    public DataSet executeSql(String commandText, params SqlParameter[] sqlParameters)
    {
        return executeSqlCommand(commandText, CommandType.Text, sqlParameters);
    }
    public DataSet executeSqlCommand(String commandText, CommandType Commandtype, params SqlParameter[] sqlParameters)
    {
        DataSet myset = new DataSet();
        using (var command = Database.GetDbConnection().CreateCommand())
        {
            command.CommandText = commandText;
            command.CommandType = Commandtype;
            foreach (var _kv in sqlParameters)
            {
                DbParameter _dbpara = command.CreateParameter();
                _dbpara.ParameterName = _kv.ParameterName;
                _dbpara.Value = _kv.Value;
                command.Parameters.Add(_dbpara);
            }
            Database.OpenConnection();
            DbDataAdapter adapter = DbProviderFactories.GetFactory(Database.GetDbConnection()).CreateDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(myset);
        }
        return myset;
    }
    

    the DbDataAdapter instance can be retrieved from DbProviderFactories class.

    DbDataAdapter adapter = DbProviderFactories.GetFactory(Database.GetDbConnection()).CreateDataAdapter();
    

提交回复
热议问题