请建议您在SqlCommand
和SqlDataAdapter
之间选择SQL Server INSERT
,UPDATE
和DELETE
操作。
让我们考虑SQL Server的以下代码片段:
public int Execute(SqlCommand commandSql)
或
public int Insert(SqlCommand commandSql)
考虑到SqlCommand
(Execute
方法)的明显优势,它可用于所有INSERT,UPDATE和DELETE操作以及SqlDataAdapter
的优势(插入,更新,删除方法)是断开连接的体系结构,使用SqlCommand
ExecuteNonQuery
对SqlDataAdapter
ExecuteNonQuery
使用的优缺点是什么?
企业级Web应用程序的注意事项?请让专家澄清一下。
请注意,这个问题不是关于LINQ或实体框架,这与SqlCommand
ExecuteNonQuery
相对于SqlDataAdapter ExecuteNonQuery
是什么?
重要问题:SqlCommand
ExecuteNonQuery
是否使用已连接或已断开连接的架构?
//Insert
private bool Insert(string firstName, string lastName, string synonym)
{
bool isInserted = false;
try
{
int rowsAffected = 0;
StringBuilder insertQuery = new StringBuilder();
insertQuery.Append("INSERT INTO Customer ");
insertQuery.Append("(");
insertQuery.Append("FirstName, ");
insertQuery.Append("LastName, ");
insertQuery.Append("Synonym ");
insertQuery.Append(") ");
insertQuery.Append("VALUES ");
insertQuery.Append("(");
insertQuery.Append("@FirstName, ");
insertQuery.Append("@LastName, ");
insertQuery.Append("@Synonym ");
insertQuery.Append(");");
using (SqlCommand commandSql = new SqlCommand(insertQuery.ToString()))
{
commandSql.Parameters.AddWithValue("@FirstName", firstName);
commandSql.Parameters.AddWithValue("@LastName", lastName);
commandSql.Parameters.AddWithValue("@Synonym", synonym);
DataAccessLayer dal = new DataAccessLayer();
rowsAffected = dal.Execute(commandSql); // SqlCommand ????
rowsAffected = dal.Insert(commandSql); // SqlDataAdapter ???
}
if (rowsAffected > 0)
{
isInserted = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return isInserted;
}
//UPDATE
private bool Update(int custId, string firstName, string lastName, string synonym)
{
bool isUpdated = false;
try
{
int rowsAffected = 0;
StringBuilder updateQuery = new StringBuilder();
updateQuery.Append("UPDATE Customer ");
updateQuery.Append("SET ");
updateQuery.Append("FirstName = @FirstName, ");
updateQuery.Append("LastName = @LastName, ");
updateQuery.Append("Synonym = @Synonym ");
updateQuery.Append("WHERE ");
updateQuery.Append("CustomerId = @CustomerId;");
using (SqlCommand commandSql = new SqlCommand(updateQuery.ToString()))
{
commandSql.Parameters.AddWithValue("@CustomerId", custId);
commandSql.Parameters.AddWithValue("@FirstName", firstName);
commandSql.Parameters.AddWithValue("@LastName", lastName);
commandSql.Parameters.AddWithValue("@Synonym", synonym);
DataAccessLayer dal = new DataAccessLayer();
rowsAffected = dal.Execute(commandSql); // SqlCommand ????
rowsAffected = dal.Update(commandSql); // SqlDataAdapter ???
}
if (rowsAffected > 0)
{
isUpdated = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return isUpdated;
}
//DELETE
private bool Delete(int custId)
{
bool isDeleted = false;
try
{
int rowsAffected = 0;
StringBuilder deleteQuery = new StringBuilder();
deleteQuery.Append("DELETE FROM Customer ");
deleteQuery.Append("WHERE ");
deleteQuery.Append("CustomerId = @CustomerId;");
using (SqlCommand commandSql = new SqlCommand(deleteQuery.ToString()))
{
commandSql.Parameters.AddWithValue("@CustomerId", custId);
DataAccessLayer dal = new DataAccessLayer();
rowsAffected = dal.Delete(commandSql);
rowsAffected = dal.Execute(commandSql); // SqlCommand ????
rowsAffected = dal.Delete(commandSql); // SqlDataAdapter ???
}
if (rowsAffected > 0)
{
isDeleted = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return isDeleted;
}
// SqlCommand
public int Execute(SqlCommand commandSql)
{
int rowsAffected = 0;
using (SqlConnection connectionSql = new SqlConnection("ConnectionString"))
{
commandSql.Connection = connectionSql;
commandSql.Connection.Open();
rowsAffected = commandSql.ExecuteNonQuery();
}
return rowsAffected;
}
// SqlDataAdapter
public int Insert(SqlCommand commandSql)
{
int rowsAffected = 0;
using (SqlConnection connectionSql = new SqlConnection("ConnectionString"))
{
using (SqlDataAdapter dataAdapterSql = new SqlDataAdapter())
{
dataAdapterSql.InsertCommand = commandSql;
dataAdapterSql.InsertCommand.Connection = connectionSql;
dataAdapterSql.InsertCommand.Connection.Open();
rowsAffected = dataAdapterSql.InsertCommand.ExecuteNonQuery();
}
}
return rowsAffected;
}
// SqlDataAdapter
public int Update(SqlCommand commandSql)
{
int rowsAffected = 0;
using (SqlConnection connectionSql = new SqlConnection("ConnectionString"))
{
using (SqlDataAdapter dataAdapterSql = new SqlDataAdapter())
{
dataAdapterSql.UpdateCommand = commandSql;
dataAdapterSql.UpdateCommand.Connection = connectionSql;
dataAdapterSql.UpdateCommand.Connection.Open();
rowsAffected = dataAdapterSql.UpdateCommand.ExecuteNonQuery();
}
}
return rowsAffected;
}
// SqlDataAdapter
public int Delete(SqlCommand commandSql)
{
int rowsAffected = 0;
using (SqlConnection connectionSql = new SqlConnection("ConnectionString"))
{
using (SqlDataAdapter dataAdapterSql = new SqlDataAdapter())
{
dataAdapterSql.DeleteCommand = commandSql;
dataAdapterSql.DeleteCommand.Connection = connectionSql;
dataAdapterSql.DeleteCommand.Connection.Open();
rowsAffected = dataAdapterSql.DeleteCommand.ExecuteNonQuery();
}
}
return rowsAffected;
}
请帮忙。
由于
答案 0 :(得分:0)
SQLDataAdapter是与Dataset类一起使用的专用案例。如果您在示例中使用纯SQL输入,则最好在没有使用特定数据集的情况下将SQLCommand用于此目的。如果您打算使用类似于企业数据集的东西,那么考虑更新的实体框架是值得的,因为它提供了打字与普通数据集的对比。