- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在学习 Web 开发人员,并且正在学习 ASP.NET WEBFORMS。我对 DataAdapter 和更新/删除表有疑问。
我想知道哪种方法是正确的。假设我有这种方法。
更新方法
public void UpdateDataTable()
{
SqlConnection conn = new SqlConnection(strcon);
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
string sqlsel = "SELECT ActId, Title FROM Act WHERE ArtistId = " + Session["UserId"];
try
{
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "MyTable");
dt = ds.Tables["MyTable"];
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
catch (Exception ex)
{
LabelMessage.Text = ex.Message;
}
finally
{
conn.Close();
}
}
我在页面加载到 (!Page.IsPostBack)
时调用此方法。所以我的问题是,因为 DataSet 将其全部保存在内存中(DataTable 也是如此)。我想再次使用 DataAdapter 对象更新一行,哪种方法最好用?在点击事件中。
方法一
protected void ButtonUpdate_Click1(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(strcon);
SqlDataAdapter da = null;
string sqlupd = "UPDATE [Act] SET [Title] = @Title, [Description] = @Description, [Duration] = @Duration WHERE [ActId] = @ActId";
try
{
conn.Open();
da = new SqlDataAdapter();
da.UpdateCommand = new SqlCommand(sqlupd, conn);
da.UpdateCommand.Parameters.AddWithValue("@Title", TextBoxTitle.Text);
da.UpdateCommand.Parameters.AddWithValue("@Description", TextBoxText.Text);
da.UpdateCommand.Parameters.AddWithValue("@Duration", TextBoxDuration.Text);
da.UpdateCommand.Parameters.AddWithValue("@ActId", LabelID.Text);
da.UpdateCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
LabelMessage.Text = "" + ex.Message;
}
finally
{
conn.Close();
}
// Call the Update Method
UpdateDataTable();
}
还是重新填充所有,然后放入 DataSet -> DataTable 中更好?像这样。
方法二
protected void ButtonUpdate_Click1(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(strcon);
SqlDataAdapter da = null;
DataSet ds = null;
DataTable dt = null;
SqlCommand cmd = null;
string sqlsel = "SELECT * FROM Act WHERE ArtistId = " + Session["UserId"];
string sqlupd = "UPDATE [Act] SET [Title] = @Title, [Description] = @Description, [Duration] = @Duration WHERE [ActId] = @ActId";
try
{
da = new SqlDataAdapter();
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sqlsel, conn);
ds = new DataSet();
da.Fill(ds, "MyTable");
dt = ds.Tables["MyTable"];
dt.Rows[Gridview1.SelectedIndex]["Title"] = TextBoxTitle.Text;
dt.Rows[Gridview1.SelectedIndex]["Description"] = TextBoxText.Text;
dt.Rows[Gridview1.SelectedIndex]["Duration"] = TextBoxDuration.Text;
// UPDATE
cmd = new SqlCommand(sqlupd, conn);
cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 50, "Title");
cmd.Parameters.Add("@Description", SqlDbType.Text, 250, "Description");
cmd.Parameters.Add("@Duration", SqlDbType.Int, 4, "Duration");
SqlParameter parm = cmd.Parameters.Add("@ActId", SqlDbType.Int, 4, "ActId");
parm.SourceVersion = DataRowVersion.Original;
da.UpdateCommand = cmd;
da.Update(ds, "MyAct");
}
catch (Exception ex)
{
LabelMessage.Text = "" + ex.Message;
}
finally
{
conn.Close();
}
UpdateDataTable();
;
}
那么哪种方法最好用呢?为什么? :)
最佳答案
两者都不是一个好的选择,将任何 DAL 代码放在您的代码隐藏/ Controller / View 中是一个很大的禁忌,并且是一种非常短视的编码实践。您应该有一些基本的模型、业务逻辑和 DAL 命名空间类可以使用。
关于c# - DataAdapter,更新哪个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21215570/
我有这样的代码: var ds = new DataSet(); var fooIDToFoo = new Dictionary {{"Foo1", foo1}, {"Food2", foo2}};
我编写了一组异步方法来快速处理大量数据库拉取和编译。在大多数情况下,这些工作非常棒,并且确实为我的软件创造了奇迹。然而,最近我在这些方法上遇到了一个小问题:用户时不时地搞砸了,软件拉取数据的时间范围变
我从HERE复制了一些代码,但出现错误:“')'附近的语法不正确。 但是我看不到这段代码有什么问题 Dim conString As String = "Data Source=.\SQLEXPRES
我们使用 VB.net 对数据读取器(7 条记录)执行 mySQL 语句,然后通过下面的代码加载到数据表(新创建的 6 条记录?) - 当涉及到数据表时,第一条记录似乎潜逃了 - 我无法理解为什么 -
众所周知,DataAdapter 会打开和关闭尚未打开的连接。但是用我的代码它打开但没有关闭..我正在使用 MySql.Data.MySqlClient.MySqlDataAdapter,不确定我做错
我目前正在学习 Web 开发人员,并且正在学习 ASP.NET WEBFORMS。我对 DataAdapter 和更新/删除表有疑问。 我想知道哪种方法是正确的。假设我有这种方法。 更新方法 publ
我尝试在 C#.net 中使用 DataAdapter。但我仍然不了解 DataAdapter。 我阅读了很多关于 DataAdapter 和 DataReader 的文章和博客。 我知道 DataA
我想使用 DataAdapter 填充 DataGridView 控件。但是我不知道该怎么做,因为我使用的是带参数的存储过程。有人可以举个例子吗? 最佳答案 我明白了!...嘿嘿 protected
数据未显示。我尝试在调试器中调试cide。我收到错误 $.jqx.dataAdapter is not a constructor 数据已成功填充到 aDataSet 中。请告诉我什么可能是错误的。
我已经初始化了一个数据适配器: string sql = "SELECT * From localitati"; da1 = new System.Data.SqlClient.SqlDataAdap
我有一个断开连接的 dataTable,其中包含一些记录。 我正在使用以下函数获取 dataTable。 static System.Data.DataTable ReadSetUpTable(str
我有一个相对简单的例程,它查看媒体文件的数据库条目,计算宽度、高度和文件大小,然后将它们写回数据库。 数据库是 SQLite,使用 System.Data.SQLite 库,处理大约 4000 行。我
我正在尝试按照标题所说的进行操作,但在运行时出现此错误: Incorrect syntax near the keyword 'Top'. string connString = @"server
我知道可能有一个明显的原因,但我找不到.. 我通常在连接数据库和读取数据时使用using语句,但我不能在DataAdapter上使用它,因为它没有实现IDisposable. 另外:填充数据集后它的行
我知道 DataAdapter 存在性能问题,但是否有任何可能更快的解决方法?目前,DataAdapter.Fill 方法在处理 3000 条记录时需要 5-6 秒,这对我的应用来说太慢了。如果我删除
我试图从 MySQL 和 Oracle 数据库加载大的十进制值,这会导致异常。据我所知,当它尝试将值转换为 .Net 的十进制数据类型时,会导致异常,但是从数据库读取如此巨大的值的方法是什么? MyS
我在 vb.net 中使用 MySQLDatagridview正确显示,一切正常,当我尝试用更新(方法)更新数据库中的更改时出现问题默认情况下,数据在内存中更新“被删除的学生 ID = 2”但是数据库
我正在尝试使用 cwac 提供的演示......但是当我将它拉入我的 fragment 时我收到错误“方法 getActivity() 未定义类型 DataAdapter” 我确信它是上下文相关的,所
我正在处理一些遗留代码,试图了解它是如何工作的。 该代码有一个 MySqlCommand,它调用一个存储过程来填充一个 DataTable,如下所示: _apptCmd = new MySql
以下代码未通过 dataadapter.update() 将数据集的更改保存到数据库。我将 winform 上的数据显示到文本框。 我有一个保存按钮,用于保存对数据库所做的更改。更改仅保存到数据集的内
我是一名优秀的程序员,十分优秀!