SQL Parameterized ???????? ???? ODBC .NET ??????? ?? ????? C# .NET ?? ????? ???? ???????????? ???????? ????

???? ?????? ???? ??????
???? ID: 310130 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ??? ?? ??? ?? parameterized SQL Server ???????? ????????? ODBC .NET ???????? ??????? ?? ????? C# .NET ?? ????? ???? ??? ??? ???? ?? ??? ???? ????? ???

ODBC .NET ??????? ?? ????? ?? ???? parameterized ???????? ????????? ?? ????????? little SQL ?? OLE DB ??????? ?? ????? ?? ?? ?? ????????? ????????? ?? ????? ??, ?? ?? ?? ?????????? ????: ???????? ????????? ?????? ?? ??? ???????? ????????? ?? ??? ?? ???? ODBC ??? ???????? ?? ????? ????? ?? ??? ???????? ?? ???????? ??????? ?? ??? MSDN ????????? ??? ODBC ?????????? ?? ?????? ??? "????????? ???" ???? ?? ??????

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

  1. ???? ?? ????? ???????? expects ????????? ????? ??????? ??? ??? ???????? ???????? ????????? ?? ??? ??? ???????? ?? ?? ?????? ??:
    {CALL CustOrderHist (?)}
  2. ???? ?? ????? ???????? expects ?? ?? ?????? ???????? ?? ????? ??? ???? ?? ?? ???? ???????? ????????? ?? ??? ??? ???????? ?? ?? ?????? ??? ???? ??????????? ????? ??? ?? ???????????? ???? ??:
    {? = CALL Procedure1 (?, ?)
  3. ODBC .NET ???????? ???????, OLE DB ???????, ???? ??????? ?????? (zero-based) ?????? ?? ??? ?? ???? ?????????? ?? ??????? ???? ???

????????? - ?? ????? ???????? ?? ??????? ????

  1. ??? ???? ??? ??? ?? ????, ?? ??????? ?? ??????? ODBC .NET ???????? ??????? ?? ????? Microsoft ??? ????:
    HTTP://MSDN.Microsoft.com/en-us/DATA/aa937729.aspx
  2. Visual Studio .NET ??????? ????, ?? ???? ??? ???? ????? ?? ??? ?? ??? ??? ????? C# .NET Windows ????????? ??????
  3. ????? ????????????????? ??,?????? ??????????? ????, ?? ???? ???Microsoft.Data.ODBC.dll????? ?????? ?? ???? ?? ??? ??? ??????? ??? ??????????????? ????? ?? ??????? ?? ??? ?? ..
  4. ??? ????? ?? ??? ????, ?? ????? ????? ??? ??????:
    using System.Data;
    using Microsoft.Data.Odbc;
    					
  5. ?????? ??????????????? ??? ?? ???????? ????? ????? ?? ?????
  6. ??? ?? ??? ??? ????? ??? ????? ???? ?? ??? ???? ??? ?? ???-????? ????????? ????,?????? ???? ???? ?? ??? ????? ??? ???????????? ????,????? ?????????, SQL ????? ??????? ???????? ?? ??? ??? ?????? ???????? ????:
    OdbcConnection cn;
    OdbcCommand cmd;
    OdbcParameter prm;
    OdbcDataReader dr;
    
    try {
        //Change the connection string to use your SQL Server.
        cn = new OdbcConnection("Driver={SQL Server};Server=servername;Database=Northwind;Trusted_Connection=Yes");
    
        //Use ODBC call syntax.
        cmd = new OdbcCommand("{call CustOrderHist (?)}", cn);
    
        prm = cmd.Parameters.Add("@CustomerID", OdbcType.Char, 5);
        prm.Value = "ALFKI";
    
        cn.Open();
    
        dr = cmd.ExecuteReader();
    
        //List each product.
        while (dr.Read()) 
    	Console.WriteLine(dr.GetString(0));
    
        //Clean up.
        dr.Close();
        cn.Close();
    }
    catch (OdbcException o) {
        MessageBox.Show(o.Message.ToString());
    }
    					
  7. ????????? ?? ?????? ?? ??? ??? ?? ????? ???????? ?? ??? ??? CustomerID ??? ?? ??? ?? "CustOrderHist" ???????? ?????????, ??? ?? ?? ?? resultset ???? ??? ?????? ????? ??? ????????? ?????? ALFKI ?????? ???? ??? ???????? ?? ???? ????? ??????

    ???:: ?????? ????? ????? ?? ??? CTRL + ALT + O ??????

????????? - ?????? ???????? ?????? ?? ??????? ????

  1. ????????? ????? ??????? ??? ????? ???????? ????????? ????? ?????? ???????? ?? ????? ????? ?? ???????? ????????? ?? CustomerID ?? ??? ??? ??? ????? ???????? ??????? ???? ?? ?? ?????? ?????? ??? ???? ?? ???? ???? ??, ?? ?????? ?? ??? ??? ??? ?????? ???????? ?????? paid ???? ?? ????? ??? freight ???? ??, ?? ?? ????? ??? ?? ??? ??? ?????? ?????? ??? ???? ?????? ???? ???
    CREATE PROCEDURE usp_TestParameters
    @CustID CHAR(5),
    @AvgFreight MONEY OUTPUT
    AS
    SELECT @AvgFreight = AVG(Freight) FROM Orders WHERE CustomerID = @CustID
    SELECT * FROM Orders WHERE CustomerID = @CustID
    RETURN @@ROWCOUNT
    					
  2. ???, ???? ??? ?? ????? ????? ????????? ??? ????? ??? substituting 1 ?? 6 ??? ?? ???????:
    OdbcConnection cn;
    
    try {
        cn = new OdbcConnection("Driver={SQL Server};Server=servername;Database=Northwind;Trusted_Connection=Yes");
    
        OdbcCommand cmd = new OdbcCommand("{? = call usp_TestParameters (?, ?)}", cn);
    
        OdbcParameter prm = cmd.Parameters.Add("@RETURN_VALUE", OdbcType.Int);
        prm.Direction = ParameterDirection.ReturnValue;
    
        prm = cmd.Parameters.Add("@CustomerID", OdbcType.Char, 5);
        prm.Value = "ALFKI";
    
        prm = cmd.Parameters.Add("@AvgFreight", OdbcType.Double);
        prm.Direction = ParameterDirection.Output;
    
        cn.Open();
        OdbcDataReader dr = cmd.ExecuteReader();
    
        while (dr.Read())
             Console.WriteLine(dr.GetString(0));
    
        dr.Close();
        cn.Close();
    
        Console.WriteLine("Average Freight (output param): {0}", cmd.Parameters[2].Value);
        Console.WriteLine("Order Count (return value): {0}", cmd.Parameters[0].Value);
    
    }
    
    catch (OdbcException o) {
        MessageBox.Show(o.Message.ToString());
    }
    					
  3. ????????? ?? ?????? ?? ??? ?? ???, ?? ????? ???????? ?? ??? ??? CustomerID ??? passing ??? 1 ??? ???? ?? "usp_TestParameters" ???????? ????????? ?? ??? ????, ?? ?? resultset, ?? ?????? ???????? ?? ????? ??? ???? ??? ?????? ????? ??? ???? ????????? ?????? ALFKI, ???? ?? ???? ?? ?????? ?? ????? paid ?????? ??? freight ?????? ??? ?? ???? ????? ??????

    ???:: ?????? ????? ????? ?? ??? CTRL + ALT + O ??????

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

  • ODBC .NET ???????? ??????? ?? ??? ADO ???????? ??? ???? ?? ??? ???????? ????????????, ???? CommandText ?? ??? ??? ???? ????????? ?? ??? ???? ??, ????? ?? ????? ???? ???? ?? ???? ???
  • ?? ??? ???????? ????????? ?? ?? resultset ???? ??, ?? ?????? parameter(s) ?? ????? ??? ?????? ???? ?? ?? ?? ?? resultset ?? ?? ?? ?????? ?? ???? ?????? ?? ???, ??? ?? ???? ??? ????? ??? ????? "dr.Close()" ???? ???? ??, ?? ???? ?? ?????? ???????? ?? ??? ??? ?? ????? ??? ??????? ???? ??? ???????
  • ODBC .NET ???????? ???????, OLE DB ???????, ???? ??????? ?????? (zero-based) ?????? ?? ??? ?? ???? ?????????? ?? ??????? ???? ???
  • ODBC .NET ???????? ??????? Visual Studio .NET ?? ??? ???? ??, ????? ???-??? ??????? ???? ??????

??????

ODBC ??? ???????? ?? ???????? ??????? ?? ??? MSDN ????????? ??? ODBC ?????????? ?? ?????? ??? "????????? ???" ???? ?? ??????

???

???? ID: 310130 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
??????: 
kbdatabase kbhowtomaster kbsystemdata kbmt KB310130 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:310130

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

 

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