我看了一些EF教程,但是我不知道几件事情。我目前正在使用ADO与SQL服务器数据库交互的.net测试项目中工作。现在我必须将ADO部分传递给实体框架。 我目前正在使用3层,业务,对象和DataAcess。从ADO到实体框架的ASP/C#
在DA中,我有以下查询来从我的网站更新Employee表。它的工作原理,但我想通过使用Linq将它传递给实体框架。我还有其他一些插入和选择的例子,但是我确信一旦我能够对它进行排序,我就能找到它们。
注意:我已经使用实体框架将我的DataAccess类与数据库连接起来,实体列表对象称为ListEntities。
SqlConnection conn = new SqlConnection();
try
{
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("UPDATE Employee SET [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected]", conn);
comm.Parameters.Add("@Name",
System.Data.SqlDbType.VarChar, 70);
comm.Parameters["@Name"].Value = User.Name;
comm.Parameters.Add("@Lname",
System.Data.SqlDbType.VarChar, 70);
comm.Parameters["@Lname"].Value = User.Lname;
comm.Parameters.Add("@Age",
System.Data.SqlDbType.Int);
comm.Parameters["@Age"].Value = User.Age;
comm.Parameters.Add("@Email",
System.Data.SqlDbType.NVarChar, 70);
comm.Parameters["@Email"].Value = User.Email;
comm.Parameters.Add("@AYear",
System.Data.SqlDbType.Int);
comm.Parameters["@Year"].Value = User.Year;
comm.Parameters.Add("@ColorID",
System.Data.SqlDbType.Int);
comm.Parameters["@ColorID"].Value = User.ColorID;
comm.Parameters.Add("@AvatarID",
System.Data.SqlDbType.Int);
comm.Parameters["@AvatarID"].Value = User.AvatarID;
comm.Parameters.Add("@EmployeeID",
System.Data.SqlDbType.Int);
comm.Parameters["@EmployeeID"].Value = User.UserID;
conn.Open();
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
如果你只是要重新抛出它,不要捕捉异常。这样做没有意义。并且绝对不要在发现异常时抛出ex;否则将失去堆栈跟踪。只要“扔”; – mason
FYI你可以单行这些:'comm.Parameters.Add(“@ Name”,System.Data.SqlDbType.VarChar,70).Value = User.Name;'保存集合查找。 –
不是我问过的,但很高兴知道。 –