CREATE TABLE [tab_cJ] (
[id] [int] IDENTITY (1, 1) NOT NULL PRIMARY KEY,
[name] [varchar] (50) ,
[age] [int] NULL ,
[info] [varchar] (200)
)
[id] [int] IDENTITY (1, 1) NOT NULL PRIMARY KEY,
[name] [varchar] (50) ,
[age] [int] NULL ,
[info] [varchar] (200)
)
------------------------- ------------------ build stored procedures
CREATE PROCEDURE QueryInfoByName
@name varchar(50),
@age int
AS
select info from tab_cj where [name]=@name and age=@age
GO
@name varchar(50),
@age int
AS
select info from tab_cj where [name]=@name and age=@age
GO
--------------------transfer----------------------------- -
the using (Conn the SqlConnection the SqlConnection new new = ( "the Data .; the Initial Cataog the Source = = Test; the Integrated Security = True"))
{
the SqlCommand cmd = conn.CreateCommand ();
cmd.CommandType = CommandType.StoredProcedure; // execute the stored procedure specified operation
cmd.CommandText = "QueryInfoByName"; // name of the stored procedure
// stored procedure corresponding to the first parameter QueryInfoByName the @name
the SqlParameter = new new parName the SqlParameter ( "@ name", SqlDbType.VarChar, 50);
// specified parameters @ to name into a value
parName.Value = "AA";
// stored procedure corresponding to the second parameter QueryInfoByName @age
the SqlParameter = new new parAge the SqlParameter ( "@ Age", SqlDbType.Int);
// to specify the parameters @age the value transferred
= 12 parAge.Value;
// This step is very important, we must set a good add two parameters to the type parameter set Command object's
cmd.Parameters.Add (parName);
cmd.Parameters.Add (parAge);
// one embodiment, the query results back to be displayed on the control DataGrid like
the DataSet the DataSet new new DS = ();
the SqlDataAdapter = new new Adapter the SqlDataAdapter (cmd);
adapter.Fill (DS);
// Second way, by reading a single value
conn.Open ();
the SqlDataReader = cmd.ExecuteReader Reader ();
IF (reader.HasRows)
{
the while (reader.Read ())
{
Response.Write (reader.GetString (0));
}
}
}
{
the SqlCommand cmd = conn.CreateCommand ();
cmd.CommandType = CommandType.StoredProcedure; // execute the stored procedure specified operation
cmd.CommandText = "QueryInfoByName"; // name of the stored procedure
// stored procedure corresponding to the first parameter QueryInfoByName the @name
the SqlParameter = new new parName the SqlParameter ( "@ name", SqlDbType.VarChar, 50);
// specified parameters @ to name into a value
parName.Value = "AA";
// stored procedure corresponding to the second parameter QueryInfoByName @age
the SqlParameter = new new parAge the SqlParameter ( "@ Age", SqlDbType.Int);
// to specify the parameters @age the value transferred
= 12 parAge.Value;
// This step is very important, we must set a good add two parameters to the type parameter set Command object's
cmd.Parameters.Add (parName);
cmd.Parameters.Add (parAge);
// one embodiment, the query results back to be displayed on the control DataGrid like
the DataSet the DataSet new new DS = ();
the SqlDataAdapter = new new Adapter the SqlDataAdapter (cmd);
adapter.Fill (DS);
// Second way, by reading a single value
conn.Open ();
the SqlDataReader = cmd.ExecuteReader Reader ();
IF (reader.HasRows)
{
the while (reader.Read ())
{
Response.Write (reader.GetString (0));
}
}
}
2. Call the stored procedure with an output parameter
------------------------- ------------------ build stored procedures
create procedure getAge
(
@name varchar(50),
@age int output
)
as
select @age=age from tab_cJ where [name]=@name
(
@name varchar(50),
@age int output
)
as
select @age=age from tab_cJ where [name]=@name
--------------------transfer----------------------------- -
the using (Conn the SqlConnection the SqlConnection new new = ( "the Data .; the Initial Cataog the Source = = Test; the Integrated Security = True"))
{
the SqlCommand cmd = conn.CreateCommand ();
cmd.CommandType = CommandType.StoredProcedure; // execute the stored procedure specified operation
cmd.CommandText = "getAge"; // name of the stored procedure
the SqlParameter = new new parName the SqlParameter ( "@ name", SqlDbType.VarChar, 50);
parName.Value = "AA";
// output corresponding to the stored procedure parameter getAge @ Age
the SqlParameter = new new parAge the SqlParameter ( "@ Age", SqlDbType.Int);
parAge.Direction = ParameterDirection.Output;
cmd.Parameters.Add (parName);
cmd.Parameters. Add (trimming);
try
{
conn.Open();
cmd.ExecuteNonQuery();
int i = int.Parse(parAge.Value.ToString());
Response.Write(i.ToString());
conn.Close();
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
}
{
the SqlCommand cmd = conn.CreateCommand ();
cmd.CommandType = CommandType.StoredProcedure; // execute the stored procedure specified operation
cmd.CommandText = "getAge"; // name of the stored procedure
the SqlParameter = new new parName the SqlParameter ( "@ name", SqlDbType.VarChar, 50);
parName.Value = "AA";
// output corresponding to the stored procedure parameter getAge @ Age
the SqlParameter = new new parAge the SqlParameter ( "@ Age", SqlDbType.Int);
parAge.Direction = ParameterDirection.Output;
cmd.Parameters.Add (parName);
cmd.Parameters. Add (trimming);
try
{
conn.Open();
cmd.ExecuteNonQuery();
int i = int.Parse(parAge.Value.ToString());
Response.Write(i.ToString());
conn.Close();
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
}