发布网友 发布时间:2022-04-07 17:59
共5个回答
懂视网 时间:2022-04-07 22:20
代码添加控件-从数据库提取数据
标签:
热心网友 时间:2022-04-07 19:28
//查询
string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Student.mdb";
OleDbConnection conn = new OleDbConnection(ConnStr);
string SQLStr = "select * from student ";
OleDbCommand com = new OleDbCommand();
com.Connection = conn;
com.CommandType = CommandType.Text;
com.CommandText = SQLStr;
try
{
conn.Open();
}
catch
{
MessageBox.Show("数据库连接失败!", "出错");
return ;
}
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(com);
da.Fill(ds ,"stu");
conn.Close();
this.dataGridView1.DataSource = ds.Tables["stu"].DefaultView;
//修改2
string ConnStr = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=Student.mdb";
OleDbConnection conn = new OleDbConnection(ConnStr);
string SQLStr = "update student set Name=@Name,Sex=@Sex,ClassID=@ClassID,Birthday=@Birthday,Native=@Native where StudentID=@StudentID";
OleDbCommand com = new OleDbCommand();
com.Connection = conn;
com.CommandType = CommandType.Text;
com.CommandText = SQLStr;
OleDbParameter[] paras = new OleDbParameter[6];
paras[0] = new OleDbParameter("@Name", OleDbType.VarChar, 10);
paras[0].Value = this.textBox18.Text.Trim();
paras[1] = new OleDbParameter("@Sex", OleDbType.VarChar, 2);
paras[1].Value = this.textBox19.Text.Trim();
paras[2] = new OleDbParameter("@ClassID", OleDbType.VarChar, 6);
paras[2].Value = this.textBox16.Text.Trim();
paras[3] = new OleDbParameter("@Birthday", OleDbType.Date);
paras[3].Value = this.textBox17.Text.Trim();
paras[4] = new OleDbParameter("@Native", OleDbType.VarChar, 20);
paras[4].Value = this.textBox15.Text.Trim();
paras[5] = new OleDbParameter("@StudentID", OleDbType.VarChar, 8);
paras[5].Value = this.textBox20.Text.Trim();
for (int i = 0; i < paras.Length; i++)
{
com.Parameters.Add(paras[i]);
}
try
{
conn.Open();
}
catch
{
MessageBox.Show("数据库连接失败!", "出错");
return;
}
int k;
k = com.ExecuteNonQuery();
conn.Close();
if (k > 0)
MessageBox.Show("数据修改成功!");
else
MessageBox.Show("数据修改失败!");
private void dataGridView1_Click(object sender, EventArgs e)
{//单击网络时当前记录在下面显示并修改
this.textBox20.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
this.textBox18.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
this.textBox19.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
this.textBox16.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
this.textBox17.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
this.textBox15.Text = this.dataGridView1.CurrentRow.Cells[5].Value.ToString();
}
热心网友 时间:2022-04-07 20:46
思路:热心网友 时间:2022-04-07 22:21
第一个窗口中动态的控件是什么意思?不太明白,是数据库有几项内容创建几个控件吗?热心网友 时间:2022-04-08 00:12
给你举个例子:Lable lb=new Lable();