C#如何动态添加以下控件,并读取动态添加控件的内容写到数据库? 20
C#如何动态添加以下控件,并读取动态添加控件的内容写到数据库?
然后在另一个窗口中,从数据库读取动态控件写入的内容,动态创建控件,并显示内容
控件:
textbox,Label,textbox,label,textbox,checkbox1,checkbox2,checkbox3,label,textbox,combobox
combobox的类型为:DropDownList
控件图片: 展开
//查询
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();
}
1.先要获取文本中的值;
2.把值添加到数据库中;
3.在另一个窗体中读出;
其实学过基础C#的人都会哦。、,这个无非是对数据库的操作。
要是代码不会的话,再问我吧。
第二窗口个按你的意思是首先得创建数据库,然后在Form_Load()中连接数据库,读取一条一条记录,再根据记录是否空确定new 控件。怎么这么多动态
通过一个一般处理程序或者一个.aspx文件,在一般处理程序中接受参数值,再把参数值传递给定义的变量,变量的类型随便你选择。
string name= lb.text;
详细的我会给你个登录的例子。