8,833
社区成员




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace phoneBook1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public DataSet ds;
public DataRow[] dr;
public int i = 0;
public void update()
{
dgv.DataSource = ds.Tables[0];
}
private void Form1_Load(object sender, EventArgs e)
{
enter form = new enter();
form.ShowDialog();
if (enter.flag == 0)
{
this.Close();
}
dgv.DataSource = bandsource().Tables[0];
dgv.Visible = false;
}
}
public Form1()
{
InitializeComponent();
}
public static DataSet ds;
public static DataRow[] dr;
public static OleDbDataAdapter da;
public static int i = 0;
public static int n = 1;
//更新数据
public void updategvd()
{
dgv.DataSource = ds.Tables[0];
}
//获得textbox中内容
public string[] textGet()
{
string[] str=new string[6];
str[0] = Name.Text.ToString(); str[1] = Sex.Text.ToString(); str[2] = HomeTel.Text.ToString();
str[3] = OfficeTel.Text.ToString(); str[4] = Mark.Text.ToString();
return str;
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“telephoneDataSet.telephoneinfo”中。您可以根据需要移动或删除它。
this.telephoneinfoTableAdapter.Fill(this.telephoneDataSet.telephoneinfo);//自动添加??
enter form = new enter();
form.ShowDialog();
if (enter.flag == 0)
{
this.Close();
}
dgv.DataSource = bandsource().Tables["phone"];
}
public DataSet bandsource()
{
string str = "provider=Microsoft.Jet.OLEDB.4.0; data Source=telephone.mdb; Persist Security Info=False";
OleDbConnection conn = new OleDbConnection(str);
ds = new DataSet();
try
{
conn.Open();
string sqlstr = "select telephoneinfo.PersonID as 自动编号,telephoneinfo.Name as 姓名,telephoneinfo.Sex as 性别,telephoneinfo.OfficeTel as 办公室电话,telephoneinfo.HomeTel as 家庭电话,telephoneinfo.Mark as 备注 from telephoneinfo order by PersonID";
da = new OleDbDataAdapter(sqlstr, conn);
da.Fill(ds, "phone");
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
conn.Close();
}
return ds;
}
private void save_Click(object sender, EventArgs e)
{
OleDbCommandBuilder builder = new OleDbCommandBuilder(da);
da.Update(ds, "phone");
ds.AcceptChanges();
}
private void updateData_Click(object sender, EventArgs e)
{
ds.Clear();
da.Fill(ds, "phone");
dgv.DataSource = ds.Tables["phone"];
}
其中我通过update来更新数据dataset是显示的是修改之后的结果,但是当我打开数据库时,发现数据并没有更新。public Form1()
{
InitializeComponent();
}
public static DataSet ds;
public static DataRow[] dr;
public static OleDbDataAdapter da;
public static int i = 0;
public static int n = 1;
//更新数据
public void updategvd()
{
dgv.DataSource = ds.Tables[0];
}
//获得textbox中内容
public string[] textGet()
{
string[] str=new string[6];
str[0] = Name.Text.ToString(); str[1] = Sex.Text.ToString(); str[2] = HomeTel.Text.ToString();
str[3] = OfficeTel.Text.ToString(); str[4] = Mark.Text.ToString();
return str;
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“telephoneDataSet.telephoneinfo”中。您可以根据需要移动或删除它。
this.telephoneinfoTableAdapter.Fill(this.telephoneDataSet.telephoneinfo);//自动添加??
enter form = new enter();
form.ShowDialog();
if (enter.flag == 0)
{
this.Close();
}
dgv.DataSource = bandsource().Tables["phone"];
}
public DataSet bandsource()
{
string str = "provider=Microsoft.Jet.OLEDB.4.0; data Source=telephone.mdb; Persist Security Info=False";
OleDbConnection conn = new OleDbConnection(str);
ds = new DataSet();
try
{
conn.Open();
string sqlstr = "select telephoneinfo.PersonID as 自动编号,telephoneinfo.Name as 姓名,telephoneinfo.Sex as 性别,telephoneinfo.OfficeTel as 办公室电话,telephoneinfo.HomeTel as 家庭电话,telephoneinfo.Mark as 备注 from telephoneinfo order by PersonID";
da = new OleDbDataAdapter(sqlstr, conn);
da.Fill(ds, "phone");
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
conn.Close();
}
return ds;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace phoneBook1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public DataSet ds;
public DataRow[] dr;
public int i = 0;
public void update()
{
dgv.DataSource = ds.Tables[0];
}
private void Form1_Load(object sender, EventArgs e)
{
enter form = new enter();
form.ShowDialog();
if (enter.flag == 0)
{
this.Close();
}
dgv.DataSource = bandsource().Tables[0];
dgv.Visible = false;
}
public DataSet bandsource()
{
string str = "provider=Microsoft.Jet.OLEDB.4.0; data Source=telephone.mdb; Persist Security Info=False";
OleDbConnection conn = new OleDbConnection(str);
DataSet ds = new DataSet();
try
{
conn.Open();
string sqlstr = "select PersonID ,Name,Sex,OfficeTel,HomeTel,Mark from telephoneinfo order by PersonID";
OleDbDataAdapter da = new OleDbDataAdapter(sqlstr, conn);
da.Fill(ds, "phone");
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
conn.Close();
}
return ds;
}
}