Implement HOW TO: ??? ????? ???????? SqlParameter ?? ????? ?? ????? ?? ???? IN

?????? ????????? ?????? ?????????
???? ???????: 555167 - ??? ???????? ???? ????? ????? ??? ???????.
Author: William Ryan MVP
????? ???? | ?? ????

???????

????? ????? ?? ??? ?????? ?????? ?????? ????, ??? ????? SQL ?? ????? ???? ???? ????????? ??????? ?? ????? IN. ? ???? ??? ??????? ?? ??? ??????? ??? ????? SqlParameter.

?????

???? ??? ???? ??? ???? ?? ?????? ADO.NET ???? ?????? beneficial ???????? ?? ??? ??????-??? ?????? ????? ??????? ?? ??? ?????? ????? ?? ???? ???? ????. ??? ??? ????? @ SomeValue 1,2,3,4,5 = and ????????? ?? SELECT * FROM [TableName] WHERE ?? ???? ?? ????? [ColumnName] IN @ SomeValue ?? ????? ???????? ?? [ColumnName] ???? ???? ????? 1,2,3,4 ?? 5. ????? ????? ?? ???? ?????? ??? ?? ????? ???? ?? ?????? " 1,2,3,4,5 ". ???? ??? ??? ????? ???????:
<CODE>
????? CREATE PROCEDURE up_ProcedureHelper
     @ VARCHAR AS ??????? (50)--?? ??? ???? ??? ?????
AS
SELECT * FROM [TableName]
WHERE IN [FieldName] (@ Tokens)
</CODE>
??? ???? ??? ?????:
<CODE>
????? cs = ConfigurationSettings.AppSettings("ConnectString"));
using(SqlConnection cn = new SqlConnection(cs)){
      cmd SqlCommand = SqlCommand ????? (" up_ProcedureHelper ", ConnectionName);
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("@Token", "1,2,3,4,5);
      cn.Open () ? reality //In Connection.Open ? ExecuteReader ???? ??? ????? ?? Try / Catch / Finally
      dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);    
      Debug.Assert (dr.HasRows, " We ??? ?? ???? ???? ???? ??? ") ? //Fails ??? ColumnName ???? ?? ??? ???????? " 1,2,3,4,5 " ???? ??? ????

}

????

????? ????? ???? ????? ?? Accommodate Methodology ?????
  ???? ????? ???? ??? ??? ??????? ?? ?????? ???? ????? ???????? ???? ?????? ?????? Sql Server 2000 's. ??? ?????? ???? ????? ?????? ???? ??????? ??? ?? ?????? ????? ?? ?????? ???? ????. ???? ??? ?? ??????? ???? ??????, ??? ????? ?? ????? ?? ?????? ??????. ???????? ??? ????????? ???? ???? ?????? ?? ????? ?? ?????? LIKE :
<CODE>
????? CREATE PROCEDURE up_ProcedureHelper
     @ VARCHAR AS ??????? (50)--?? ??? ???? ??? ?????
AS
@ VARCHAR(50) AS Temp DECLARE
????? CREATE TABLE HOLDER(TokenValues VARCHAR(50)) #
WHILE LEN(@Tokens) > 0
    BEGIN
       IF CHARINDEX(',', @Tokens) = 0
          BEGIN
             SET @TEMP @Tokens =  
             SET @Tokens = ''
             INSERT INTO VALUES(@Temp) #Holder (TokenValues)
          END
       ELSE
          BEGIN
             SET @Temp = LEFT (@Tokens, CHARINDEX(',', @Tokens)-1)     
             INSERT INTO VALUES(@Temp) #Holder (TokenValues)
             SET @Tokens = ????? (@Tokens, LEN(@Tokens)-LEN(@Temp)-1)
       END
    END
SELECT * FROM [TableName]
WHERE IN [FieldName] (TokenValues SELECT FROM #Holder)
</CODE>
 
???? ?? ??????? " 1,2,3,4,5 " ? ???? ??? ????? ???? Temp ???? #Holder ?? ?????? 5 ? ????? 1,2,3,4 & 5 ??? ???????. ??? ?????? ??? ????? ????? ??? ????? ???? ?? ??????? ?? ??? ??? ?????. ???? ??? ?????? ???? ????? ?????? ????????. ?? ??????? ????? ??? ?????? imperative ?? ????? ?????? ??????? connections. ???? ?? ???? ??? ?? ??? ??? ? ????? ?? ?? ???? ??? ??????? ??????? ??????? ??? finally??? ?? ????????? ?? ??????? .Close() ????? ?? ????. ??? ??? ?????? C# , ????? ?? ??? ??????? ???????? ??????? ???????? ?? ??????? finally ??? ?? ???? ?? ?? SqlConnection?? closed ????? disposed ?? (???? ??? ??? ?????? ?? ???? ????? ????? ????).
<CODE>
????? cs = ConfigurationSettings.AppSettings("ConnectString"));
using(SqlConnection cn = new SqlConnection(cs)){
      cmd SqlCommand = SqlCommand ????? (" up_ProcedureHelper ", ConnectionName);
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("@Token", "1,2,3,4,5");
      cn.Open () ? reality //In Connection.Open ? ExecuteReader ???? ??? ????? ?? Try / Catch / Finally
      dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);    
      Debug.Assert(dr.HasRows, "We should have Rows but Don't") //Now ???? ???????-; ? ??? ??? ?????? 5.
}
</CODE>

??????? ????

Calling ???????? ??????? ?????? ?? SQL Server
?????? ??? ?? Dynamic Sql ?? ????? ?? ?? ???
?????? ???? ??? ??? ??? Parameterize  ???????????
????? ????? ?????? ?????? SQL ??? ??????? Parameterized ???????????

???????

???? ???????: 555167 - ????? ??? ??????: 07/????? ???????/1425 - ??????: 1.0
????? ???
  • Microsoft ADO.NET 1.1
  • Microsoft ADO.NET 1.0
????? ??????: 
kbpubtypecca kbpubmvp kbhowto KB555167 KbMtar kbmt
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????. ??? ?? ???? ??? ??????? ????? ?? ???????? ?? ??? ???????? ?????? ????? ????????? ??????? ???? ??? ???????
???? ??? ????? ??????? ?????? ??????????555167
COMMUNITY SOLUTIONS CONTENT DISCLAIMER
MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com