标签:style blog color io ar strong 数据 sp div
//创建一个静态方法
public static DataSet fnInsertSingleUser(DataTable v_dt, params string[] param) { try { SqlConnection cn = new SqlConnection(connectionString);//connertionString链接数据库字符串 SqlCommand cmd = cn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = @"" + param[0]; SqlParameter p = cmd.Parameters.AddWithValue("@User", v_dt);
//参数可以随意增加 SqlParameter pCode = cmd.Parameters.AddWithValue("@pCode", param[1]); SqlParameter gsdm = cmd.Parameters.AddWithValue("@gsdm", param[2]); SqlParameter khdm = cmd.Parameters.AddWithValue("@khdm", param[3]); SqlParameter qy = cmd.Parameters.AddWithValue("@qy", param[4]); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); return ds; } catch (Exception ex) { throw ex; } }
SQL
IF EXISTS ( SELECT A = 1 FROM sys.objects WHERE name = ‘PRCreate ‘ AND type = ‘P‘ ) DROP PROCEDURE dbo.PRCreate go IF EXISTS ( SELECT A = 1 FROM sys.table_types WHERE name = ‘tempPR ‘ AND is_user_defined = 1 ) DROP TYPE dbo.tempPR go CREATE TYPE dbo.tempPR AS TABLE ( PT_PCURR decimal(15, 2), --这2个字段必须与传入的C# datatable中 字段的先后顺序一致 MATNR varchar(18)-- ) go CREATE PROCEDURE dbo.PRCreate ( @User AS dbo.tempPR READONLY, --C# datatable参数 @pCode varchar(50), @gsdm varchar(50), @khdm varchar(50), @qy varchar(50) ) AS BEGIN
SELECT * FROM @User
END
调用
DataSet ds = new DataSet(); ds = SqlHelper.fnInsertSingleUser(dt, "PRCreate", param[0], param[2], param[3], param[4]);//在存储过程中处理,返回结果集
调用时注意传入的参数必须与存储过程中 @User @pCode @gsdm @khdm @qy 的参数一致(见第一段代码)
重点:
1、就是传入存储过程的Datatable参数的字段先后顺序,必须与在存储过程中创建的表字段的先后顺序一致
2、调试:用一句select语句,返回一个DataSet到C#中看看,传入datatable是否正确
标签:style blog color io ar strong 数据 sp div
原文地址:http://www.cnblogs.com/holyson/p/3994108.html