简体   繁体   English

如何在C#中使用datagridview列名更新Access数据库的特定列

[英]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 我有一个datagridview ,它的columnNames等于我在access database

Now I want this: whene user click on savebtn all of datagridview columns values automatically updated to my database, 现在我想要这样:当用户单击savebtn所有datagridview列的值都会自动更新到我的数据库中,
I've tried this code but it get me syntax error : 我已经尝试过此代码,但它使我syntax error

{"Syntax error (missing operator) in query expression '@1Where FeedID=@feedID'."} {“查询表达式'@ 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 + "' 另外,您在这里拥有什么样的数据: " =@" + columnValue + " 。似乎您需要在这里使用Braskets: " =@'" + columnValue + "'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中使用datagridview更新Access数据库 - How to update Access database using a datagridview in C# 如何使用C#将datagridview更新到数据库? - how to update datagridview to database using c#? c# - 如何为datagridview实现更新按钮并使用dataHandler类(数据访问类)将值保存到数据库 - how to implement update button for datagridview and save values to database using a dataHandler class(data access class) c# 使用 C# 使用 DataGridView 中的值更新数据库 - Update database with values in DataGridView using C# 如何使用C#将DataGridView数据插入Access数据库表中? - How to insert DataGridView data in to table of access database using c#? C#-尝试使用datagridview(也使用OleDbCommandBuilder)更新访问数据库时出现“更新语句中的语法错误” - C# - “Syntax error in update statement” when trying to update the access database using datagridview (also using OleDbCommandBuilder) 如何在C#中更改datagridview的列名? - How to change the column name of datagridview in c#? 如何在C#中为Datagridview使用列名 - How to Use Column Name for Datagridview in C# datagridview 列名 C# - datagridview column name C# 使用C#中的datagridview和navigator绑定删除和更新ms Access数据库 - Delete and Update ms access database using a datagridview and navigator binding in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM