обновить данные вставки из ограниченного представления данных из таблицы данных - PullRequest
0 голосов
/ 21 сентября 2018

Я ограничен datagridview из datatable из базы данных sql сервера

Мне нужно знать, как я могу сделать кнопку обновления, которая при нажатии обновляет отредактированные данные в пределах этой datagridview

если есть новые, напрямую добавьте строки в datagridview вставьте их в базу данных, если есть обновление для ранее ограниченных данных, обновите его

Я искал, но я не совсем понимаю, как это сделать

  1. почему я должен использовать DataAdpterReader?

  2. почему я должен использовать Dataset, который представляет собой набор DataTable, и я ужеесть один DataTable?

Спасибо

Ответы [ 3 ]

0 голосов
/ 21 сентября 2018

Если вы хотите, чтобы строки в вашем DataGirdView были отражены в базе данных, вы просто должны использовать методы объекта DataAdapter.

Проще говоря, объекты DataAdapter и DataReader предоставляют вам простой и эффективный способ чтения и записи в базу данных.Кроме того, они также делают все это, не влияя на фактическую таблицу базы данных, так что это означает, что все остается нетронутым, пока вы не скажете.

Для этого примера представьте, что у нас есть таблица SQL с именем contacts с тремя столбцами, а именно: fname , mname и lname .

Чтобы начать работу, нам понадобится функция, которую мы могли бы использовать дляполучить данные из таблицы "contacts".

    protected DataSet GetData()
    {
        // our select query to obtain all the rows from the contacts table
        string selectQuery = "SELECT * FROM contacts";

        // Where the data from the underlying table will be stored
        DataSet ds = new DataSet();

        // Connect to the database and get the data from the "contacts" table
        using (SqlConnection conn = new SqlConnection(connString))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(selectQuery, conn))
            {
                da.Fill(ds, "contacts"); // Add the rows from the "contacts" table to our dataset
            }
        }

        return ds;
    }

Затем вы можете привязать DGV к таблице, хранящейся в возвращенном объекте набора данных, выполнив

DGV_Items.DataSource = GetData();

В своем событии form_load.

Теперь мы закончили настройку нашего метода для получения данных из базы данных, теперь мы настроили метод для манипулирования всеми этими данными, которые у нас теперь есть.

    protected void UpdateTable(DataSet ds)
    {
        SqlConnection conn = new SqlConnection(connString);

        // Insert, update and delete queries
        string updateQuery = "UPDATE contacts SET fname=@first,mname=@middle,lname=@last WHERE ID=@id";
        string deleteQuery = "DELETE FROM contacts WHERE ID=@id";
        string insertQuery = "INSERT INTO contacts VALUES(@first,@middle,@last)";

        // Create the parameters for the queries above
        SqlParameter[] insertParams = new SqlParameter[]
        {
            // the first parameter (e.g. @first) has to match with the declaration in the query

            // the second parameter (e.g.SqlDbType.NVarChar) is the data type of the actual column in the source table

            // the third paramter (e.g. 100) is the length of the data in the database table's column

            // the last parameter (e.g. "fname") is the DataPropertyName of the source column which is
            // basically the name of the database table column that the DGV column represents

            new SqlParameter("@first", SqlDbType.NVarChar, 100, "fname"),
            new SqlParameter("@middle", SqlDbType.NVarChar, 100, "mname"),
            new SqlParameter("@last", SqlDbType.NVarChar, 100, "lname")
        };

        SqlParameter[] updateParams = new SqlParameter[]
        {
            new SqlParameter("@first", SqlDbType.NVarChar, 100, "fname"),
            new SqlParameter("@middle", SqlDbType.NVarChar, 100, "mname"),
            new SqlParameter("@last", SqlDbType.NVarChar, 100, "lname"),
            new SqlParameter("@id", SqlDbType.Int, 100, "id")
        };

        SqlParameter[] DeleteParams = new SqlParameter[]
        {
            new SqlParameter("@id", SqlDbType.Int, 100, "id")
        };

        // Create the SqlCommand objects that will be used by the DataAdapter to modify the source table
        SqlCommand insertComm = new SqlCommand(insertQuery, conn);
        SqlCommand updateComm = new SqlCommand(updateQuery, conn);
        SqlCommand deleteComm = new SqlCommand(deleteQuery, conn);

        // Associate the parameters with the proper SqlCommand object
        insertComm.Parameters.AddRange(insertParams);
        updateComm.Parameters.AddRange(updateParams);
        deleteComm.Parameters.AddRange(DeleteParams);

        // Give the DataAdapter the commands it needs to be able to properly update your database table
        SqlDataAdapter dataAdapter = new SqlDataAdapter()
        {
            InsertCommand = insertComm,
            UpdateCommand = updateComm,
            DeleteCommand = deleteComm
        };

        // A DataTable and a DataSet are basically the same. Except the DataSet is a collection of DataTables
        // Here, you can see that we've accessed a specific DataTable in the DataSet.

        // Calling the Update method executes the proper command based on the modifications to the specified
        // DataTable object then commits these changes to the database
        dataAdapter.Update(ds.Tables["contacts"]);
    }

