C#中ListBox控件如何实时显示ACCESS数据库表中某个字段对应的所有记录值!!!求解呀!!!
图1C#应用程序界面图2数据表如上图1所示:在组名texbox中输入数值,点击添加可以插入ACCESS数据库表中,然后左边的LIstbox显示对应表中GroupNameS...
图1C#应用程序界面
图2 数据表
如上图1所示:在组名texbox中输入数值,点击添加可以插入ACCESS数据库表中,然后左边的LIstbox显示对应表中GroupNameSet字段所有记录,当选择listbox表中某一数值可以进行删除与修改(实际是对数据库操作)。请各位大侠给予详细代码,再此深表感谢!!!
求高手给予详细代码呀!!! 展开
图2 数据表
如上图1所示:在组名texbox中输入数值,点击添加可以插入ACCESS数据库表中,然后左边的LIstbox显示对应表中GroupNameSet字段所有记录,当选择listbox表中某一数值可以进行删除与修改(实际是对数据库操作)。请各位大侠给予详细代码,再此深表感谢!!!
求高手给予详细代码呀!!! 展开
3个回答
展开全部
1.添加:点击添加按钮的时候调用NewData(this.textbox1.Text)方法;
2.删除:点击删除按钮的时候调用DeleteData(this.pkid);//pkid 是表的主键,通过双击listbox的item时获取;
3.修改:首先在双击listbox的时候,获取到带有pkid的整条记录,然后将该记录的GroupNameSet内容赋值给textbox1.Text,然后可在textbox1.Text上修改该内容,修改后,通过点击修改按钮,调用updateData(this.textbox1.Text)方法来更新数据;
下面是操作access的4个方法:
-----------------------------------------------------
1.insert
public void NewData(String GroupNameSet)
{
String sql = "insert into mytable(GroupNameSet) values(@GroupNameSet)";
OleDbConnection cn = new OleDbConnection(_ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
cmd.Parameters.AddWithValue("@GroupNameSet", GroupNameSet);
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
2.update
public void UpdateData(String GroupNameSet,String pkid)
{
String sql = "update mytable set GroupNameSet=@GroupNameSet where pkid=@pkid";
OleDbConnection cn = new OleDbConnection(_ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
cmd.Parameters.AddWithValue("@GroupNameSet", GroupNameSet);
cmd.Parameters.AddWithValue("@pkid", pkid);
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
3.delete
public void DeleteData(String pkid)
{
String sql = "delete from mytable where pkid=@pkid";
OleDbConnection cn = new OleDbConnection(_ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
cmd.Parameters.AddWithValue("@pkid", pkid);
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
4.select
public DataTable GetData()
{
//从数据库中读出数据
String sql = "select * from mytable";
OleDbDataAdapter da = new OleDbDataAdapter(sql, new OleDbConnection(_ConnectionString));
//da.SelectCommand.Parameters.Add(p0);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
catch (Exception err)
{
return null;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
5.connection string:
public static String ConnectionString_unEncry
{
get
{
String str = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}\myAccessDatabase.mdb", System.Windows.Forms.Application.StartupPath);
return str;
}
}
2.删除:点击删除按钮的时候调用DeleteData(this.pkid);//pkid 是表的主键,通过双击listbox的item时获取;
3.修改:首先在双击listbox的时候,获取到带有pkid的整条记录,然后将该记录的GroupNameSet内容赋值给textbox1.Text,然后可在textbox1.Text上修改该内容,修改后,通过点击修改按钮,调用updateData(this.textbox1.Text)方法来更新数据;
下面是操作access的4个方法:
-----------------------------------------------------
1.insert
public void NewData(String GroupNameSet)
{
String sql = "insert into mytable(GroupNameSet) values(@GroupNameSet)";
OleDbConnection cn = new OleDbConnection(_ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
cmd.Parameters.AddWithValue("@GroupNameSet", GroupNameSet);
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
2.update
public void UpdateData(String GroupNameSet,String pkid)
{
String sql = "update mytable set GroupNameSet=@GroupNameSet where pkid=@pkid";
OleDbConnection cn = new OleDbConnection(_ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
cmd.Parameters.AddWithValue("@GroupNameSet", GroupNameSet);
cmd.Parameters.AddWithValue("@pkid", pkid);
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
3.delete
public void DeleteData(String pkid)
{
String sql = "delete from mytable where pkid=@pkid";
OleDbConnection cn = new OleDbConnection(_ConnectionString);
OleDbCommand cmd = new OleDbCommand(sql, cn);
cmd.Parameters.AddWithValue("@pkid", pkid);
if (cn.State != ConnectionState.Open)
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
throw err;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
4.select
public DataTable GetData()
{
//从数据库中读出数据
String sql = "select * from mytable";
OleDbDataAdapter da = new OleDbDataAdapter(sql, new OleDbConnection(_ConnectionString));
//da.SelectCommand.Parameters.Add(p0);
try
{
if (cn.State != ConnectionState.Open)
cn.Open();
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
catch (Exception err)
{
return null;
}
finally
{
if (cn.State != ConnectionState.Closed)
cn.Close();
}
}
5.connection string:
public static String ConnectionString_unEncry
{
get
{
String str = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}\myAccessDatabase.mdb", System.Windows.Forms.Application.StartupPath);
return str;
}
}
更多追问追答
追问
请问一下:listbox中如何显示字段GroupName所显示的所有字段值,如何实现?谢了呀!
追答
private void Init_ListBox()
{
this.ListBox1.Items.Clear();
DataTable dt=GetData();
if(dt!=null&&dt.Rows.Count>0)
{
foreach(DataRow dr in dt.Rows)
{
this.ListBox1.Items.Add(dr["GroupName"]);
}
}
}
------------------------------------------或者
private void Init_ListBox2()
{
DataTable dt=GetData();
this.listBox1.DataSource = dt;
this.listBox1.DisplayMember = "GroupName";//显示的字段
this.listBox1.ValueMember = "pkid";//唯一值字段
}
展开全部
//通过listbox1.DataSource 来初始化其Items中的值
DataTable table = GetTable();// 这里省略了从Access中获取表数据的代码
listBox1.DataSource = table;
listBox1.DisplayMember = "要显示的字段名称";
DataTable table = GetTable();// 这里省略了从Access中获取表数据的代码
listBox1.DataSource = table;
listBox1.DisplayMember = "要显示的字段名称";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
插入代码:insert into groupnameset (数据库字段1) values(textbox中的值);
插入完成后,检索数据库中的数值到你得Listbox中
插入完成后,检索数据库中的数值到你得Listbox中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询