try
{
con.Open();
OracleCommand cmd = new OracleCommand("insert into test_table (item_no, item_name, serial) values('A123', 't-bone', '" + textBox1.Text + "')", con);
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
System.Data.DataTable dt = new System.Data.DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
MessageBox.Show("Serial #" + textBox1.Text + " has been added.");
button1_Click(sender, e);
}
这是我用来插入单行数据并且序列号从0001到9999开始的代码。我从textBox1获得序列号,但是如果我想制作textBox2并使用textBox1和textBox2之间的范围将多行插入到datagridview中,我应该这样做吗?如果是textBox1 = 0001和textBox = 0010,它将插入10行相同的信息,但序列号将在0001和0010之间。
请帮助。
答案 0 :(得分:0)
只要有许多更好的方法,您都可以通过这样的方法来实现。
try
{
con.Open();
int first = convert.toint32 (textBox1.Text);
int second = convert.toint32 (textBox2.Text);
for (int i = first; i <= second;i++)
{
OracleCommand cmd = new
OracleCommand("insert into test_table (item_no,
item_name, serial) values('A123', 't-bone', '" + i.toString ()+
"')", con);
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
System.Data.DataTable dt = new
System.Data.DataTable();
dt.Load(dr);
}
dataGridView1.DataSource = dt;
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
MessageBox.Show("Serial #" + textBox1.Text
+ " has been added.");
button1_Click(sender, e);
}