SqlHelper
1 class SqlDataReader1 2 { 3 //连接字符串 4 public static readonly string connectString = ConfigurationManager.ConnectionStrings["xiaoCeShi"].ConnectionString; 5 /// <summary> 6 /// 输入名字模糊查询输出可能学生的ID和名字 7 /// </summary> 8 /// <param name="sql"> 查询语句</param> 9 /// <returns>返回查询的名字和ID数组</returns> 10 public static string[] GetPosibleSut(string sql) 11 { 12 string NameIDArray=""; 13 if (connectString!=null) 14 { 15 using (SqlConnection sqlconn = new SqlConnection(connectString)) 16 { 17 SqlCommand sqlcomm = new SqlCommand( sql,sqlconn); 18 //打开连接 19 sqlconn.Open(); 20 //执行查询 21 SqlDataReader dr = sqlcomm.ExecuteReader(CommandBehavior.CloseConnection); 22 //如果读取到相应的数据执行循环 23 while(dr.Read()) 24 { 25 NameIDArray += "ID"+dr["StudentId"].ToString() +"----------------Name"+dr["StudentName"].ToString()+","; 26 } 27 28 } 29 30 } 31 return NameIDArray.TrimEnd(',').Split(','); 32 } 33 public static string[] GetPosibleSutByAda(string sql) 34 { 35 string NameIDArray = ""; 36 using (SqlConnection conn=new SqlConnection(connectString)) 37 { 38 SqlCommand comm = new SqlCommand(sql,conn); 39 SqlDataAdapter da = new SqlDataAdapter(comm); 40 DataSet ds = new DataSet(); 41 da.Fill(ds); 42 DataTable dt=ds.Tables[0]; 43 if (dt!=null) 44 { 45 foreach (DataRow item in dt.Rows) 46 { 47 48 NameIDArray += "ID" + item["StudentId"].ToString() + "----------------Name" + item["StudentName"].ToString() + ","; 49 } 50 51 } 52 53 54 } 55 return NameIDArray.TrimEnd(',').Split(','); 56 57 } 58 }