应用于Web Service调用存储过程

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace DAL
{
public class DBHelper
{
private readonly static string connection =”Data Source=.;Initial Catalog=Test;User ID=sa;pwd=123321”;

}
 /// <summary>
    /// 存储过程增删改
    /// </summary>
    /// <param name="type"></param>
    /// <param name="procSql"></param>
    /// <param name="par"></param>
    /// <returns></returns>
    public int SqlExec(CommandType type,string proSql,SqlParameter[] par)
    {
//异常处理机制
try
{
    using(Sqlconnection conn = new SqlConnection(connection))
    {
        conn.Open();
        Sqlcommand cmd = new SqlCommand(proSql,conn);
        cmd.CommandType = Type;
        if(par!=null){
            cmd.Parameters.AddRange(par);
        }
        return cmd.ExecuteNonQuery();
    }
}
catch
{
       return -1;
}
    }
    /// <summary>
    /// 存储过程调用
    /// </summary>
    /// <param name="procName"></param>
    /// <param name="par"></param>
    /// <returns></returns>
public DataTable GetProDataTable(CommandType Type,string proName,SqlParameter[] par)
{
    try
    {
        using(SqlConnction conn = new SqlConnection(connection))
        {
             SqlCommand command = new SqlCommand(procName, conn);  //执行SQL语句
                            command.CommandType = type;
                        if (par != null)
                        {
                                command.Parameters.AddRange(par);
                         }
                            SqlDataAdapter sda = new SqlDataAdapter(command);  //适配器
                         DataTable dt = new DataTable();               //NEW DataTable
                        dt.TableName = "dt";
                            sda.Fill(dt);  //填充数据
                         return dt;
        }
    }
    catch(Exception){return null;}
}

}

展开阅读全文

没有更多推荐了,返回首页