C#建立了ado.net实体数据模型,也就是实体框架,请问怎么实现数据更新操作,求具体代码!
2个回答
展开全部
aspx.cs
PersonDao dao = new PersonDao();
Person person = dao.getPerson("姓名"); //取记录
person.Age = 20; //更新该员工的年龄
dao.updatePerson(person);// 将新的数据保存到数据库
//实体数据模型
public class Person
{
private string name;
private int age;
private string sex;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Sex
{
get { return sex; }
set { sex = value; }
}
}
//对数据库的操作
public class PersonDao
{
private static string ConnStr = "数据库连接字符串";
public Person getPerson(string name)//可以用按其它条件查询, 确保唯一
{
string sql = "select name, age ,sex from table where name ='" + name + "'";
Person person = new Person();
SqlConnection sqlConn = new SqlConnection(ConnStr);
SqlCommand cmd = null;
try
{
sqlConn.Open();
cmd = sqlConn.CreateCommand();
cmd.CommandText = sql;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
person.Name = dr["name"].ToString();
person.Age = Convert.ToInt32(dr["age"]);
person.Sex = dr["age"].ToString();
}
}
catch (SqlException e)
{
person = null;
//记录错误信息
}
finally
{
if (sqlConn.State != ConnectionState.Closed)
{
sqlConn.Dispose();
sqlConn.Close();
}
cmd.Dispose();
}
return person;
}
public int updatePerson(Person person)//可以用按其它条件查询, 确保唯一
{
string sql = "update person set age = "+person.Age+", sex='"+person.Sex+"' where name = '"+person.Name+"'";
int i = 0;
SqlConnection sqlConn = new SqlConnection(ConnStr);
SqlCommand cmd = null;
try
{
sqlConn.Open();
cmd = sqlConn.CreateCommand();
cmd.CommandText = sql;
i = cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
i = 0;
//记录错误信息
}
finally
{
if (sqlConn.State != ConnectionState.Closed)
{
sqlConn.Dispose();
sqlConn.Close();
}
cmd.Dispose();
}
return i;
}
}
PersonDao dao = new PersonDao();
Person person = dao.getPerson("姓名"); //取记录
person.Age = 20; //更新该员工的年龄
dao.updatePerson(person);// 将新的数据保存到数据库
//实体数据模型
public class Person
{
private string name;
private int age;
private string sex;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Sex
{
get { return sex; }
set { sex = value; }
}
}
//对数据库的操作
public class PersonDao
{
private static string ConnStr = "数据库连接字符串";
public Person getPerson(string name)//可以用按其它条件查询, 确保唯一
{
string sql = "select name, age ,sex from table where name ='" + name + "'";
Person person = new Person();
SqlConnection sqlConn = new SqlConnection(ConnStr);
SqlCommand cmd = null;
try
{
sqlConn.Open();
cmd = sqlConn.CreateCommand();
cmd.CommandText = sql;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
person.Name = dr["name"].ToString();
person.Age = Convert.ToInt32(dr["age"]);
person.Sex = dr["age"].ToString();
}
}
catch (SqlException e)
{
person = null;
//记录错误信息
}
finally
{
if (sqlConn.State != ConnectionState.Closed)
{
sqlConn.Dispose();
sqlConn.Close();
}
cmd.Dispose();
}
return person;
}
public int updatePerson(Person person)//可以用按其它条件查询, 确保唯一
{
string sql = "update person set age = "+person.Age+", sex='"+person.Sex+"' where name = '"+person.Name+"'";
int i = 0;
SqlConnection sqlConn = new SqlConnection(ConnStr);
SqlCommand cmd = null;
try
{
sqlConn.Open();
cmd = sqlConn.CreateCommand();
cmd.CommandText = sql;
i = cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
i = 0;
//记录错误信息
}
finally
{
if (sqlConn.State != ConnectionState.Closed)
{
sqlConn.Dispose();
sqlConn.Close();
}
cmd.Dispose();
}
return i;
}
}
展开全部
。。。。。我想说楼上的都是扯淡。已经建立实体数据库,还用的到sql语句吗?
using (MvcMailEntities MME = new MvcMailEntities()) //调用名字为MvcMail 的实体数据库
{
MsgInfo Mi = new MsgInfo(); //建立一个新实体中的对象
Mi = MME.MsgInfo.First(a => a.Mid == 1); //找到数据库中 Mid为1 的MsgInfo
Mi.Mflag = 2; //修改此记录的Mflag=2
MME.SaveChanges(); //此行是进行保存操作,是必需的。不执行此行,数据库不会更改。
}
using (MvcMailEntities MME = new MvcMailEntities()) //调用名字为MvcMail 的实体数据库
{
MsgInfo Mi = new MsgInfo(); //建立一个新实体中的对象
Mi = MME.MsgInfo.First(a => a.Mid == 1); //找到数据库中 Mid为1 的MsgInfo
Mi.Mflag = 2; //修改此记录的Mflag=2
MME.SaveChanges(); //此行是进行保存操作,是必需的。不执行此行,数据库不会更改。
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询