日期:2014-05-18 浏览次数:21432 次
//下面是获取Access数据连接的 在AccessHelper.cs类里 /// <summary> /// 获取Access数据库连接语句 /// </summary> /// <returns></returns> public static OleDbConnection GetConn() { System.Data.OleDb.OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\source\\Database\\DB.mdb;"); return cnn; } /// <summary> /// 根据SQL命令返回数据DataTable数据表, /// 可直接作为dataGridView的数据源 /// </summary> /// <param name="SQL"></param> /// <returns></returns> public static DataTable SelectToDataTable(string SQL) { OleDbDataAdapter adapter = new OleDbDataAdapter(); OleDbCommand command = new OleDbCommand(SQL, GetConn()); adapter.SelectCommand = command; DataTable Dt = new DataTable(); adapter.Fill(Dt); return Dt; } //下面是winform代码 //取出所有分类 并绑定到combobox中 combobox控件名是: cmFatherType private void GetFatherTypeCom(string isType) { //cmFatherType.Items.Clear(); string sql = "select typeId,typeName from type_Info where fatherId=0 and isTYpe=" + isType; DataTable dt = AccessHelper.SelectToDataTable(sql); cmFatherType.DisplayMember = "typename"; cmFatherType.ValueMember = "typeId"; cmFatherType.DataSource = dt; } /// <summary> ///取出分类 下面是取出所有分类 并绑定到ListView中 控件的名字叫 lvFatherType /// </summary> private void GetType() { int typeId; string typeName; string addTime; string userName; AccessHelper helper = new AccessHelper(); try { string sql = "select t.*,u.userName from type_info t,user_info u where t.userId=u.userId and fatherId=0 and isType=1"; OleDbDataReader reader; reader = helper.GetDataReader(sql); lvFatherType.Items.Clear(); if (reader.HasRows) { while (reader.Read()) { typeId = (int)reader["typeId"]; typeName = (string)reader["typeName"]; userName = (string)reader["userName"]; addTime = Convert.ToDateTime(reader["addTime"]).ToString("yyyy年M月d号"); //创建一个ListView选项 ListViewItem lv = new ListViewItem(typeName); lv.Tag = typeId; ///将ID放在tag中 lvFatherType.Items.Add(lv);//向ListView中添加一个新项 lv.SubItems.AddRange(new string[] { userName, addTime }); } reader.Close(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { helper.CloseDataBase();//关闭数据库 } } //下面是删除的代码 if (lvFatherType.SelectedItems.Count != 0) { ListView.SelectedIndexCollection c = lvFatherType.SelectedIndices; string typeId = lvFatherType.Items[c[0]].Tag.ToString(); if (Messag