我正在尝试使用currencymanager addnew方法向我的数据库添加新记录,但它没有更新数据库,这里是完整的代码。
namespace currencyManager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
CurrencyManager cm;
private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection("data source=COUNTER16-PC,initial catalog=myDatabase;integrated security=true");
con.Open();
cmd = new SqlCommand("Select * from Students", con);
da = new SqlDataAdapter();
da.SelectCommand = cmd;
dt = new DataTable();
da.Fill(dt);
cm = (CurrencyManager)BindingContext[dt];
txtID.DataBindings.Add("text",dt,"ID");
txtFname.DataBindings.Add("text", dt, "FirstName");
txtLname.DataBindings.Add("text", dt, "LASTNAME");
txtState.DataBindings.Add("text", dt, "STATE");
txtPhone.DataBindings.Add("text", dt, "PHONE");
txtEmail.DataBindings.Add("text", dt, "EMAIL");
txtGrade.DataBindings.Add("text", dt, "GRADEYEAR");
txtGrade.DataBindings.Add("text", dt, "GPA");
txtProgram.DataBindings.Add("text", dt, "PROGRAM");
txtNews.DataBindings.Add("text", dt, "NEWLETTER");
}
private void btn_first_Click(object sender, EventArgs e)
{
cm.Position = 0;
}
private void btn_prev_Click(object sender, EventArgs e)
{
cm.Position--;
}
private void btn_next_Click(object sender, EventArgs e)
{
cm.Position++;
}
private void btn_last_Click(object sender, EventArgs e)
{
cm.Position = cm.Count - 1;
}
private void btn_add_Click(object sender, EventArgs e)
{
cm.AddNew();
}
private void btn_save_Click(object sender, EventArgs e)
{
cm.EndCurrentEdit();
}
private void btn_update_Click(object sender, EventArgs e)
{
}
private void btn_delete_Click(object sender, EventArgs e)
{
cm.RemoveAt(cm.Position);
}
}
}
提前谢谢