winforms SqlHelper数据适配器

lmvvr0a8  于 2022-11-25  发布在  其他
关注(0)|答案(2)|浏览(147)

我使用的SqlHelper类具有CRUD操作的通用方法。

public static void Fill(DataSet dataSet, String procedureName)
    {
        SqlConnection oConnection = new SqlConnection(DBInterface.ConnectionString);
        SqlCommand oCommand = new SqlCommand(procedureName, oConnection);
        oCommand.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter oAdapter = new SqlDataAdapter();

        oAdapter.SelectCommand = oCommand;
        oConnection.Open();
        using (SqlTransaction oTransaction = oConnection.BeginTransaction())
        {
            try
            {
                oAdapter.SelectCommand.Transaction = oTransaction;
                oAdapter.Fill(dataSet);
                oTransaction.Commit();
            }
            catch
            {
                oTransaction.Rollback();
                throw;
            }
            finally
            {
                if (oConnection.State == ConnectionState.Open)
                    oConnection.Close();
                oConnection.Dispose();
                oAdapter.Dispose();
            }
        }
    }

现在在我的代码中,我调用这个方法,

private void BindCustomers()
        {
            DataSet dsCust = new DataSet();           
            SqlHelper.Fill(dsCust, "getCustomers");
            --then I bind this dataset to datagridview
        }

这一切都很好。现在我想更新数据库中的数据。但是我不知道如何调用DataAdatpaer.Update(dataset)来将datagridview中所做的更改更新到数据库中。这在这里可行吗?或者我需要按照惯例来查找更新的行并调用SqlHelper中的ExecuteNonQuery函数?使用datadapter.update(ds)可以做些什么吗?谢谢

krugob8w

krugob8w1#

您不需要隐藏数据适配器,或者如果出于某种原因隐藏了数据适配器,则需要在类中公开一个方法以将更新推送到服务器。

示例

Public class SqlHelper
{
    string commandText;
    string connectionString;
    public SqlHelper(string command, string connection)
    {
        commandText = command;
        connectionString = connection;
    }
    public DataTable Select()
    {
        var table = new DataTable();
        using (var adapter = new SqlDataAdapter(this.commandText, this.connectionString))
            adapter.Fill(table)
        return table;
    }
    public void Update(DataTable table)
    {
        using (var adapter = new SqlDataAdapter(this.commandText, this.connectionString))
        {
            var builder = new SqlCommandBuilder(adapter);
            adapter.Update(table);
        }
    }
}
7cwmlq89

7cwmlq892#

通过在类中实现此方法,您可以执行所有原始操作select、update、delete和insert。您只需要传递连接字符串、过程参数和过程名。使用此方法,个人将检索DataTable中的数据。如果任何人想要数据集,则只需将DataSet替换到DataTable的位置

SqlConnection conn = new SqlConnection("Your ConnectionString");
   
public DataTable ExecuteDataTable(string ProcedureName, SqlParameter[] _Param)
{
    try
    {               
        DataTable dataTable = new DataTable();
        SqlCommand cmd = new SqlCommand(ProcedureName, conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Clear();
        if (_Param is not null)
        {
            for (int i = 0; i < _Param.Length; i++)
            {
                if (_Param[i].ParameterName is not null)
                {
                    if (_Param[i].Value is not null)
                    {
                        cmd.Parameters.AddWithValue(_Param[i].ParameterName, _Param[i].Value);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue(_Param[i].ParameterName, DBNull.Value);
                    }
                }
            }
        }                
        conn.Open();
        SqlDataAdapter DA = new SqlDataAdapter(cmd);
        DA.Fill(dataTable);
        conn.Close();
        return dataTable;
    }
    catch (Exception ex)
    {
        conn.Close();
        throw;

    }
}

相关问题