首页 新闻 搜索 专区 学院

WM DBHelp类

0
悬赏园豆:30 [已解决问题] 解决于 2012-03-05 08:22

通过VS2008新建的智能设备项目,操作SQL2000/2005的数据库,如果全部用SQL 语句是可以的,但是我有很多的复杂关系需要通过存储过程来实现,可是我发现Windows Mobile里面连Parameters.AddRange这个方法都没有,哪位朋友有这方面的操作类请发一个给我,类似下面的这一段代码,下面的代码是CS和BS里面用到的,感谢!


using System.Net.Mime;
using DBUtility;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System;

namespace DAL
{
    public class DALHelper
    {
       

        private SqlConnection connection;
        public SqlConnection Connection
        {
            get
            {
                string connectionString = @"server=.;database=DemoDB;uid=sa;pwd=123456";
               
                if (connection == null)
                {
                    connection = new SqlConnection(connectionString);
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == System.Data.ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }
                return connection;
            }
        }

        public int ExecuteCommand(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            int result = cmd.ExecuteNonQuery();
            return result;
        }

        public int ExecuteCommand(string sql, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            return cmd.ExecuteNonQuery();
        }

     
        public string GetScalar(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            string result = cmd.ExecuteScalar().ToString();
            return result;
        }

        public string GetScalar(string sql, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            string result = cmd.ExecuteScalar().ToString();
            return result;
        }

        public SqlDataReader GetReader(string safeSql)
        {
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }

        public SqlDataReader GetReader(string sql, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            SqlDataReader reader = cmd.ExecuteReader();
            return reader;
        }

        public DataTable GetDataSet(string safeSql)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(safeSql, Connection);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }

        public DataTable GetDataSet(string sql, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds.Tables[0];
        }

        /// <summary>
        /// 返回一个数据集,从里面取出不同的表
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public DataSet GetDataSet3(string sql, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(values);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            return ds;
        }
   
    }
}

无 影的主页 无 影 | 初学一级 | 园豆:59
提问于:2011-10-07 10:21
< >
分享
最佳答案
0

你看下 这个对不

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

namespace SQL.MySchoolDAL
{
   public static class DBHelper
    {
        private static string connectionString =
            ConfigurationManager.ConnectionStrings["conString"].ToString();

        private static SqlConnection connection;
        public static SqlConnection Connection
        {
            get
            {
                if (connection == null || connection.ConnectionString=="")
                {
                    connection = new SqlConnection(connectionString);
                    connection.Open();
                }
                else if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
                else if (connection.State == ConnectionState.Broken)
                {
                    connection.Close();
                    connection.Open();
                }
                return connection;
            }
        }

        public static int ExecuteCommand(string sql, CommandType commandType, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            cmd.CommandType = commandType;
            int result = cmd.ExecuteNonQuery();
            return result;
        }

        public static int GetScalar(string sql, CommandType commandType, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            cmd.CommandType = commandType;
            int result = Convert.ToInt32(cmd.ExecuteScalar());
            return result;
        }

        public static SqlDataReader GetReader(string sql, CommandType commandType, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            cmd.Parameters.AddRange(values);
            cmd.CommandType = commandType;
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

       public static DataSet GetDataSet(string sql, CommandType commandType, params SqlParameter[] values)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(sql, Connection);
            da.SelectCommand.Parameters.AddRange(values);
            da.SelectCommand.CommandType = commandType;
            da.Fill(ds);
            return ds;
        }
    }
}

收获园豆:30
秋风sao落叶 | 初学一级 |园豆:44 | 2011-10-07 18:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册