我必须使用按钮,即Insert
和View
。问题是每次我按Insert
以便将数据存入我的数据库时,必须按下按钮View
才能查看存储在我的数据库中的数据并显示在dataGridView
中
我想要的是每次按下Insert
按钮,用户都不需要按View
按钮。我不知道如何将View
的代码插入Insert
按钮。
这是我Insert
的代码:
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customerOrder values('"+txtFname.Text+"','"+txtLname.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
txtFname.Text = "";
txtLname.Text = "";
MessageBox.Show("Inserted Successfully!");
}
这是View
的代码:
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from customerOrder";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
答案 0 :(得分:1)
将它们封装在单独的方法中,如
public void InsertData()
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customerOrder values('"+txtFname.Text+"','"+txtLname.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
txtFname.Text = "";
txtLname.Text = "";
MessageBox.Show("Inserted Successfully!");
}
Public void ViewData()
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from customerOrder";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
现在,在Insert事件处理程序中,将它们称为
protected void Insert_Click(object sender, EventArgs e)
{
InsertData();
ViewData();
}
答案 1 :(得分:1)
如果您希望为视图事件调用Procdure,可以使用
private void cmd_Insert_Click(object sender, EventArgs e)
{
/**Do insert here **//
cmd_View_Click(sender, e);
}
private void cmd_View_Click(object sender, EventArgs e)
{
/**Do View here **//
}
答案 2 :(得分:0)
将每个代码放在单独的方法上,例如:
void Insert()
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customerOrder values(@Fname,@Lname)";
cmd.Parameters.Add("@Fname",SqlDbType.NVarchar).Value = txtFname.Text;
cmd.Parameters.Add("@Lname",SqlDbType.NVarchar).Value = txtLname.Text;
cmd.ExecuteNonQuery();
con.Close();
txtFname.Text = "";
txtLname.Text = "";
MessageBox.Show("Inserted Successfully!");
}
void View()
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from customerOrder";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
然后在“插入”按钮上单击“是”:
private void btnInsert_Click(object sender, EventArgs e)
{
Insert();
View();
}
另请注意在Insert Method上使用 SQL参数。使用它们而不是连接字符串
非常重要答案 3 :(得分:0)
您只需将View
的代码插入 insertButton的代码即可。
private void btnInsert_Click(object sender, EventArgs e)
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customerOrder values('" + txtFname.Text + "','" + txtLname.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
txtFname.Text = "";
txtLname.Text = "";
MessageBox.Show("Inserted Successfully!");
FillGridView();
}
private void FillGridView()
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from customerOrder";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
答案 4 :(得分:0)
void InsertButton_Click(Object sender, EventArgs e)
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customerOrder values('"+txtFname.Text+"','"+txtLname.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
txtFname.Text = "";
txtLname.Text = "";
MessageBox.Show("Inserted Successfully!");
ViewButton_Click(sender, e)
}
void ViewButton_Click(Object sender, EventArgs e)
{
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from customerOrder";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}