日期:2014-05-18 浏览次数:20375 次
public SqlDataReader Dr(Dictionary<string,string> parms,string ctext) { SqlConnection con = this.Dbcon(); SqlCommand cmd = new SqlCommand(ctext,con); foreach (KeyValuePair<string, string> kv in parms) { cmd.Parameters.AddWithValue(kv.Key,kv.Value); } con.Open(); SqlDataReader dr = cmd.ExecuteReader(); con.Close(); return dr; }
Cldb cldb=new Cldb(); Dictionary<string, string> parmsRead = new Dictionary<string, string>(); parmsRead.Add("@UserName", strUsername); SqlDataReader da = cldb.Dr(parmsRead, sql2); while (da.Read()) { UserID = da.GetGuid(0); }
public SqlDataReader Dr(Dictionary<string,string> parms,string ctext) { SqlConnection con = this.Dbcon(); SqlCommand cmd = new SqlCommand(ctext,con); foreach (KeyValuePair<string, string> kv in parms) { cmd.Parameters.AddWithValue(kv.Key,kv.Value); } con.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);//当关闭DataReader时自动关闭Connection // con.Close();不能关闭 return dr; }
------解决方案--------------------
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
这样就可以了, 返回dataset,datable,dr,用到那个返回那个。