求注释程序!
namespace SoccerVIP
{
public partial class Form1 : Form
{
string tID = "";
DataSet myDataSet;
OleDbConnection myOleConn;
OleDbDataAdapter myDataAdapter;
public void RefreshDG()
{
if (myDataSet.Tables.Contains("SoccerClub"))
{
myDataSet.Tables.Remove("SoccerClub");
}
string sql = "select * from SoccerClub ";
myDataAdapter = new OleDbDataAdapter(sql, myOleConn);
myDataAdapter.Fill(myDataSet, "SoccerClub");
dataGridViewSoccer.DataSource = myDataSet.Tables["SoccerClub"];
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
btnShow_Click_1(sender, e);
btnModify.Enabled = false;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if((txtFirstname.Text=="")||(txtLastname.Text=="")||(txtTel.Text=="")||(txtAddress.Text=="")||(txtFavoriteTeam.Text=="")||(txtRentTime.Text=="")||(txtCoach .Text==""))
{
MessageBox.Show("不能为空");
return;
}
string sql = "insert into SoccerClub(firstName,lastName,tel,address,favoriteTeam,rentTime,coach) values('" + txtFirstname.Text + "','"
+txtLastname.Text+"',"+txtTel.Text+",'"+txtAddress.Text+"','"+txtFavoriteTeam.Text+"','"+txtRentTime.Text +"',"+txtCoach.Text+")";
try
{
OleDbCommand mycomn=new OleDbCommand(sql,myOleConn);
mycomn.ExecuteNonQuery();
RefreshDG();
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
txtFirstname.Text = "";
txtLastname.Text = "";
txtTel.Text = "";
txtAddress.Text = "";
txtFavoriteTeam.Text = "";
txtRentTime.Text = "";
txtCoach.Text ="";
}
private void btnDel_Click(object sender, EventArgs e)
{
int txtnumber = Convert.ToInt32(dataGridViewSoccer["tID", dataGridViewSoccer.CurrentCell.RowIndex].Value);
string sql = "delete from SoccerClub where tID=" + txtnumber.ToString();
try
{
OleDbCommand mycomm = new OleDbCommand(sql, myOleConn);
mycomm.ExecuteNonQuery();
RefreshDG();
}
catch (Exception ea)
{
MessageBox.Show(ea.Message);
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
tID = dataGridViewSoccer["tID", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtFirstname.Text = dataGridViewSoccer["firstName", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtLastname.Text = dataGridViewSoccer["lastName", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtTel.Text = dataGridViewSoccer["tel", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtAddress.Text = dataGridViewSoccer["address", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtFavoriteTeam.Text = dataGridViewSoccer["favoriteTeam", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtRentTime.Text = dataGridViewSoccer["rentTime", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtCoach.Text = dataGridViewSoccer["coach", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
btnModify.Enabled = true;
btnDel.Enabled = false;
btnEdit.Enabled = false;
btnAdd.Enabled = false;
}
private void btnModity_Click(object sender, EventArgs e)
{
if (tID == "")
{
MessageBox.Show("tID is error");
return;
}
string sql = "update SoccerClub set firstName='" + txtFirstname.Text + "',lastName='" + txtLastname.Text + "',tel=" + txtTel.Text + ",address='" + txtAddress.Text + "',favoriteTeam='" + txtFavoriteTeam.Text + "',rentTime='" + txtRentTime.Text + "',coach=" + txtCoach.Text + " where tID=" + tID;
try
{
OleDbCommand mycomn = new OleDbCommand(sql, myOleConn);
mycomn.ExecuteNonQuery();
RefreshDG();
txtFirstname.Text = "";
txtLastname.Text = "";
txtTel.Text = "";
txtAddress.Text = "";
txtFavoriteTeam.Text = "";
txtRentTime.Text = "";
txtCoach.Text = "";
}
catch (Exception eb)
{
MessageBox.Show(eb.Message);
}
btnAdd.Enabled = true;
btnDel.Enabled = true;
btnEdit.Enabled = true;
btnModify.Enabled = false;
}
private void btnShow_Click_1(object sender, EventArgs e)
{
try
{
string ConStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ System.Environment.CurrentDirectory + @"\dbSoccer.mdb ";
myOleConn = new OleDbConnection(ConStr);
myOleConn.Open();
}
catch (Exception ec)
{
MessageBox.Show(ec.Message);
}
string sql = "select*from SoccerClub";
myDataSet = new DataSet();
myDataAdapter = new OleDbDataAdapter(sql, myOleConn);
myDataAdapter.Fill(myDataSet, "SoccerClub");
dataGridViewSoccer.DataSource = myDataSet.Tables["SoccerClub"];
}
private void btnQuery_Click(object sender, EventArgs e)
{
Form2 myF = new Form2();
myF.ShowDialog();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
[解决办法]
namespace SoccerVIP
{
public partial class Form1 : Form
{
string tID = "";
DataSet myDataSet;
OleDbConnection myOleConn;
OleDbDataAdapter myDataAdapter;
public void RefreshDG()
{
//判断是否包含这个SoccerClub表,包含则删除。重新绑定追加
if (myDataSet.Tables.Contains("SoccerClub"))
{
myDataSet.Tables.Remove("SoccerClub");
}
string sql = "select * from SoccerClub ";//查询
myDataAdapter = new OleDbDataAdapter(sql, myOleConn);//执行
myDataAdapter.Fill(myDataSet, "SoccerClub");
dataGridViewSoccer.DataSource = myDataSet.Tables["SoccerClub"];//绑定
}
public Form1()
{
InitializeComponent();
}
//窗体初始化操作
private void Form1_Load(object sender, EventArgs e)
{
btnShow_Click_1(sender, e);
btnModify.Enabled = false;
}
private void btnAdd_Click(object sender, EventArgs e)
{
//判断要插入的文本框是否为空
if((txtFirstname.Text=="")
[解决办法]
(txtLastname.Text=="")
[解决办法]
(txtTel.Text=="")
[解决办法]
(txtAddress.Text=="")
[解决办法]
(txtFavoriteTeam.Text=="")
[解决办法]
(txtRentTime.Text=="")
[解决办法]
(txtCoach .Text==""))
{
MessageBox.Show("不能为空");return;
}
//拼接SQL语句insert
string sql = "insert into SoccerClub(firstName,lastName,tel,address,favoriteTeam,rentTime,coach) values('" + txtFirstname.Text + "','"
+txtLastname.Text+"',"+txtTel.Text+",'"+txtAddress.Text+"','"+txtFavoriteTeam.Text+"','"+txtRentTime.Text +"',"+txtCoach.Text+")";
try
{
OleDbCommand mycomn=new OleDbCommand(sql,myOleConn);
mycomn.ExecuteNonQuery();//插入数据的处理
RefreshDG();//插入数据后绑定
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}
//一堆清空文本框处理
txtFirstname.Text = "";
txtLastname.Text = "";
txtTel.Text = "";
txtAddress.Text = "";
txtFavoriteTeam.Text = "";
txtRentTime.Text = "";
txtCoach.Text ="";
}
//删除数据的事件
private void btnDel_Click(object sender, EventArgs e)
{//要删除的ID
int txtnumber = Convert.ToInt32(dataGridViewSoccer["tID", dataGridViewSoccer.CurrentCell.RowIndex].Value);
string sql = "delete from SoccerClub where tID=" + txtnumber.ToString();//删除SQL
try
{
OleDbCommand mycomm = new OleDbCommand(sql, myOleConn);
mycomm.ExecuteNonQuery();//执行删除SQL处理
RefreshDG();//删除后重新绑定
}
catch (Exception ea)
{
MessageBox.Show(ea.Message);
}
}
//编辑事件
private void btnEdit_Click(object sender, EventArgs e)
{
tID = dataGridViewSoccer["tID", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();//要编辑的ID
//取出要编辑的值显示在一堆文本框上面
txtFirstname.Text = dataGridViewSoccer["firstName", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtLastname.Text = dataGridViewSoccer["lastName", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtTel.Text = dataGridViewSoccer["tel", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtAddress.Text = dataGridViewSoccer["address", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtFavoriteTeam.Text = dataGridViewSoccer["favoriteTeam", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtRentTime.Text = dataGridViewSoccer["rentTime", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
txtCoach.Text = dataGridViewSoccer["coach", dataGridViewSoccer.CurrentCell.RowIndex].Value.ToString();
btnModify.Enabled = true;
btnDel.Enabled = false;
btnEdit.Enabled = false;
btnAdd.Enabled = false;
}
//更新操作事件
private void btnModity_Click(object sender, EventArgs e)
{
if (tID == "")
{
MessageBox.Show("tID is error");
return;
}
//拼接更新SQL
string sql = "update SoccerClub set firstName='" + txtFirstname.Text + "',lastName='" + txtLastname.Text + "',tel=" + txtTel.Text + ",address='" + txtAddress.Text + "',favoriteTeam='" + txtFavoriteTeam.Text + "',rentTime='" + txtRentTime.Text + "',coach=" + txtCoach.Text + " where tID=" + tID;
try
{
OleDbCommand mycomn = new OleDbCommand(sql, myOleConn);
mycomn.ExecuteNonQuery();//执行SQL操作
RefreshDG();//绑定
//下面一堆清空文本框的操作
txtFirstname.Text = "";
txtLastname.Text = "";
txtTel.Text = "";
txtAddress.Text = "";
txtFavoriteTeam.Text = "";
txtRentTime.Text = "";
txtCoach.Text = "";
}
catch (Exception eb)
{
MessageBox.Show(eb.Message);
}
//一堆禁用和启用控件的处理
btnAdd.Enabled = true;
btnDel.Enabled = true;
btnEdit.Enabled = true;
btnModify.Enabled = false;
}
private void btnShow_Click_1(object sender, EventArgs e)
{
try
{
//连接数据库字符串
string ConStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ System.Environment.CurrentDirectory + @"\dbSoccer.mdb ";
myOleConn = new OleDbConnection(ConStr);
myOleConn.Open();
}
catch (Exception ec)
{
MessageBox.Show(ec.Message);
}
string sql = "select*from SoccerClub";//查询
myDataSet = new DataSet();
myDataAdapter = new OleDbDataAdapter(sql, myOleConn);执行SQL语句
myDataAdapter.Fill(myDataSet, "SoccerClub");//返回结果
dataGridViewSoccer.DataSource = myDataSet.Tables["SoccerClub"];//绑定gridview
}
//打开新窗体
private void btnQuery_Click(object sender, EventArgs e)
{
//打开新窗体2
Form2 myF = new Form2();
myF.ShowDialog();
}
//退出按钮操作事件
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();//窗体关闭
}
}
}