一个菜鸟请叫个问题,不要笑哦!
//ParameterEx结构
public struct ParameterEx
{
/**/
/// <summary>
/// 参数名称
/// </summary>
public string Text;
/**/
/// <summary>
/// 参数值
/// </summary>
public object Value;
/**/
/// <summary>
/// 参数数据类型
/// </summary>
public object DataType;
/**/
/// <summary>
/// 参数传入传出方向
/// </summary>
public object ParaDirection;
/**/
/// <summary>
/// 参数大小
/// </summary>
public object ParaSize;
}
#region 设置和回传参数
//设置参数到SqlCommand
public static void SetParameters(SqlCommand cmd, ParameterEx[] parameters)
{
if (parameters != null)
{
for (int i = 0; i < parameters.GetLength(0); i++)
{
if ((ParameterDirection)parameters[i].ParaDirection == ParameterDirection.Output)
{
if (parameters[i].ParaSize == null)
{
cmd.Parameters.Add(new SqlParameter((string)parameters[i].Text, (SqlDbType)parameters[i].DataType)).Direction = (ParameterDirection)parameters[i].ParaDirection;
}
else
{
cmd.Parameters.Add(new SqlParameter((string)parameters[i].Text, (SqlDbType)parameters[i].DataType, (int)parameters[i].ParaSize)).Direction = (ParameterDirection)parameters[i].ParaDirection;
}
}
else
{
if (parameters[i].ParaSize == null)
{
cmd.Parameters.Add(new SqlParameter((string)parameters[i].Text, parameters[i].Value)).Direction = ParameterDirection.Input;
}
else
{
SqlParameter op0 = new SqlParameter((string)parameters[i].Text, (SqlDbType)parameters[i].DataType, (int)parameters[i].ParaSize);
op0.Value = parameters[i].Value;
cmd.Parameters.Add(op0);
}
}
}
}
}
//回传参数到SqlCommand
public static void ReturnParameters(SqlCommand cmd, ParameterEx[] parameters)
{
if (parameters != null)
{
for (int i = 0; i < parameters.GetLength(0); i++) //回传参数值
{
if ((ParameterDirection)parameters[i].ParaDirection == ParameterDirection.Output)
{
if ((SqlDbType)parameters[i].DataType != SqlDbType.UniqueIdentifier)
{
parameters[i].Value = cmd.Parameters[i].Value;
}
}
}
}
}
#endregion
//GetDataTable(2)由指定的存储过程和相关参数,返回一个数据表
public static DataTable GetDataTable(string storeProcedureName, ParameterEx[] parameters)
{
try
{
SqlCommand cmd = new SqlCommand(storeProcedureName, Connection);
if (IsInTransaction) cmd.Transaction = Transaction;
cmd.CommandType = CommandType.StoredProcedure;
//参数赋值
SetParameters(cmd, parameters);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
cmd = null;
return dt;
}
catch (Exception err)
{
throw new Exception("\nError:" + err.Message);
}
}
存储过程为
CREATE PROCEDURE set_shop
@shopid int,
@shopname varchar(50)
AS
select * from Shop where shop_no=@shopid and shop_name=@shopname
GO
我在调用GetDataTable时候,要传一个储存过程名称,还有个参数我就不知道怎么搞了,请问一下ParameterEx[] parameters 有什么用呢,怎么使用呢?能举个例子吗?我真心的感谢你们!!!!!!!!!!!!!!