DataTable拷贝DataRow
一开始,思路是这样的:
DataTable dt = SqlHelper.GetTable("select * from Person"); DataTable dt2 = dt.Clone(); DataRow dr = dt.Rows[]; dt2.Rows.Add(dr); Console.ReadKey();
遗憾的是,将会抛出一个异常:
修改后的实现方式:
DataTable dt = SqlHelper.GetTable("select * from Person"); DataTable dt2 = dt.Clone(); DataRow dr = dt.Rows[]; dt2.Rows.Add(dr.ItemArray); Console.ReadKey();
或者 :
DataTable dt = SqlHelper.GetTable("select * from Person"); DataTable dt2 = dt.Clone(); DataRow dr = dt.Rows[]; dt2.ImportRow(dr); Console.ReadKey();
最后附上SqlHelper:
static class SqlHelper { private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; public static DataTable GetTable(string sql, params SqlParameter[] paras) { using (SqlConnection conn = new SqlConnection(ConnectionString)) { SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddRange(paras); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); conn.Open(); adapter.Fill(ds); return ds.Tables[]; } } }View Code
相关推荐
zhongshish 2020-10-21
abfdada 2020-08-26
jinfeng0 2020-08-03
libowenhit 2020-07-23
luckykapok 2020-07-06
Jonderwu 2020-07-05
zk0 2020-06-21
Colourful 2020-06-14
arctan0 2020-06-11
houjinkai 2020-06-03
whyname 2020-05-28
LCFlxfldy 2020-05-14
plusz 2020-05-09
bruce 2020-05-09
钟鼎 2020-05-01
常言道 2020-04-25
joyjoy0 2020-03-30
hanyujianke 2020-03-06