ASP

SqlConnection con=new SqlConnection("user id=sa;password=;initial catalog=game;data source=server1")
SqlDataAdapter adp=new SqlDataAdapter("p_game_countryToflag_all",con);
adp.SelectCommand.CommandType=CommandType.StoredProcedure;
adp.SelectCommand.Parameters.Add(new SqlParameter("@user_id",SqlDbType.Char,12));
adp.SelectCommand.Parameters["@user_id"].Value=user_id;
DataSet ds=new DataSet();
adp.Fill(ds,"rand");
p_game_countryToflag_all为存储过程名
@user_id 为参数

解决方案 »

  1.   

    protected void Page_Load(Object Src, EventArgs E) 
        {
            SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;database=northwind;Trusted_Connection=yes");
            SqlDataAdapter myCommand = new SqlDataAdapter("Ten Most Expensive Products", myConnection);        myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;        DataSet ds = new DataSet();
            myCommand.Fill(ds, "产品");        MyDataGrid.DataSource=ds.Tables["产品"].DefaultView;
            MyDataGrid.DataBind();
        }
      

  2.   

    存储过程提供了数据驱动应用程序中的许多优点。利用存储过程,数据库操作可以封装在单个命令中,为获取最佳性能而进行优化并通过附加的安全性得到增强。虽然可以通过以 SQL 语句的形式传递参数自变量之前的存储过程名称来调用存储过程,但如果使用 ADO.NET Command 对象的 Parameters 集合,则可以显式地定义存储过程参数并访问输出参数和返回值。若要调用存储过程,请将 Command 对象的 CommandType 设置为 StoredProcedure。CommandType 一旦设置为 StoredProcedure,就可以使用 Parameters 集合来定义参数,如以下示例所示。SqlClient
    [Visual Basic]
    Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _
                                                       "Initial Catalog=northwind")Dim salesCMD As SqlCommand = New SqlCommand("SalesByCategory", nwindConn)
    salesCMD.CommandType = CommandType.StoredProcedureDim myParm As SqlParameter = salesCMD.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15)
    myParm.Value = "Beverages"nwindConn.Open()Dim myReader As SqlDataReader = salesCMD.ExecuteReader()Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1))Do While myReader.Read()
      Console.WriteLine("{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1))
    LoopmyReader.Close()
    nwindConn.Close()
    [C#]
    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");SqlCommand salesCMD = new SqlCommand("SalesByCategory", nwindConn);
    salesCMD.CommandType = CommandType.StoredProcedure;SqlParameter myParm = salesCMD.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15);
    myParm.Value = "Beverages";nwindConn.Open();SqlDataReader myReader = salesCMD.ExecuteReader();Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1));while (myReader.Read())
    {
      Console.WriteLine("{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1));
    }myReader.Close();
    nwindConn.Close();
    OleDb
    [Visual Basic]
    Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;" & _
                                                           "Initial Catalog=northwind")Dim salesCMD As OleDbCommand = New OleDbCommand("SalesByCategory", nwindConn)
    salesCMD.CommandType = CommandType.StoredProcedureDim myParm As OleDbParameter = salesCMD.Parameters.Add("@CategoryName", OleDbType.VarChar, 15)
    myParm.Value = "Beverages"nwindConn.Open()Dim myReader As OleDbDataReader = salesCMD.ExecuteReader()Console.WriteLine("{0}, {1}", myReader.GetName(0), myReader.GetName(1))Do While myReader.Read()
      Console.WriteLine("{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1))
    LoopmyReader.Close()
    nwindConn.Close()
    [C#]
    OleDbConnection nwindConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;" +
                                                    "Initial Catalog=northwind");OleDbCommand salesCMD = new OleDbCommand("SalesByCategory", nwindConn);
    salesCMD.CommandType = CommandType.StoredProcedure;OleDbParameter myParm = salesCMD.Parameters.Add("@CategoryName", OleDbType.VarChar, 15);
    myParm.Value = "Beverages";nwindConn.Open();OleDbDataReader myReader = salesCMD.ExecuteReader();Console.WriteLine("\t{0}, {1}", myReader.GetName(0), myReader.GetName(1));while (myReader.Read())
    {
      Console.WriteLine("\t{0}, ${1}", myReader.GetString(0), myReader.GetDecimal(1));
    }myReader.Close();
    nwindConn.Close();
    Parameter 对象可以使用 Parameter 构造函数来创建,或通过调用 Command 的 Parameters 集合的 Add 方法来创建。Parameters.Add 会将构造函数参数或现有 Parameter 对象用作输入。在将 Parameter 的 Value 设置为空引用时,请使用 DBNull.Value。对于 Input 参数之外的参数,必须设置 ParameterDirection 属性来指定参数类型是 InputOutput、Output 还是 ReturnValue。以下示例显示创建 Input、Output 和 ReturnValue 参数之间的差异。SqlClient
    [Visual Basic]
    Dim sampleCMD As SqlCommand = New SqlCommand("SampleProc", nwindConn)
    sampleCMD.CommandType = CommandType.StoredProcedureDim sampParm As SqlParameter = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int)
    sampParm.Direction = ParameterDirection.ReturnValuesampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12)
    sampParm.Value = "Sample Value"sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28)
    sampParm.Direction = ParameterDirection.OutputnwindConn.Open()Dim sampReader As SqlDataReader = sampleCMD.ExecuteReader()Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1))Do While sampReader.Read()
      Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1))
    LoopsampReader.Close()
    nwindConn.Close()Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters("@OutputParm").Value)
    Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters("RETURN_VALUE").Value)
      

  3.   

    [C#]
    SqlCommand sampleCMD = new SqlCommand("SampleProc", nwindConn);
    sampleCMD.CommandType = CommandType.StoredProcedure;SqlParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
    sampParm.Direction = ParameterDirection.ReturnValue;sampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12);
    sampParm.Value = "Sample Value";sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28);
    sampParm.Direction = ParameterDirection.Output;nwindConn.Open();SqlDataReader sampReader = sampleCMD.ExecuteReader();Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1));while (sampReader.Read())
    {
      Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1));
    }sampReader.Close();
    nwindConn.Close();Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value);
    Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);
    OleDb
    [Visual Basic]
    Dim sampleCMD As OleDbCommand = New OleDbCommand("SampleProc", nwindConn)
    sampleCMD.CommandType = CommandType.StoredProcedureDim sampParm As OleDbParameter = sampleCMD.Parameters.Add("RETURN_VALUE", OleDbType.Integer)
    sampParm.Direction = ParameterDirection.ReturnValuesampParm = sampleCMD.Parameters.Add("@InputParm", OleDbType.VarChar, 12)
    sampParm.Value = "Sample Value"sampParm = sampleCMD.Parameters.Add("@OutputParm", OleDbType.VarChar, 28)
    sampParm.Direction = ParameterDirection.OutputnwindConn.Open()Dim sampReader As OleDbDataReader = sampleCMD.ExecuteReader()Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1))Do While sampReader.Read()
      Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1))
    LoopsampReader.Close()
    nwindConn.Close()Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters("@OutputParm").Value)
    Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters("RETURN_VALUE").Value)
    [C#]
    OleDbCommand sampleCMD = new OleDbCommand("SampleProc", nwindConn);
    sampleCMD.CommandType = CommandType.StoredProcedure;OleDbParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", OleDbType.Integer);
    sampParm.Direction = ParameterDirection.ReturnValue;sampParm = sampleCMD.Parameters.Add("@InputParm", OleDbType.VarChar, 12);
    sampParm.Value = "Sample Value";sampParm = sampleCMD.Parameters.Add("@OutputParm", OleDbType.VarChar, 28);
    sampParm.Direction = ParameterDirection.Output;nwindConn.Open();OleDbDataReader sampReader = sampleCMD.ExecuteReader();Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1));while (sampReader.Read())
    {
      Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1));
    }sampReader.Close();
    nwindConn.Close();Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value);
    Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);
    将参数用于 SqlCommand
    在将参数用于 SqlCommand 时,添加到 SqlParameterCollection 的参数的名称必须匹配存储过程中的参数标记的名称。SQL Server .NET 数据提供程序将存储过程中的参数当作命名参数并搜索匹配的参数标记。SQL Server .NET 数据提供程序不支持在向 SQL 语句或存储过程传递参数时使用问号 (?) 占位符。在这种情况下,必须使用命名参数,如以下示例所示。SELECT * FROM Customers WHERE CustomerID = @CustomerID
    将参数用于 OleDbCommand
    在将参数用于 OleDbCommand 时,添加到 OleDbParameterCollection 的参数的名称必须匹配存储过程中的参数标记的名称。OLE DB .NET 数据提供程序参数将存储过程中的参数当作命名参数并搜索匹配的参数标记。OLE DB .NET 数据提供程序不支持在向 SQL 语句或存储过程传递参数时使用命名参数。在此情况下,必须使用问号 (?) 占位符,如以下示例所示。SELECT * FROM Customers WHERE CustomerID = ?
    因此,向 Parameters 集合添加 Parameter 对象的顺序必须直接对应于该参数的问号占位符的位置。导出参数信息
    参数也可以使用 CommandBuilder 类从存储过程导出。SqlCommandBuilder 和 OleDbCommandBuilder 类都提供了静态方法 DeriveParameters,该静态方法将自动使用存储过程中的参数信息填充 Command 对象的 Parameters 集合。请注意,DeriveParameters 将改写 Command 的任何现有参数信息。导出参数信息时需要经历一个到数据源的附加行程,以获取参数信息。如果参数信息在设计时是已知的,则可以通过显式设置参数来提高应用程序的性能。以下代码示例显示如何使用 CommandBuilder.DeriveParameters 来填充 Command 对象的 Parameters 集合。[Visual Basic]
    Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;")
    Dim salesCMD As SqlCommand = New SqlCommand("Sales By Year", nwindConn)
    salesCMD.CommandType = CommandType.StoredProcedurenwindConn.Open()
    SqlCommandBuilder.DeriveParameters(salesCMD)
    nwindConn.Close()
    [C#]
    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
    SqlCommand salesCMD = new SqlCommand("Sales By Year", nwindConn);
    salesCMD.CommandType = CommandType.StoredProcedure;nwindConn.Open();
    SqlCommandBuilder.DeriveParameters(salesCMD);
    nwindConn.Close();