简体   繁体   中英

How to update specific columns of access database using datagridview column name in C#

I have a datagridview that it's columnNames are equal to my columns in access database

Now I want this: whene user click on savebtn all of datagridview columns values automatically updated to my database,
I've tried this code but it get me syntax error :

{"Syntax error (missing operator) in query expression '@1Where FeedID=@feedID'."}

where is the problem?

public void UpdatetFeeds(DataGridView dgv)
{
    string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings
    ["FeedLibraryConnectionString"].ConnectionString;

    int FeedID = Convert.ToInt32(dgv.CurrentRow.Cells[0].Value);

    using (OleDbConnection Connection = new OleDbConnection(StrCon))
    {
        Connection.Open();

        using (OleDbCommand Cmd = new OleDbCommand())
        {
            Cmd.Connection = Connection;

            foreach (DataGridViewColumn column in dgv.Columns)
            {
                string columnName = dgv.Columns[column.Index].Name;
                object columnValue = dgv.CurrentRow.Cells[column.Index].Value;

                Cmd.CommandText = @"Update tFeeds Set " + columnName + " =@"
                    + columnValue + "Where FeedID=@feedID";

                Cmd.Parameters.Add("@feedID", OleDbType.Integer).Value = FeedID;


                Cmd.ExecuteNonQuery();
            }
        }
     }
 }

Seems at least you lost space

" Where FeedID=@feedID";

Also, what kind of data you have here: " =@" + columnValue + " . Seems you need braskets here: " =@'" + columnValue + "'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM