C#_数据库交互_SqlHelper

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace 收费系统
{
    class SQLhelper
    {
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            string Constrs = ConfigurationManager.ConnectionStrings["Connstrs"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(Constrs))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    foreach (SqlParameter parameter in parameters)
                    {

                        cmd.Parameters.Add(parameter);
                    }
                    return cmd.ExecuteNonQuery();


                }

            }
        }


        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
        {
            string Constrs = ConfigurationManager.ConnectionStrings["Connstrs"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(Constrs))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    foreach (SqlParameter parameter in parameters)
                    {

                        cmd.Parameters.Add(parameter);
                    }
                    DataSet dataset = new DataSet();
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(dataset);
                    return dataset.Tables[0];
                }

            }
        }
    }
}


posted @ 2013-10-08 00:42  MrMrCash  阅读(144)  评论(0编辑  收藏  举报