当前位置:编程学习 > C#/ASP.NET >>

求助asp.net瀑布流怎么和数据库对接!!!C#

如题,我下了网上的代码,但是怎么和数据里面对接起来呢(C#)?新手请教。。
最好有实例发我一份,谢谢!
97242689@qq.com asp.net 瀑布流 --------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public string connectionString
{
    get;
    private set;
}

public DBOperator(string connectionString)
{
    this.connectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
}


public DataTable GetDataTable(SqlCommand sqlCommand)
{
    ChangeNullToDBNullValue(sqlCommand);
    bool useDefaultConnection = false;
    if (sqlCommand.Connection == null)
    {
        useDefaultConnection = true;
        sqlCommand.Connection = new SqlConnection(this.connectionString);
    }
    else
    {
        useDefaultConnection = false;
        if (sqlCommand.Connection.State != ConnectionState.Closed)
        {
            throw new ArgumentException("SqlCommand's connection state must be closed.");
        }
    }

    sqlCommand.Connection.Open();
    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
    DataTable dt = new DataTable();
    da.SelectCommand = sqlCommand;
    try
    {
        da.Fill(dt);
    }
    catch (Exception ex)
    {
        DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
    }
    sqlCommand.Connection.Close();
    return dt;
}

public ExecuteResult ExecuteNonQuery(SqlCommand sqlCommand)
{
    ChangeNullToDBNullValue(sqlCommand);
    bool useDefaultConnection = false;
    if (sqlCommand.Connection == null)
    {
        useDefaultConnection = true;
        sqlCommand.Connection = new SqlConnection(this.connectionString);
    }
    else
    {
        useDefaultConnection = false;
        if (sqlCommand.Connection.State != ConnectionState.Closed)
        {
            throw new ArgumentException("SqlCommand's connection state must be closed.");
        }
    }
    sqlCommand.Connection.Open();
    sqlCommand.Transaction = sqlCommand.Connection.BeginTransaction();
    try
    {
        int rows = sqlCommand.ExecuteNonQuery();
        sqlCommand.Transaction.Commit();
        return new ExecuteResult() { ActionStatus = ActionStatusType.Success, ReturnValue = rows };
    }
    catch (SqlException ex)
    {
        sqlCommand.Transaction.Rollback();
        DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
        return new ExecuteResult() { ActionStatus = ActionStatusType.Fail, Message = ex.Message };
    }
}

public ExecuteResult ExecuteScalar(SqlCommand sqlCommand)
{
    ChangeNullToDBNullValue(sqlCommand);
    bool useDefaultConnection = false;
    if (sqlCommand.Connection == null)
    {
        useDefaultConnection = true;
        sqlCommand.Connection = new SqlConnection(this.connectionString);
    }
    else
    {
        useDefaultConnection = false;
        if (sqlCommand.Connection.State != ConnectionState.Closed)
        {
            throw new ArgumentException("SqlCommand's connection state must be closed.");
        }
    }
    sqlCommand.Connection.Open();
    sqlCommand.Transaction = sqlCommand.Connection.BeginTransaction();
    try
    {
        object returnValue = sqlCommand.ExecuteScalar();
        sqlCommand.Transaction.Commit();
        return new ExecuteResult() { ActionStatus = ActionStatusType.Success, ReturnValue = returnValue };
    }
    catch (SqlException ex)
    {
        sqlCommand.Transaction.Rollback();
        DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
        return new ExecuteResult() { ActionStatus = ActionStatusType.Fail, Message = ex.Message };
    }
}


private void ChangeNullToDBNullValue(SqlCommand command)
{
    foreach (SqlParameter para in command.Parameters)
    {
        if (para.Value == null)
        {
            para.Value = DBNull.Value;
        }
        else
        {
            if (para.Value is string)
            {
                if (string.IsNullOrWhiteSpace(para.Value.ToString()))
                {
                    para.Value = string.Empty;
                }
            }
        }
    }
}

--------------------编程问答--------------------
引用 1 楼 hdhai9451 的回复:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

public string connectionString
{
    get;
    private set;
}

public DBOperator(string connectionString)
{
    this.connectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
}


public DataTable GetDataTable(SqlCommand sqlCommand)
{
    ChangeNullToDBNullValue(sqlCommand);
    bool useDefaultConnection = false;
    if (sqlCommand.Connection == null)
    {
        useDefaultConnection = true;
        sqlCommand.Connection = new SqlConnection(this.connectionString);
    }
    else
    {
        useDefaultConnection = false;
        if (sqlCommand.Connection.State != ConnectionState.Closed)
        {
            throw new ArgumentException("SqlCommand's connection state must be closed.");
        }
    }

    sqlCommand.Connection.Open();
    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
    DataTable dt = new DataTable();
    da.SelectCommand = sqlCommand;
    try
    {
        da.Fill(dt);
    }
    catch (Exception ex)
    {
        DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
    }
    sqlCommand.Connection.Close();
    return dt;
}

public ExecuteResult ExecuteNonQuery(SqlCommand sqlCommand)
{
    ChangeNullToDBNullValue(sqlCommand);
    bool useDefaultConnection = false;
    if (sqlCommand.Connection == null)
    {
        useDefaultConnection = true;
        sqlCommand.Connection = new SqlConnection(this.connectionString);
    }
    else
    {
        useDefaultConnection = false;
        if (sqlCommand.Connection.State != ConnectionState.Closed)
        {
            throw new ArgumentException("SqlCommand's connection state must be closed.");
        }
    }
    sqlCommand.Connection.Open();
    sqlCommand.Transaction = sqlCommand.Connection.BeginTransaction();
    try
    {
        int rows = sqlCommand.ExecuteNonQuery();
        sqlCommand.Transaction.Commit();
        return new ExecuteResult() { ActionStatus = ActionStatusType.Success, ReturnValue = rows };
    }
    catch (SqlException ex)
    {
        sqlCommand.Transaction.Rollback();
        DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
        return new ExecuteResult() { ActionStatus = ActionStatusType.Fail, Message = ex.Message };
    }
}

public ExecuteResult ExecuteScalar(SqlCommand sqlCommand)
{
    ChangeNullToDBNullValue(sqlCommand);
    bool useDefaultConnection = false;
    if (sqlCommand.Connection == null)
    {
        useDefaultConnection = true;
        sqlCommand.Connection = new SqlConnection(this.connectionString);
    }
    else
    {
        useDefaultConnection = false;
        if (sqlCommand.Connection.State != ConnectionState.Closed)
        {
            throw new ArgumentException("SqlCommand's connection state must be closed.");
        }
    }
    sqlCommand.Connection.Open();
    sqlCommand.Transaction = sqlCommand.Connection.BeginTransaction();
    try
    {
        object returnValue = sqlCommand.ExecuteScalar();
        sqlCommand.Transaction.Commit();
        return new ExecuteResult() { ActionStatus = ActionStatusType.Success, ReturnValue = returnValue };
    }
    catch (SqlException ex)
    {
        sqlCommand.Transaction.Rollback();
        DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
        return new ExecuteResult() { ActionStatus = ActionStatusType.Fail, Message = ex.Message };
    }
}


private void ChangeNullToDBNullValue(SqlCommand command)
{
    foreach (SqlParameter para in command.Parameters)
    {
        if (para.Value == null)
        {
            para.Value = DBNull.Value;
        }
        else
        {
            if (para.Value is string)
            {
                if (string.IsNullOrWhiteSpace(para.Value.ToString()))
                {
                    para.Value = string.Empty;
                }
            }
        }
    }
}


朋友 可以帮忙注释下么!!?
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,