请参阅.net企业级范例duwamish的相关代码,duwamish的安装程序在.net安装目录下可以找到

解决方案 »

  1.   

    不知道你要做怎么样的封装。简单的封装:Select单独封装,返回一个DataTable. 其他的都封装在一起,返回integer。串行连接数据库的话,这样封装也可满足事务操作的要求,较简单。复杂的话,建议参考下SqlHelper之类的结构。更狠的,就做个数据持久层,像NHibernate的。
      

  2.   

    简单封装倒是可以写一段给你参考下。本人也是菜鸟:)    Public Function CoOraSelect(ByVal strSql As String, ByRef oraDataTable As DataTable) As Integer
            Dim oraDataAdapter As OracleDataAdapter
            Dim oraComm As OracleCommand
            Dim ret As Integer
            Try
                ret = 0
                oraComm = oraConn.CreateCommand()
                oraComm.CommandText = strSql
                If Not oraTrans Is Nothing Then
                    oraComm.Transaction = oraTrans
                End If
                oraDataAdapter = New OracleDataAdapter(oraComm)
                oraDataTable = New DataTable
                oraDataAdapter.Fill(oraDataTable)
                If oraDataTable.Rows.Count <= 0 Then
                    ret = -9
                End If
                oraDataAdapter.Dispose()
            Catch ex As Exception
                m_OraException = ex
                ret = -1
            End Try
            Return ret
        End Function
      

  3.   

    一个单件模式的数据库操作类(可以避免使用多线程时产生多个连接而导致数据库死锁)
    class DbOperator
    {
    private SqlConnection conn_;
    private SqlCommand sqlCommand_;
    private SqlDataReader reader_;
    public DbOperator(string ConnStr)
    {
    conn_ = new SqlConnection(ConnStr);
    conn_.Open();
    sqlCommand_ = conn_.CreateCommand();
    }

    public SqlDataReader execQueryReader(string sql)
    {
    sqlCommand_.CommandText = sql;
    try
    {
    reader_ = sqlCommand_.ExecuteReader();
    }
    catch(SqlException D)
    {
    StreamWriter file = new StreamWriter(Perty["ErrorFile"].ToString(),true);
    file.WriteLine(D.Message.ToString());
    file.Flush();
    file.Close();
    }
    return reader_;
    }
    public void execNoQuery(string sql)
    {
    sqlCommand_.CommandText = sql;
    try
    {
    sqlCommand_.ExecuteNonQuery();
    }
    catch(SqlException D)
    {
    StreamWriter file = new StreamWriter(Perty["ErrorFile"].ToString(),true);
    file.WriteLine(D.Message.ToString());
    file.Flush();
    file.Close();
    }
    }
    public void closeReader()
    {
    if (reader_ != null)
    {
    reader_.Close();
    }
    }
    public void dbclose()
    {
    conn_.Close();
    }
    }调用方式
    DbOperator db = new DbOperator(ConnStr);db.execNoQuery("sql语句");//不需要返回结果的操作SqlDataReader reader = db.execQueryReader("sql语句");//需要返回结果的操作