Вышеупомянутый метод будетобрабатывать все манипуляции с данными.Он будет действовать на основе изменений, внесенных в объект DataTable, связанный с вашим DGV.Наконец, вы можете вызвать все методы, которые мы сделали в обработчике событий кнопки update .

    private void Btn_Update_Click(object sender, EventArgs e)
    {
        // Grab the DGV's data source which contains the information shown in the DGV
        DataSet ds = (DataSet)dgv_items.DataSource;
        // Have any updates to the said dataset committed to the database
        UpdateTable(ds);
        // rebind the DGV
        dgv_items.DataSource = GetData();
    }

EDIT

Наряду с предложениями Crowcoder, вотлучший способ написать все, что я написал выше:

/// <summary>
/// A collection of methods for easy manipulation of the data in a given SQL table
/// </summary>
class DBOps
{
    // The connection string contains parameters that dictate how we connect to the database
    private string connString = ConfigurationManager.ConnectionStrings["contactsConnectionString"].ConnectionString;

    // The table the instance of the class will be interacting with
    private string srcTable;

    // The SqlConnection Object that we will be using to connect to the database
    SqlConnection conn;

    // The DataAdapter object that we will be using to interact with our database
    SqlDataAdapter da;

    // The DataSet that we will be storing the data retrieved from the database
    DataSet ds;

    // The queries we would be using to manipulate and interact with the data in the database
    private string selectQuery;
    private string updateQuery;
    private string deleteQuery;
    private string insertQuery;

    // The collection of parameters for the queries above
    private SqlParameter[] insertParams;
    private SqlParameter[] updateParams;
    private SqlParameter[] DeleteParams;

    // The command objects that will be used by our data adapter when
    // interacting with the database
    private SqlCommand insertComm;
    private SqlCommand updateComm;
    private SqlCommand deleteComm;

    /// <summary>
    /// Initialize a new instance of the DBOps class
    /// </summary>
    /// <param name="tableName">The name of the table that the object will be interacting with</param>
    public DBOps(string tableName)
    {
        // Initialize the SqlConnection object
        conn = new SqlConnection(connString);

        // Initialize our collection of DataTables
        ds = new DataSet();

        srcTable = tableName;

        // initialize the query strings
        selectQuery = string.Format("SELECT * FROM {0}", srcTable);
        insertQuery = string.Format("INSERT INTO {0}(fname, mname, lnmae) VALUES(@first, @middle, @last", srcTable);
        updateQuery = string.Format("UPDATE {0} SET fname=@first, mname=@middle, lname=@last WHERE ID=@id", srcTable);
        deleteQuery = string.Format("DELETE FROM {0} WHERE ID=@id", srcTable);

        // Initialize the collection of parameters for each query above
        insertParams = new SqlParameter[]
        {
            // new SqlParameter(@paramName, paramDataType, paramValueLength, DGVDataPropertyName);
            new SqlParameter("@first", SqlDbType.NVarChar, 100, "fname"),
            new SqlParameter("@middle", SqlDbType.NVarChar, 100, "mname"),
            new SqlParameter("@last", SqlDbType.NVarChar, 100, "lname")
        };

        updateParams = new SqlParameter[]
        {
            new SqlParameter("@first", SqlDbType.NVarChar, 100, "fname"),
            new SqlParameter("@middle", SqlDbType.NVarChar, 100, "mname"),
            new SqlParameter("@last", SqlDbType.NVarChar, 100, "lname"),
            new SqlParameter("@id", SqlDbType.Int, 100, "id")

        };

        DeleteParams = new SqlParameter[]
        {
            new SqlParameter("@id", SqlDbType.Int, 100, "id")
        };

        // Initialize the SqlCommand objects that will be used by the DataAdapter to modify the source table
        insertComm = new SqlCommand(insertQuery, conn);
        updateComm = new SqlCommand(updateQuery, conn);
        deleteComm = new SqlCommand(deleteQuery, conn);

        // Associate the parameters with the proper SqlCommand object
        insertComm.Parameters.AddRange(insertParams);
        updateComm.Parameters.AddRange(updateParams);
        deleteComm.Parameters.AddRange(DeleteParams);

        // Give the DataAdapter the commands it needs to be able to properly update your database table
        da = new SqlDataAdapter()
        {
            InsertCommand = insertComm,
            UpdateCommand = updateComm,
            DeleteCommand = deleteComm
        };
    }

    /// <summary>
    /// Retrieve the data from the SQl table
    /// </summary>
    /// <returns></returns>
    public DataSet GetData()
    {
        DataSet ds = new DataSet();

        // Connect to the database and get the data from the "contacts" table
        using (conn)
        {
            conn.Open();
            using (SqlDataAdapter da = new SqlDataAdapter(selectQuery, conn))
            {
                da.Fill(ds); // Add the rows from the "contacts" table to our dataset
            }
        }

        return ds;
    }

