任何体面的ADO.NET Helper都可以在那里使用?

时间:2009-08-03 09:27:07

标签: c# .net ado.net

我正在寻找一些不错的ADO.NET帮助程序实用程序类,以便我可以查询ado.net数据源。我不是在寻找任何过于花哨的东西,但它必须支持交易。那里有什么东西吗?

P.S。我知道数据访问块会这样做,但我正在寻找一些比其他组件更独立的东西。像一个简单的图书馆或什么

4 个答案:

答案 0 :(得分:4)

这取决于你对“没有什么花哨”和“简单”的定义,但是BLToolkit做了相当不错的工作来抽象你不想手动编写的样板ADO.NET代码。例如,

public abstract class PersonAccessor : DataAccessor<Person>
{
    [SqlQuery("SELECT * FROM Person WHERE PersonID = @id")]
    public abstract Person GetPersonByID(int @id);
}

Person person = DataAccessor.CreateInstance<PersonAccessor>.
    GetPersonByID(2);

只会从数据库中获取Person对象 - 看到了吗? - 5-6行代码。

关于DataSet s,here's如何:

        using (DbManager db = new DbManager())
        {
            DataSet ds = db
                .SetCommand("SELECT * FROM Person")
                .ExecuteDataSet();

            Assert.AreNotEqual(0, ds.Tables[0].Rows.Count);
        }

添加参数就像在DbManager上调用方法一样简单。

你不应该真的害怕ORM。

答案 1 :(得分:2)

如果它只是一个轻包装器为什么不自己包装命令?

例如:

    /// <summary>
    /// Executes a stored procedure or query, returns the number of rows effected.
    /// </summary>
    /// <param name="commandText"></param>
    /// <param name="commandType"></param>
    /// <param name="sqlParameters"></param>
    /// <param name="sqlTransaction"></param>
    /// <returns></returns>
    public static int ExecuteQuery(string commandText, CommandType commandType, List<SqlParameter> sqlParameters, SqlTransaction sqlTransaction)
    {
        if (sqlTransaction == null)
        {
            using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString()))
            {
                sqlConnection.Open();
                using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
                {
                    sqlCommand.CommandType = commandType;
                    sqlCommand.CommandText = commandText;
                    if (sqlParameters != null)
                    {
                        foreach (SqlParameter sqlParameter in sqlParameters)
                        {
                            sqlCommand.Parameters.Add(sqlParameter);
                        }
                    }
                    return sqlCommand.ExecuteNonQuery();
                }
            }
        }
        else
        {
            SqlCommand sqlCommand = new SqlCommand(commandText, sqlTransaction.Connection, sqlTransaction);
            sqlCommand.CommandType = commandType;
            foreach (SqlParameter sqlParameter in sqlParameters)
            {
                sqlCommand.Parameters.Add(sqlParameter);
            }
            return sqlCommand.ExecuteNonQuery();
        }
    }

答案 2 :(得分:1)

答案 3 :(得分:0)

public class ADOUtility
{
    private string _connectionstring;
    private SqlConnection DbConn;
    private bool DBStatus;

    public int CmdTimeOut;
    public string ConnectionString
    {
        set { _connectionstring = value; }
    }


    public ADOUtility(string connstr)
    {
        _connectionstring = connstr;
        CmdTimeOut = -1;
        DBStatus = false;
    }

    public ADOUtility()
    {
        DBStatus = false;
        CmdTimeOut = -1;
    }

