我正在.net框架上用C Sharp开发一个WinForm应用程序.我现在使用的数据库字符串是
当我使用Microsoft SQL Server 2005进行开发时,如上所述,我可以使用MultipleActiveResultSets属性同时使用2个数据读取器.
用于调用2个数据读取器的方法如下:
public static void SignUpControllerDay(DateTime Date, System.Windows.Forms.DataGridView PassedGrid) { string sql_SignUp = String.Format(@"SELECT Emp_ID as Emp_ID, Name as Name, Sum(Sum) as Sum FROM (SELECT DISTINCT o.Date, e.Emp_ID as Emp_ID, e.First_Name+ ' ' +e.Last_Name as Name, o.Quantity as Sum FROM Employee e,OT_Hours o,Position p,Signup_Sheet s WHERE e.Emp_ID=o.Emp_ID and e.Emp_ID = s.Employee_ID and s.Day_Shift = 1 and e.Position_ID = p.Position_ID and p.Position_Name = 'Controller' and o.Quantity NOT IN(0.3) and s.Date = '{0}' and o.Date <= CONVERT(VARCHAR,'{0}',101) AND o.Date > CONVERT(VARCHAR,DATEADD(YYYY,-1,'{0}'),101) ) as OVERTIME GROUP BY Emp_ID,Name ORDER BY Sum", Date); SqlConnection sqlConn = null; SqlCommand cmd_SignUp; SqlDataReader dr_SignUp; try { sqlConn = new SqlConnection(databaseConnectionString); sqlConn.Open(); cmd_SignUp = new SqlCommand(sql_SignUp, sqlConn); dr_SignUp = cmd_SignUp.ExecuteReader(); while (dr_SignUp.Read()) { ArrayList arrPhone = new ArrayList(); string sql_Phone = String.Format("SELECT Phone_Number FROM Contact_Details WHERE Emp_ID = {0}", dr_SignUp["Emp_ID"]); SqlCommand cmd_Phone = new SqlCommand(sql_Phone, sqlConn); SqlDataReader dr_Phone = cmd_Phone.ExecuteReader(); while (dr_Phone.Read()) { arrPhone.Add(dr_Phone["Phone_Number"].ToString()); } //--Retrieving Sectors ArrayList arrSector = new ArrayList(); string sql_Sector = String.Format(@"SELECT e1.EMP_ID, ( SELECT cast(Sector_ID as varchar(10)) + ';' FROM Employee_Sector_relationship e2 WHERE e2.Emp_ID = e1.Emp_ID ORDER BY Sector_ID FOR XML PATH('') ) AS Sectors FROM Employee_Sector_Relationship e1 WHERE Emp_ID = {0} GROUP BY Emp_ID ", dr_SignUp["Emp_ID"]); SqlCommand cmd_Sector = new SqlCommand(sql_Sector, sqlConn); SqlDataReader dr_Sector = cmd_Sector.ExecuteReader(); while (dr_Sector.Read()) { arrSector.Add(dr_Sector["Sectors"].ToString()); } if (arrSector.Count == 0) { arrSector.Add(" "); } if (arrPhone.Count == 0) { arrPhone.Add(" "); } //-- if (arrPhone.Count == 2) { PassedGrid.Rows.Add(dr_SignUp["Emp_ID"].ToString(), dr_SignUp["Name"].ToString(), arrSector[0], dr_SignUp["Sum"], arrPhone[0], arrPhone[1]); } else { PassedGrid.Rows.Add(dr_SignUp["Emp_ID"].ToString(), dr_SignUp["Name"].ToString(), arrSector[0], dr_SignUp["Sum"], arrPhone[0]); } } } catch (Exception e) { MessageBox.Show("Error found in SignUpControllerDay..." + Environment.NewLine + e.ToString()); } finally { if (sqlConn != null) { sqlConn.Close(); } } }
一切正常.现在真正的问题.我被告知,应用程序上线的生产SQL服务器是Microsoft SQL server 2000.经过一些研究,我发现Microsoft服务器2000不支持多个活动结果集.简而言之,它不允许我同时使用2个数据读取器.
关于SQL Server 2000,我需要知道如何从2个不同的表中同时读取数据.
有没有其他方法可以读取我在代码中提到的数据..
请帮助..应用程序已接近完成并准备投入生产.但是MS server 2000不允许applcaition相应地工作......
请帮忙
只需创建两个连接,即可在Sql Server 2000中拥有两个活动数据加载器.
为了证明这一点,我必须首先指责你使用两个非常糟糕的做法:动态sql和arraylists.你的代码中没有任何地方.你还应该阅读使用构造,尽管如果你还在使用.net 1.1,你对"使用"和"arraylists"表示歉意和哀悼.
也就是说,代码的外观如下:
string sql_Phone = "SELECT Phone_Number FROM Contact_Details WHERE Emp_ID = @EmpID"; using (SqlConnection cn2 = new Sqlconnection(databaseConnectionString)) using (SqlCommand cmd_Phone = new SqlCommand(sql_Phone, cn2)) { cmd_Phone.Parameters.Add("@EmpID", SqlDbType.Int); cn2.Open(); while (dr_SignUp.Read()) { ListarrPhone = new List (); cmd_Phone.Parameters[0].Value = dr_SignUp["Emp_ID"]; using (SqlDataReader dr_Phone = cmd_Phone.ExecuteReader()) { while (dr_Phone.Read()) { arrPhone.Add(dr_Phone["Phone_Number"].ToString()); } }
另外,看看你的代码我怀疑你真正需要做的是重写你的sql.您可以将所有这些组合到一个只是直接绑定到网格的查询中.