我收到这样的错误消息:


  SqlParameterCollection仅接受非null的SqlParameter类型对象,而不接受String对象


在此代码上:

.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar,128).Value = username);


如果我将其更改为:

.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar,128).SqlValue = username);


不应该只重视工作吗?

sqlValue不是数据库类型吗?

这是我使用的DAL:

public class DBAccess : IDisposable
{

    private IDbCommand cmd = new SqlCommand();
    private string strConnectionString = "";
    private bool handleErrors = false;
    private string strLastError = "";

    public DBAccess()
    {
        strConnectionString = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
        SqlConnection cnn = new SqlConnection();
        cnn.ConnectionString = strConnectionString;
        cmd.Connection = cnn;
        cmd.CommandType = CommandType.StoredProcedure;
    }


    public CommandType CommandType
    {
        get
        {
            return cmd.CommandType;
        }
        set
        {
            cmd.CommandType = value;
        }
    }

    public IDataReader ExecuteReader()
    {
        IDataReader reader = null;
        try
        {
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                this.Open();
            }
            reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception ex)
        {
            if (handleErrors)
                strLastError = ex.Message;
            else
                throw;
        }
        return reader;
    }

    public IDataReader ExecuteReader(string commandtext)
    {
        IDataReader reader = null;
        try
        {
            cmd.CommandText = commandtext;
            reader = this.ExecuteReader();
        }
        catch (Exception ex)
        {
            if (handleErrors)
                strLastError = ex.Message;
            else
                throw;
        }

        return reader;
    }

    public object ExecuteScalar()
    {
        object obj = null;
        try
        {
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                this.Open();
            }
            obj = cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {
            if (handleErrors)
                strLastError = ex.Message;
            else
                throw;
        }

        return obj;
    }

    public object ExecuteScalar(string commandtext)
    {
        object obj = null;
        try
        {
            cmd.CommandText = commandtext;
            obj = this.ExecuteScalar();
        }
        catch (Exception ex)
        {
            if (handleErrors)
                strLastError = ex.Message;
            else
                throw;
        }

        return obj;
    }

    public int ExecuteNonQuery()
    {
        int i = -1;
        try
        {
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                this.Open();
            }
            i = cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            if (handleErrors)
                strLastError = ex.Message;
            else
                throw;
        }

        return i;
    }


    public int ExecuteNonQuery(string commandtext)
    {
        int i = -1;
        try
        {
            cmd.CommandText = commandtext;
            i = this.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            if (handleErrors)
                strLastError = ex.Message;
            else
                throw;
        }

        return i;
    }


    public DataSet ExecuteDataSet()
    {
        SqlDataAdapter da = null;
        DataSet ds = null;
        try
        {
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                this.Open();
            }
            da = new SqlDataAdapter();
            da.SelectCommand = (SqlCommand)cmd;
            ds = new DataSet();
            da.Fill(ds);
        }
        catch (Exception ex)
        {
            if (handleErrors)
                strLastError = ex.Message;
            else
                throw;
        }

        return ds;
    }


    public DataSet ExecuteDataSet(string commandtext)
    {
        DataSet ds = null;
        try
        {
            cmd.CommandText = commandtext;
            ds = this.ExecuteDataSet();
        }
        catch (Exception ex)
        {
            if (handleErrors)
                strLastError = ex.Message;
            else
                throw;
        }

        return ds;
    }

    public int CommandTimeout
    {
        get
        {
            return cmd.CommandTimeout;
        }
        set
        {
            cmd.CommandTimeout = value;
        }
    }

    public IDbConnection Connection
    {
        get
        {
            return cmd.Connection;
        }
        set
        {
            cmd.Connection = value;
        }
    }

    public string CommandText
    {
        get
        {
            return cmd.CommandText;
        }
        set
        {
            cmd.CommandText = value;
            cmd.Parameters.Clear();
        }
    }

    public IDataParameterCollection Parameters
    {
        get
        {
            return cmd.Parameters;
        }
    }

    public IDbTransaction Transaction
    {
        get
        {
            return cmd.Transaction;
        }
        set
        {
            cmd.Transaction = value;
        }
    }

    public void AddParameter(string paramname, object paramvalue)
    {
        var param = new SqlParameter(paramname, paramvalue);
        cmd.Parameters.Add(param);
    }

    public void AddParameter(IDataParameter param)
    {
        cmd.Parameters.Add(param);
    }


    public IDbTransaction BeginTransaction()
    {
        var tran = cmd.Connection.BeginTransaction();
        cmd.Transaction = tran;
        return tran;
    }

    public void CommitTransaction()
    {
        cmd.Transaction.Commit();
    }

    public void RollbackTransaction()
    {
        cmd.Transaction.Rollback();
    }

    public System.Data.ConnectionState State
    {
        get
        {
            return cmd.Connection.State;
        }
    }

    public string ConnectionString
    {
        get
        {
            return strConnectionString;
        }
        set
        {
            strConnectionString = value;
        }
    }

    private void Open()
    {
        cmd.Connection.Open();
    }

    public bool HandleExceptions
    {
        get
        {
            return handleErrors;
        }
        set
        {
            handleErrors = value;
        }
    }

    public string LastError
    {
        get
        {
            return strLastError;
        }
    }

    public void Dispose()
    {
        cmd.Connection.Close();
        cmd.Connection.Dispose();
        cmd.Dispose();
    }
}

最佳答案

这似乎解决了问题。

.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar, 128) { Value = username });

关于c# - SqlParameter值还是sqlValue?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26462543/

10-11 05:06