c#数据库操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace mysite
{
    public class DatabaseQuery
    {
        public static SqlDataReader Select(string sql)
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["mysiteConnectionString"].ConnectionString);
                conn.Open();
                
                cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                var res = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return res;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                cmd.Dispose();
            }
        }
    }
}

posted on 2011-05-30 18:36  大宝pku  阅读(312)  评论(0编辑  收藏  举报

导航