    /// <summary>
    /// Commit the changes present in the object's DataSet to the Database
    /// </summary>
    public void UpdateData(DataSet ds)
    {
        // Calling the Update method executes the proper command based on the modifications to the specified
        // DataTable object
        da.Update(ds.Tables[srcTable]);
    }

Чтобы использовать этот класс, просто создайте его экземпляр, написав:

DBOps ops = new DBOps("contacts");

В вашем Обновить обработчик события нажатия кнопки , вы можете зафиксировать все изменения, сделанные в базовом источнике данных вашего DGV, вызвав метод UpdateData .

private void Btn_Update_Click(object sender, EventArgs e)
{
    // Grab the DGV's data source which contains the information shown in the DGV
    DataSet ds = (DataSet)dgv_items.DataSource;
    // Have any updates to the said dataset committed to the database
    ops.UpdateData(ds);
    // rebind the DGV
    dgv_items.DataSource = ops.GetData();
}

Чтобы суммировать все это:

  • Объекты DataAdapter и DataReader предоставляют вам методы, которые позволят вам взаимодействовать с базой данных безопасным, эффективным и простым способом.
  • DataTable и DataSet практически одинаковы.За исключением DataTable только одна таблица и DataSet это набор DataTables.С другой стороны, у каждого из них также есть определенные методы, которых нет у другого.
0 голосов
/ 22 сентября 2018

Я придумала решение, которое я создал, чтобы получить все данные и добавить их в DataTable в наборе данных, как вы можете видеть в примере ниже

public DATEaSet GetDATEa()
{
    string connStr = ConfigurationManager.ConnectionStrings["connstr"].ToString();
    string cmdStr = @"SELECT  SEQ,
                                       ID,
                                       DATE,
                                       Started,
                                       END,
                                       TYPE,
                                       ENTRANCE,
                                       OUTGOING
                               FROM LOGING
                        WHERE SICK_ID=@ID;";

    SqlConnection conn = new SqlConnection(connStr);
    using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
    {
        try
        {
            conn.Open();
            cmd.CommandText = cmdStr;
            cmd.CommandType = CommandType.Text;

            ds = new DATEaSet();
            cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int)).Value = Convert.ToInt32(TB_ID.Text);
            da = new SqlDATEaAdapter(cmd);

            da.Fill(ds, "DATEaTable1");
            MyDGV.Columns["MyDGV_RowNum"].DATEaPropertyName = ds.Tables["DATEaTable1"].Columns["SEQ"].ColumnName;
            MyDGV.Columns["MyDGV_SessionID"].DATEaPropertyName = ds.Tables["DATEaTable1"].Columns["ID"].ColumnName;
            MyDGV.Columns["MyDGV_DATEe"].DATEaPropertyName = ds.Tables["DATEaTable1"].Columns["DATE"].ColumnName;
            MyDGV.Columns["MyDGV_StartTime"].DATEaPropertyName = ds.Tables["DATEaTable1"].Columns["Started"].ColumnName;
            MyDGV.Columns["MyDGV_EndTime"].DATEaPropertyName = ds.Tables["DATEaTable1"].Columns["END"].ColumnName;
            MyDGV.Columns["MyDGV_Type"].DATEaPropertyName = ds.Tables["DATEaTable1"].Columns["TYPE"].ColumnName;
            MyDGV.Columns["MyDGV_CreatedBy"].DATEaPropertyName = ds.Tables["DATEaTable1"].Columns["ENTRANCE"].ColumnName;
            MyDGV.Columns["MyDGV_EntryDATEe"].DATEaPropertyName = ds.Tables["DATEaTable1"].Columns["OUTGOING"].ColumnName;

            return ds;
        }
        catch (Exception ex)
        {
            string ErrorMsg = ex.Message.Substring(0, Math.Min(ex.Message.Length, 1024));
            MessageBox.Show(ErrorMsg);
            return null;
        }
    }
}

Для события Form_Load, которое я установилИсточник данных из Datagridview к DS, возвращенный описанным выше методом

Обратите внимание , что в некоторых случаях вы должны установить DataMember явно

MyDGV.DataSource = GetPatientSessions();
MyDGV.DataMember = "DATEaTable1";

сейчас, когда пользовательотредактируйте или добавьте строку в datagridview, а затем нажмите кнопку сохранения. Приведенный ниже метод обновит, отредактирует данные и вставит введенную новую строку

private void BTN_Save_Click(object sender, EventArgs e)
{
    using (SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(da))
    {
        try
        {
            da.Update(ds, "DATEaTable1");
        }
        catch (Exception ex)
        {
            string ErrorMsg = ex.Message.Substring(0, Math.Min(ex.Message.Length, 1024));
            MessageBox.Show(ErrorMsg);
        }
    }
}
0 голосов
/ 21 сентября 2018

Если я правильно понял, вам просто нужно выполнить тот же запрос, который будет заполнять "обновить" сетку, т.е. OnClick - выполняет приведенный выше код.Я не очень понимаю вашу оговорку if?Если есть новое?Новые данные?Вы хотите добавить его в базу данных?Вы можете добавить эту функциональность к этому событию OnClick, чтобы записать в БД все, что есть, независимо от того, изменились вы или нет, это действительно зависит от того, сколько данных вы представляете, поэтому решение может отличаться.

...