    /// <summary>Create and opens a new connection </summary>
    public void Open()
    {
        try
        {
            DbConn = new SqlConnection();
            DbConn.ConnectionString = _connectionstring;
            DbConn.Open();
            DBStatus = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void IsConnOpen()
    {
        try
        {
            if (DbConn.State == ConnectionState.Closed)
            {
                Open();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


    public Boolean IsOpen()
    {
        return DBStatus;
    }


    public void Close()
    {
        if (IsOpen())
        {
            DbConn.Close();
            DbConn.Dispose();
            DBStatus = false;
        }
    }


    public Int16 Insert(string SqlQuery, params SqlParameter[] SqlParam)
    {
        IsConnOpen();
        Int16 newProdID = 0;
        SqlCommand cmd = default(SqlCommand);

        SqlQuery += ";SELECT CAST(SCOPE_IDENTITY() AS int);";
        cmd = new SqlCommand(SqlQuery, DbConn);

        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }

        foreach (SqlParameter param in SqlParam)
        {
            cmd.Parameters.Add(param);
        }

        newProdID = Convert.ToInt16(cmd.ExecuteScalar());

        return newProdID;
    }


    public Int64 Execute(string SqlQuery)
    {
        IsConnOpen();
        Int64 newProdID = 0;
        SqlCommand cmd = new SqlCommand(SqlQuery, DbConn);
        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }

        newProdID = Convert.ToInt64(cmd.ExecuteNonQuery());

        return newProdID;
    }


    public Int64 Execute(string SqlQuery, params SqlParameter[] SqlParam)
    {
        IsConnOpen();
        SqlCommand cmd = new SqlCommand(SqlQuery, DbConn);
        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }

        foreach (SqlParameter param in SqlParam)
        {
            cmd.Parameters.Add(param);
        }

        return Convert.ToInt64(cmd.ExecuteNonQuery());
    }


    public object SP_Execute(string SpName, params SqlParameter[] SqlParam)
    {
        IsConnOpen();
        SqlCommand cmd = new SqlCommand(SpName, DbConn);
        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }
        cmd.CommandType = CommandType.StoredProcedure;

        foreach (SqlParameter param in SqlParam)
        {
            cmd.Parameters.Add(param);
        }

        return cmd.ExecuteScalar();
    }


    public object SelectColumn(string SqlQuery, params SqlParameter[] SqlParam)
    {
        IsConnOpen();
        SqlCommand cmd = new SqlCommand(SqlQuery, DbConn);
        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }
        cmd.CommandType = CommandType.Text;

        foreach (SqlParameter param in SqlParam)
        {
            cmd.Parameters.Add(param);
        }

        return cmd.ExecuteScalar();
    }


    public object SelectColumn(string tablename, string columnname, string condition)
    {
        IsConnOpen();
        if (!string.IsNullOrEmpty(condition))
        {
            condition = "where " + condition;
        }

        string query = null;
        query = "select " + columnname + " from " + tablename + " " + condition;
        SqlCommand cmd = new SqlCommand(query, DbConn);
        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet dbtable = new DataSet();

        adapter.Fill(dbtable, tablename);

        if (dbtable.Tables[tablename].Rows.Count > 0)
        {
            return dbtable.Tables[tablename].Rows[0][0];
        }
        else
        {
            return "no_record_found";
        }
    }


    public DataTable Select_Table(string SqlQuery)
    {
        IsConnOpen();
        SqlCommand cmd = new SqlCommand(SqlQuery, DbConn);
        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }

        SqlDataAdapter adapter = new SqlDataAdapter(cmd);

        DataSet dbtable = new DataSet();
        adapter.Fill(dbtable, "recordset");

        return dbtable.Tables["recordset"];

    }


    public DataTable Select_Table(string SqlQuery, params SqlParameter[] SqlParam)
    {
        IsConnOpen();
        SqlCommand cmd = new SqlCommand(SqlQuery, DbConn);
        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);

        foreach (SqlParameter param in SqlParam)
        {
            cmd.Parameters.Add(param);
        }

        DataSet dbtable = new DataSet();
        adapter.Fill(dbtable, "recordset");

        return dbtable.Tables["recordset"];

    }

    public int Select_Row(string SqlQuery, ref DataRow RowData, params SqlParameter[] SqlParam)
    {
        IsConnOpen();
        SqlCommand cmd = new SqlCommand(SqlQuery, DbConn);
        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);

        foreach (SqlParameter param in SqlParam)
        {
            cmd.Parameters.Add(param);
        }

        DataSet dbtable = new DataSet();
        adapter.Fill(dbtable, "recordset");

        if (dbtable.Tables["recordset"].Rows.Count > 0)
        {
            RowData = dbtable.Tables["recordset"].Rows[0];

            return dbtable.Tables["recordset"].Rows.Count;
        }
        else
        {
            RowData = null;
            return 0;
        }
    }



    public DataTable SP_Execute_Table(string SpName, params SqlParameter[] SqlParam)
    {
        IsConnOpen();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = SpName;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = DbConn;
        if (CmdTimeOut >= 0)
        {
            cmd.CommandTimeout = CmdTimeOut;
        }

        foreach (SqlParameter param in SqlParam)
        {
            cmd.Parameters.Add(param);
        }

        SqlDataAdapter adapter = new SqlDataAdapter(cmd);

        DataSet dbtable = new DataSet();
        adapter.Fill(dbtable, "recordset");

        return dbtable.Tables["recordset"];
    }
}