c# 怎样datarow[] 填充到datatable中 跪求
展开全部
public bool CheckNull() //判断各个文本框是否为空
{
control = this;
for( int i = 0; i < control.Controls.Count; i++ )
{
if( control.Controls[i].Enabled
&&((control.Controls[i] is TextBox)
||(control.Controls[i] is ComboBox))
&& (control.Controls[i].Text.Trim() == "")
&& (control.Controls[i].Tag != null) )
{
if( control.Controls[i] is TextBox && ((TextBox)control.Controls[i]).ReadOnly)
{
continue;
}
MessageBox.Show((String)(control.Controls[i].Tag) + "不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
control.Controls[i].Text = "";
control.Controls[i].Focus();
return false;
}
}
return true;
}
//清除文本框内容
public void ClearContent()
{
control = this;
for( int i = 0; i < control.Controls.Count; i++ )
{
if( ( control.Controls[i] is TextBox ))
(control.Controls[i] as TextBox ).Text = "";
else if( control.Controls[i] is ComboBox )
( control.Controls[i] as ComboBox ).SelectedIndex = -1 ;
else if( control.Controls[i] is CheckBox )
( control.Controls[i] as CheckBox ).Checked = false;
}
}
private void SetButtonStatus( bool flag )//设置“添加”“保存”按钮的切换
{
btnAdd.Visible = flag;
btnSave.Visible = !flag;
}
private void SetControlsEnable(bool isEnable)//使控件变为可编辑(即可以输入文字)
{
control = this;
for( int i = 0; i < control.Controls.Count; i++)
{
if ( (control.Controls[i] is TextBox)
||(control.Controls[i] is ComboBox)
||(control.Controls[i] is PictureBox))
{
control.Controls[i].Enabled = isEnable;
}
}
}
public bool ExecuteSQL(String sql)
{
SqlConnection myConnection = new SqlConnection(ConnectionString); //连接数据库
String cmdText = sql;
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
try
{
myCommand.ExecuteNonQuery();
myConnection.Close();
return true;
}
catch (Exception ex)
{
myConnection.Close();
return false;
}
}
public DataSet GetDataSetBySql(String sql)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);//连接数据库
String cmdText = sql;
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);//DataAdapter的Fill方法用于使用DataAdapte的SelectCommand的结果来填充DataSet
myConnection.Open(); //打开数据库
DataSet ds = new DataSet(); //DataSet对象可以保存多个数据表DataTable的查询结果,
//DataSet不一定要连接到一个数据库上,它本身是一个离线数据,
//只有在对数据库进行编辑时才需要对数据库进行连接
da.Fill(ds);
return ds;
}
public void LoadStudent() //查找“学生”信息
{
sqlText = "select * from students";
dsStudent = GetDataSetBySql(sqlText);
dtStudent = dsStudent.Tables[0].Copy();
dtStudent.TableName = "student";
dgShowStudent.DataSource = dtStudent;
}
public void Loadserstu(String sqlstu )//输出查找的学生信息
{
String stext=sqlstu;
dsStudent = GetDataSetBySql(stext);
dtStudent = dsStudent.Tables[0].Copy();
dtStudent.TableName = "stu";
dgShowStudent.DataSource = dtStudent;
}
public void LoadDept()
{
String sqlText = "select * from Department";
dsDept = GetDataSetBySql(sqlText);
dtDept = dsDept.Tables[0].Copy();
// cmbDept.DataSource = dtDept;
foreach( DataRow row in dtDept.Rows )
cmbDept.Items.Add( row["Sdept"].ToString() );
}
private void CFrmInputStudent_Load(object sender, System.EventArgs e)
{
LoadStudent();
LoadDept();
SetControlsEnable(false);
btnSave.Enabled = true;
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
btndel.Enabled = false;
btndeled.Enabled = false;
btnup.Enabled = false;
btnuped.Enabled = false;
btnser.Enabled = false;
btnsered.Enabled = false;
SetControlsEnable(true);
SetButtonStatus( false );
ClearContent();
}
private void btnSave_Click(object sender, System.EventArgs e)
{
if(!CheckNull())
return;
strNumber = this.tbNumber.Text.Trim();
strAge = this.tbAge.Text.Trim();
strName = this.tbName.Text.Trim();
strSex = cmbSex.SelectedItem.ToString();
strDept = cmbDept.SelectedItem.ToString();
sqlText = "insert into students (Sno,Sname,Ssex,Sage,Sdept) values ('" + strNumber + "','"
+ strName +"','"
+ strSex +"','"
+ strAge +"','"
+ strDept +"')";
bool tag = ExecuteSQL(sqlText);
if (!tag)
{
MessageBox.Show("插入失败!");
return;
}
btnsered.Enabled = true;
btnser.Enabled = true;
btndel.Enabled = true;
btndeled.Enabled = true;
btnup.Enabled = true;
btnuped.Enabled = true;
LoadStudent();
SetButtonStatus( true );
SetControlsEnable( false);
ClearContent();
}
private void btnClose_Click(object sender, System.EventArgs e)
{
this.Close();
this.Dispose();
}
private void tbAge_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
String strAge = tbAge.Text;
for( int i = 0; i < strAge.Length; i++ )
{
if( strAge[i] >= '0' && strAge[i] <= '9')
continue;
else
{
MessageBox.Show( "代码必须是数字!","错误", MessageBoxButtons.OK, MessageBoxIcon.Error );
tbAge.SelectAll();
e.Cancel = true;
return;
}
}
}
private void btnser_Click(object sender, EventArgs e)
{
btndeled.Enabled = false;
btndel.Enabled = false;
btnup.Enabled = false;
btnuped.Enabled = false;
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnser.Visible = false;
SetControlsEnable(true);
}
private void btnsered_Click(object sender, EventArgs e)
{
strNumber = this.tbNumber.Text.Trim();
sqlText = "select * from students where sno='" + strNumber + "' ";
bool tag = ExecuteSQL(sqlText);
if (!tag)
{
MessageBox.Show("没有相关信息");
return;
}
btnser.Visible = true;
btnAdd.Enabled = true;
btndel.Enabled = true;
btndeled.Enabled = true;
btnup.Enabled = true;
btnuped.Enabled = true;
btnser.Enabled = true;
btnsered.Enabled = true;
Loadserstu(sqlText);
SetControlsEnable(false);
ClearContent();
}
private void btndel_Click(object sender, EventArgs e)
{
btnup.Enabled = false;
btnuped.Enabled = false;
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnser.Enabled = false;
btnsered.Enabled = false;
btndel.Visible = false;
tbNumber.Enabled = true;
}
private void btndeled_Click(object sender, EventArgs e)
{
strNumber = this.tbNumber.Text.Trim();
sqlText = "delete from students where sno='" + strNumber + "'";
MessageBox.Show("是否删除","注意!", MessageBoxButtons.YesNo);
bool tag = ExecuteSQL(sqlText);
if(!tag)
{
MessageBox.Show("删除出错!");
return;
}
LoadStudent();
ClearContent();
btnAdd.Enabled = true;
btnSave.Enabled = true;
btnser.Enabled = true;
btnsered.Enabled = true;
btndel.Visible=true;
tbNumber.Enabled = false;
btnup.Enabled = true;
btnuped.Enabled = true;
}
private void btnup_Click(object sender, EventArgs e)
{
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnser.Enabled = false;
btnsered.Enabled = false;
btndel.Enabled = false;
btndeled.Enabled = false;
btnup.Visible = false;
SetControlsEnable(true);
}
private void btnuped_Click(object sender, EventArgs e)
{
if (!CheckNull())
return;
strNumber = tbNumber.Text.Trim();
strName = tbName.Text.Trim();
strSex = cmbSex.SelectedItem.ToString();
strAge = tbAge.Text.Trim();
strDept = cmbDept.SelectedItem.ToString();
sqlText = "update students set sname='" + strName + "', ssex='" + strSex + "', sage='" + strAge + "',sdept='" + strDept + "'where sno='" + strNumber + "';";
bool tag = ExecuteSQL(sqlText);
if(!tag)
{
MessageBox.Show("修改出错!");
return;
}
LoadStudent();
ClearContent();
SetControlsEnable(false);
btnAdd.Enabled=true;
btnSave.Enabled=true;
btnser.Enabled=true;
btnsered.Enabled=true;
btndel.Enabled=true;
btndeled.Enabled=true;
btnup.Visible=true;
}
}
}
学生管理系统 做的不全。。 BUG 存在很多。。。
{
control = this;
for( int i = 0; i < control.Controls.Count; i++ )
{
if( control.Controls[i].Enabled
&&((control.Controls[i] is TextBox)
||(control.Controls[i] is ComboBox))
&& (control.Controls[i].Text.Trim() == "")
&& (control.Controls[i].Tag != null) )
{
if( control.Controls[i] is TextBox && ((TextBox)control.Controls[i]).ReadOnly)
{
continue;
}
MessageBox.Show((String)(control.Controls[i].Tag) + "不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
control.Controls[i].Text = "";
control.Controls[i].Focus();
return false;
}
}
return true;
}
//清除文本框内容
public void ClearContent()
{
control = this;
for( int i = 0; i < control.Controls.Count; i++ )
{
if( ( control.Controls[i] is TextBox ))
(control.Controls[i] as TextBox ).Text = "";
else if( control.Controls[i] is ComboBox )
( control.Controls[i] as ComboBox ).SelectedIndex = -1 ;
else if( control.Controls[i] is CheckBox )
( control.Controls[i] as CheckBox ).Checked = false;
}
}
private void SetButtonStatus( bool flag )//设置“添加”“保存”按钮的切换
{
btnAdd.Visible = flag;
btnSave.Visible = !flag;
}
private void SetControlsEnable(bool isEnable)//使控件变为可编辑(即可以输入文字)
{
control = this;
for( int i = 0; i < control.Controls.Count; i++)
{
if ( (control.Controls[i] is TextBox)
||(control.Controls[i] is ComboBox)
||(control.Controls[i] is PictureBox))
{
control.Controls[i].Enabled = isEnable;
}
}
}
public bool ExecuteSQL(String sql)
{
SqlConnection myConnection = new SqlConnection(ConnectionString); //连接数据库
String cmdText = sql;
SqlCommand myCommand = new SqlCommand(cmdText, myConnection);
myConnection.Open();
try
{
myCommand.ExecuteNonQuery();
myConnection.Close();
return true;
}
catch (Exception ex)
{
myConnection.Close();
return false;
}
}
public DataSet GetDataSetBySql(String sql)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);//连接数据库
String cmdText = sql;
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmdText, myConnection);//DataAdapter的Fill方法用于使用DataAdapte的SelectCommand的结果来填充DataSet
myConnection.Open(); //打开数据库
DataSet ds = new DataSet(); //DataSet对象可以保存多个数据表DataTable的查询结果,
//DataSet不一定要连接到一个数据库上,它本身是一个离线数据,
//只有在对数据库进行编辑时才需要对数据库进行连接
da.Fill(ds);
return ds;
}
public void LoadStudent() //查找“学生”信息
{
sqlText = "select * from students";
dsStudent = GetDataSetBySql(sqlText);
dtStudent = dsStudent.Tables[0].Copy();
dtStudent.TableName = "student";
dgShowStudent.DataSource = dtStudent;
}
public void Loadserstu(String sqlstu )//输出查找的学生信息
{
String stext=sqlstu;
dsStudent = GetDataSetBySql(stext);
dtStudent = dsStudent.Tables[0].Copy();
dtStudent.TableName = "stu";
dgShowStudent.DataSource = dtStudent;
}
public void LoadDept()
{
String sqlText = "select * from Department";
dsDept = GetDataSetBySql(sqlText);
dtDept = dsDept.Tables[0].Copy();
// cmbDept.DataSource = dtDept;
foreach( DataRow row in dtDept.Rows )
cmbDept.Items.Add( row["Sdept"].ToString() );
}
private void CFrmInputStudent_Load(object sender, System.EventArgs e)
{
LoadStudent();
LoadDept();
SetControlsEnable(false);
btnSave.Enabled = true;
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
btndel.Enabled = false;
btndeled.Enabled = false;
btnup.Enabled = false;
btnuped.Enabled = false;
btnser.Enabled = false;
btnsered.Enabled = false;
SetControlsEnable(true);
SetButtonStatus( false );
ClearContent();
}
private void btnSave_Click(object sender, System.EventArgs e)
{
if(!CheckNull())
return;
strNumber = this.tbNumber.Text.Trim();
strAge = this.tbAge.Text.Trim();
strName = this.tbName.Text.Trim();
strSex = cmbSex.SelectedItem.ToString();
strDept = cmbDept.SelectedItem.ToString();
sqlText = "insert into students (Sno,Sname,Ssex,Sage,Sdept) values ('" + strNumber + "','"
+ strName +"','"
+ strSex +"','"
+ strAge +"','"
+ strDept +"')";
bool tag = ExecuteSQL(sqlText);
if (!tag)
{
MessageBox.Show("插入失败!");
return;
}
btnsered.Enabled = true;
btnser.Enabled = true;
btndel.Enabled = true;
btndeled.Enabled = true;
btnup.Enabled = true;
btnuped.Enabled = true;
LoadStudent();
SetButtonStatus( true );
SetControlsEnable( false);
ClearContent();
}
private void btnClose_Click(object sender, System.EventArgs e)
{
this.Close();
this.Dispose();
}
private void tbAge_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
String strAge = tbAge.Text;
for( int i = 0; i < strAge.Length; i++ )
{
if( strAge[i] >= '0' && strAge[i] <= '9')
continue;
else
{
MessageBox.Show( "代码必须是数字!","错误", MessageBoxButtons.OK, MessageBoxIcon.Error );
tbAge.SelectAll();
e.Cancel = true;
return;
}
}
}
private void btnser_Click(object sender, EventArgs e)
{
btndeled.Enabled = false;
btndel.Enabled = false;
btnup.Enabled = false;
btnuped.Enabled = false;
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnser.Visible = false;
SetControlsEnable(true);
}
private void btnsered_Click(object sender, EventArgs e)
{
strNumber = this.tbNumber.Text.Trim();
sqlText = "select * from students where sno='" + strNumber + "' ";
bool tag = ExecuteSQL(sqlText);
if (!tag)
{
MessageBox.Show("没有相关信息");
return;
}
btnser.Visible = true;
btnAdd.Enabled = true;
btndel.Enabled = true;
btndeled.Enabled = true;
btnup.Enabled = true;
btnuped.Enabled = true;
btnser.Enabled = true;
btnsered.Enabled = true;
Loadserstu(sqlText);
SetControlsEnable(false);
ClearContent();
}
private void btndel_Click(object sender, EventArgs e)
{
btnup.Enabled = false;
btnuped.Enabled = false;
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnser.Enabled = false;
btnsered.Enabled = false;
btndel.Visible = false;
tbNumber.Enabled = true;
}
private void btndeled_Click(object sender, EventArgs e)
{
strNumber = this.tbNumber.Text.Trim();
sqlText = "delete from students where sno='" + strNumber + "'";
MessageBox.Show("是否删除","注意!", MessageBoxButtons.YesNo);
bool tag = ExecuteSQL(sqlText);
if(!tag)
{
MessageBox.Show("删除出错!");
return;
}
LoadStudent();
ClearContent();
btnAdd.Enabled = true;
btnSave.Enabled = true;
btnser.Enabled = true;
btnsered.Enabled = true;
btndel.Visible=true;
tbNumber.Enabled = false;
btnup.Enabled = true;
btnuped.Enabled = true;
}
private void btnup_Click(object sender, EventArgs e)
{
btnAdd.Enabled = false;
btnSave.Enabled = false;
btnser.Enabled = false;
btnsered.Enabled = false;
btndel.Enabled = false;
btndeled.Enabled = false;
btnup.Visible = false;
SetControlsEnable(true);
}
private void btnuped_Click(object sender, EventArgs e)
{
if (!CheckNull())
return;
strNumber = tbNumber.Text.Trim();
strName = tbName.Text.Trim();
strSex = cmbSex.SelectedItem.ToString();
strAge = tbAge.Text.Trim();
strDept = cmbDept.SelectedItem.ToString();
sqlText = "update students set sname='" + strName + "', ssex='" + strSex + "', sage='" + strAge + "',sdept='" + strDept + "'where sno='" + strNumber + "';";
bool tag = ExecuteSQL(sqlText);
if(!tag)
{
MessageBox.Show("修改出错!");
return;
}
LoadStudent();
ClearContent();
SetControlsEnable(false);
btnAdd.Enabled=true;
btnSave.Enabled=true;
btnser.Enabled=true;
btnsered.Enabled=true;
btndel.Enabled=true;
btndeled.Enabled=true;
btnup.Visible=true;
}
}
}
学生管理系统 做的不全。。 BUG 存在很多。。。
展开全部
有时候会碰到这样的问题,要 DataTabel .Select(" ") 返回的行集 DataRow[ ] 重新组合到一个相同结构的表 DataTable 中
这样的问题有时候是必须的,当然解决的方法有很多,最近我从网上搜索到一个比较不错的方法供大家分享:
public DataTable GetTable(DataRow[] drow1)
{
DataTable tempT=new DataTable();
if(drow1.Length ==0) return tempT;
tempT= drow1[0].Table.Clone();//克隆表结构
DataSet tempDs = new DataSet();
tempDs.Tables.Add(tempT);//添加到辅助数据集中
tempDs.Merge(drow1);//利用数据集合并行集到表
return tempDs.Tables[0];//返回此表
}
还是不错的,呵呵……
转载http://blog.myspace.cn/e/401592736.htm
这样的问题有时候是必须的,当然解决的方法有很多,最近我从网上搜索到一个比较不错的方法供大家分享:
public DataTable GetTable(DataRow[] drow1)
{
DataTable tempT=new DataTable();
if(drow1.Length ==0) return tempT;
tempT= drow1[0].Table.Clone();//克隆表结构
DataSet tempDs = new DataSet();
tempDs.Tables.Add(tempT);//添加到辅助数据集中
tempDs.Merge(drow1);//利用数据集合并行集到表
return tempDs.Tables[0];//返回此表
}
还是不错的,呵呵……
转载http://blog.myspace.cn/e/401592736.htm
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果datarow数组的结构和datatable中的结构完全一致的话,可以考虑以下方法
DataRow[] drs;//你的datarow[]
DataTable dt; //你的datatable
//上述变量已经初始化
foreach (DataRow dr in drs)
{
dt.ImportRow(dr);
}
如果两者结构不一致的话,就要考虑创建新行然后复制了
DataRow[] drs;//你的datarow[]
DataTable dt; //你的datatable
//上述变量已经初始化
foreach (DataRow dr in drs)
{
dt.ImportRow(dr);
}
如果两者结构不一致的话,就要考虑创建新行然后复制了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
foreach(Datarow dr in Datarow)
{
datatable 表名.Rows.add(dr);
}
{
datatable 表名.Rows.add(dr);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询