这么简单还要添加东西吗?
//构造
public customer(bool isNew)
{
this.isNew = isNew;
InitializeComponent();
}
//add按钮事件
private void btnAdd_Click(object sender, EventArgs e)
{
customer cm = new customer(true);
cm.Show();
}
//load事件
private void customer_Load_1(object sender, EventArgs e)
{
if (isNew)
{
foreach (var c in this.Controls)
{
if (c is Button)
{
var btn = c as Button;
if (btn != null)
{
btn.Visible = false;
}
}
}
Button btnAdd = new Button();
btnAdd.Name = "btnAdd";
btnAdd.Text = "Ok";
btnAdd.Click += new EventHandler(DataAdd_Click);
this.Controls.Add(btnAdd);
return;
}
//这里是楼主写的代码给我省略了
}
//添加数据
private void DataAdd_Click(object sender, EventArgs e)
{
string sql="";
sql ="Insert Into customer (CustomerID,FirstName,LastName,OfficePhone,HomePhone,MobilePhone,HKID)" ;
sql += "Select '" + txtCustomerID.Text + "','" + txtFirstName.Text + "','" + txtLastName.Text + "','" + txtOfficePhone.Text + "','" + txtHomePhone.Text + "','" + txtMoblie.Text + "','" + txtHKID.Text + "'";
using (connTotour = new OleDbConnection(connStr))
{
try
{
OleDbCommand cmd = new OleDbCommand(sql, connTotour);
connTotour.Open();
if (cmd.ExecuteNonQuery() != 0)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("faile");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connTotour.Close();
}
}
}
这样就ok了
是不是很简单