質問

Considering this example:

MyParameters.Name = "Paul";
MyParameters.Nickname = String.Empty;

ExecuteStoredProcedure(MyStoredProcedure, MyParameters);

When my stored procedure executes it will run something like this?

MyStoredProcedure 'Paul',''

So, my question is: C# String.Empty is equal to Database ''?

PS.: I'm using SQL Server

役に立ちましたか?

解決

Yes, an empty string is sent as an empty string. See, the values are translated and sent as expected. For example, if you needed to send null then you'd set Nickname like this:

MyParameters.Nickname = null;

Your code is also equivalent to:

MyParameters.Nickname = "";

他のヒント

String.Empty is semantically identical to "" in C#, which is a zero-length non-null string (whether it is entirely identical is more complicated, and depends on exactly what runtime version you are using). In SQL, '' is a zero-length non-null string. So yes, string.Empty, String.Empty and "" are all logically equivalent to ''.

As above said, String.Empty is same "".

Another option, declare parameter as null or "" in stored procedure like..

    create procedure spname 
{  
      parameter1 int = null, parameter2 varchar(50) = null, parameter3 varchar(50) = ""   
} 

as

begin

.....

end

While passing parameter from c# , if you not pass , then it will automatically taken blank("") or null as you declare of right side of parameter

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top