c# - DataAdapter,更新哪个方法?

标签 c# asp.net ado.net dataset dataadapter

我目前正在学习 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/

相关文章:

c# - 使用 C# 中的子查询 Access 数据库 INSERT

c# - 从 C# .Net Core 项目正确调用外部 C 库方法*在 Linux 上*

c# - .net开发代码结构-Controllers, Services, Repositories & Contexts

asp.net - 密码保护 IIS 7.5 中的 ASP.NET Web 应用程序

c# - 在 ListView 控件中隐藏 ID 列

c# - 通过动态图像处理程序渲染图像的布局修复

c# - 如何阻止 setter 通过 Web 服务公开?

asp.net - 在 RadAjax 回调后执行客户端 Javascript

c# - 在 C# 中运行存储过程,传递参数并捕获输出结果

.net - 在哪里可以找到 .Net ORM 与数字或图表的比较?