欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

c# 调用存储过程方法

程序员文章站 2024-01-27 16:36:46
...

/// summary /// 运用实例 /// /summary public void getSqlPar() { SqlConnection scn = null; SqlCommand scm = null; DbParameter[] para ={ MakeParam("@job_desc", (DbType)SqlDbType.VarChar, 50, ParameterDirection.Input, "45"), }; using (scn = n

///

/// 运用实例

///

public void getSqlPar()

{

SqlConnection scn = null;

SqlCommand scm = null;

DbParameter[] para ={

MakeParam("@job_desc", (DbType)SqlDbType.VarChar, 50, ParameterDirection.Input, "45"),

};

using (scn = new SqlConnection())

{

scn.ConnectionString = "server=.;uid=sa;pwd=111111;database=pubs;";

scn.Open();

using (scm = new SqlCommand())

{

scm.Connection = scn;

scm.CommandType = CommandType.StoredProcedure;

scm.CommandText = "jobs_add_item";

foreach (DbParameter db in para)

{

scm.Parameters.Add(db);

}

scm.ExecuteNonQuery();

}

}

}

///

/// 生成参数

///

/// 存储过程名称

/// 参数类型

/// 参数大小

/// 参数方向

/// 参数所需要的值

///

public DbParameter MakeParam(string ParamName, DbType DbType, Int32 Size, ParameterDirection Direction, Object Value)

{

DbParameter param;

param = MakeParams(ParamName, DbType, Size);

param.Direction = Direction;

if (!(Direction == ParameterDirection.Output && Value == null))

param.Value = Value;

return param;

}

///

/// 生成一个不带值的参数

///

/// 参数名称

/// 参数类型

/// 参数大小

///

public DbParameter MakeParams(string ParamName, DbType DbType, Int32 Size)

{

SqlParameter param;

if (Size > 0)

param = new SqlParameter(ParamName, (SqlDbType)DbType, Size);

else

param = new SqlParameter(ParamName, (SqlDbType)DbType);

return param;

}