c# - SqlParameter value or sqlValue? -



c# - SqlParameter value or sqlValue? -

i error saying this:

sqlparametercollection accepts non-null sqlparameter type objects, not string objects

on code:

.parameters.add(new sqlparameter("@username", sqldbtype.nvarchar,128).value = username);

if alter to:

.parameters.add(new sqlparameter("@username", sqldbtype.nvarchar,128).sqlvalue = username);

shouldn't value work?

is'nt sqlvalue database type?

here dal use:

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 { { homecoming cmd.commandtype; } set { cmd.commandtype = value; } } public idatareader executereader() { idatareader reader = null; seek { if (cmd.connection.state == connectionstate.closed) { this.open(); } reader = cmd.executereader(commandbehavior.closeconnection); } grab (exception ex) { if (handleerrors) strlasterror = ex.message; else throw; } homecoming reader; } public idatareader executereader(string commandtext) { idatareader reader = null; seek { cmd.commandtext = commandtext; reader = this.executereader(); } grab (exception ex) { if (handleerrors) strlasterror = ex.message; else throw; } homecoming reader; } public object executescalar() { object obj = null; seek { if (cmd.connection.state == connectionstate.closed) { this.open(); } obj = cmd.executescalar(); } grab (exception ex) { if (handleerrors) strlasterror = ex.message; else throw; } homecoming obj; } public object executescalar(string commandtext) { object obj = null; seek { cmd.commandtext = commandtext; obj = this.executescalar(); } grab (exception ex) { if (handleerrors) strlasterror = ex.message; else throw; } homecoming obj; } public int executenonquery() { int = -1; seek { if (cmd.connection.state == connectionstate.closed) { this.open(); } = cmd.executenonquery(); } grab (exception ex) { if (handleerrors) strlasterror = ex.message; else throw; } homecoming i; } public int executenonquery(string commandtext) { int = -1; seek { cmd.commandtext = commandtext; = this.executenonquery(); } grab (exception ex) { if (handleerrors) strlasterror = ex.message; else throw; } homecoming i; } public dataset executedataset() { sqldataadapter da = null; dataset ds = null; seek { if (cmd.connection.state == connectionstate.closed) { this.open(); } da = new sqldataadapter(); da.selectcommand = (sqlcommand)cmd; ds = new dataset(); da.fill(ds); } grab (exception ex) { if (handleerrors) strlasterror = ex.message; else throw; } homecoming ds; } public dataset executedataset(string commandtext) { dataset ds = null; seek { cmd.commandtext = commandtext; ds = this.executedataset(); } grab (exception ex) { if (handleerrors) strlasterror = ex.message; else throw; } homecoming ds; } public int commandtimeout { { homecoming cmd.commandtimeout; } set { cmd.commandtimeout = value; } } public idbconnection connection { { homecoming cmd.connection; } set { cmd.connection = value; } } public string commandtext { { homecoming cmd.commandtext; } set { cmd.commandtext = value; cmd.parameters.clear(); } } public idataparametercollection parameters { { homecoming cmd.parameters; } } public idbtransaction transaction { { homecoming 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; homecoming tran; } public void committransaction() { cmd.transaction.commit(); } public void rollbacktransaction() { cmd.transaction.rollback(); } public system.data.connectionstate state { { homecoming cmd.connection.state; } } public string connectionstring { { homecoming strconnectionstring; } set { strconnectionstring = value; } } private void open() { cmd.connection.open(); } public bool handleexceptions { { homecoming handleerrors; } set { handleerrors = value; } } public string lasterror { { homecoming strlasterror; } } public void dispose() { cmd.connection.close(); cmd.connection.dispose(); cmd.dispose(); } }

actually sec line of code won't work, because both

new sqlparameter("@username", sqldbtype.nvarchar,128).value = username

and

new sqlparameter("@username", sqldbtype.nvarchar,128).sqlvalue = username

are expressions evaluate string object, , error telling sqlparametercollection not take string objects.

a neater way add together parameters values this:

.parameters.add("@username", sqldbtype.nvarchar, 128).value = username;

this possible because add returns sqlparameter object can set value property on same line.

c# asp.net sql-server

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -