---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
//连接的constr
private static string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
public SqlDataReader ExecuteReader(string sql)
{
using (SqlConnection conn = new SqlConnection(constr))
{//到数据库的连接
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{//cmd
cmd.CommandText = sql;
return cmd.ExecuteReader();
}
}
}
#region //从数据库读取到编号的值
SqlHelper sqlhelper=new SqlHelper();
SqlDataReader reader= sqlhelper.ExecuteReader("select * from Sheet1");
while (reader.Read())//开始逐条读取table中的数据
{
string ids = reader["" + "编号" + ""].ToString();
string isd = reader["编号"].ToString();
if (ids != "")
{
cmbidlist.Items.Add(ids);//将所有的编号读入到可选下啦列表中
}
}
#endregion
阅读器关闭时尝试调用Read无效
已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
摸索一番,解决办法如下:
class SqlHelper
{
private string strs = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
public SqlDataReader ExecuteDataReader(string sql, params SqlParameter[] pms)
{
SqlConnection conn = new SqlConnection(strs);
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
cmd.CommandText = sql;
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
}
OK!