SQLServer复制⼀条数据
insert into Table_1 (name) select name from Table_1 where id='1';select @@identity;
insert into Table_2 SELECT * from Table_1 where id='1'
/// <summary>
/// typeName为类的字符串形式,例⼦ : BLL.air_user -> "BLL.air_user"
/// </summary>
/// <param name="typeName"></param>
/// <returns></returns>
public Type typen(string typeName)
{
Type type = null;
Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
int assemblyArrayLength = assemblyArray.Length;
for (int i = 0; i < assemblyArrayLength; ++i)
{
type = assemblyArray[i].GetType(typeName);
if (type != null)
{
return type;
}
}
for (int i = 0; (i < assemblyArrayLength); ++i)
{
Type[] typeArray = assemblyArray[i].GetTypes();
int typeArrayLength = typeArray.Length;
for (int j = 0; j < typeArrayLength; ++j)
{
if (typeArray[j].Name.Equals(typeName))
{
return typeArray[j];
}
}
}
return type;
}
using (var db = new CKQMSDB())
{
if (true)
{
string strTableName = "AcountUser";
// 查询数据
List<c1> list = db.Database.SqlQuery<c1>("select * from AcountUser").ToList();
// 修改数据
int result = db.Database.ExecuteSqlCommand($"update {strTableName} set name='123' where id='{list[0].id}'");
// ⽣成需要复制的列
string columnNames = "";
//Type t = typeof(c1);
Type t = GetTypeByFullName("DAL.EF.AcountUser");// 获取类型
t.GetProperties().ToList().ForEach(c=> {
if (c.Name == "id")
{
return;// 去⾃增长id
}
columnNames += c.Name + ",";
});
columnNames = columnNames.TrimEnd(',');
// 复制⾏返回新增数据的id
typeof array//insert into Table_2 (name) select name from Table_1 where id='1'
int autoId = db.Database.ExecuteSqlCommand($"insert into {strTableName} ({columnNames}) select {columnNames} from {strTableName} where id='1';select @@identity;"); string strSQL = "SELECT COUNT(*) FROM test";
int autoId2 = db.Database.SqlQuery<int>(strSQL).ToList()[0];
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论