通过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;
}
}
}
桃花长相依