protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
#region 实参
string id = "1";
string name = "大红";
/*
score是输出参数,如果下面的代码封装成一个方法,
这个参数前面带有ref,比如像这种方法
public object pro_out16(
string id,
string name,
ref System.Nullable<int> score)
*/
int? score = 0;
#endregion
//1 、定义SqlCommand
var command = new System.Data.SqlClient.SqlCommand();
#region 2 、定义链接
var _connection = new System.Data.SqlClient.SqlConnection();
_connection.ConnectionString = WebApplication4.Properties.Settings.Default.db_sql20001ConnectionString;
command.Connection = _connection;
#endregion
#region 3、 定义执行类型,比如执行存储过程
command.CommandText = "dbo.pro_out16";
command.CommandType = System.Data.CommandType.StoredProcedure;
#endregion
#region 4。1、 定义参数类型
command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, 10, 0, null, System.Data.DataRowVersion.Current, false, null, "", "", ""));
command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@id", System.Data.SqlDbType.VarChar, 20, System.Data.ParameterDirection.Input, 0, 0, null, System.Data.DataRowVersion.Current, false, null, "", "", ""));
command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 20, System.Data.ParameterDirection.Input, 0, 0, null, System.Data.DataRowVersion.Current, false, null, "", "", ""));
command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@score", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.InputOutput, 10, 0, null, System.Data.DataRowVersion.Current, false, null, "", "", ""));
#endregion
#region 4。2参数赋值
if ((id == null))
{
command.Parameters[1].Value = System.DBNull.Value;
}
else
{
command.Parameters[1].Value = ((string)(id));
}
if ((name == null))
{
command.Parameters[2].Value = System.DBNull.Value;
}
else
{
command.Parameters[2].Value = ((string)(name));
}
if ((score.HasValue == true))
{
command.Parameters[3].Value = ((int)(score.Value));
}
else
{
command.Parameters[3].Value = System.DBNull.Value;
}
#endregion
#region 5、打开链接并执行,执行后关闭链接
System.Data.ConnectionState previousConnectionState = command.Connection.State;
if (((command.Connection.State & System.Data.ConnectionState.Open)
!= System.Data.ConnectionState.Open))
{
command.Connection.Open();
}
object returnValue;
try
{
returnValue = command.ExecuteScalar();
}
finally
{
if ((previousConnectionState == System.Data.ConnectionState.Closed))
{
command.Connection.Close();
}
}
#endregion
#region 6、执行后获取输出参数的值
if (((command.Parameters[3].Value == null)
|| (command.Parameters[3].Value.GetType() == typeof(System.DBNull))))
{
score = new System.Nullable<int>();
}
else
{
score = new System.Nullable<int>(((int)(command.Parameters[3].Value)));
}
#endregion
#region 7、执行后获取存储过程的返回值
var Pro_returnvalue=0;
if (((command.Parameters[0].Value == null)
|| (command.Parameters[0].Value.GetType() == typeof(System.DBNull))))
{
Pro_returnvalue = new System.Nullable<int>();
}
else
{
Pro_returnvalue = new System.Nullable<int>(((int)(command.Parameters[0].Value)));
}
#endregion
#region 在界面上显示输出参数的值
TextBox1.Text = score.ToString();
#endregion
#region 在界面上显示输出存储过程返回值
TextBox2.Text = Pro_returnvalue.ToString();
#endregion
}
}
#region 上面的例子用到了这些类:
//System.Data.SqlClient.SqlCommand;
//System.Data.SqlClient.SqlConnection;
//System.Data.CommandType;
//System.Data.SqlClient.SqlParameter;
//System.Data.ConnectionState;
//System.Data.SqlDbType
#endregion