第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

OleDbCommandBuilder 更新 - 至少缺少一個參數(shù)值

OleDbCommandBuilder 更新 - 至少缺少一個參數(shù)值

C#
守著一只汪 2021-12-05 16:31:34
我做了什么: 我使用 OleDbAdapter 從數(shù)據(jù)庫中讀取數(shù)據(jù),填充一個新的 DataTable。這很順利。然后我想在該數(shù)據(jù)表中添加一列,這也很順利。我添加了一個 OleDbCommandBuilder,以使用多一列的 DataTable 更新數(shù)據(jù)庫。我用 OleDbCommandBuilder 的“自動方式”進行了嘗試,因為我認為我想要的很簡單。但到目前為止,這并沒有奏效。我期望的 是 OleDbCommandBuilder 正在為我編寫一個新的 SQL 命令,其中包含“UPDATE”或“INSERT”。我進一步希望,除了 SELECT 命令之外,我無法讀取 OleDbAdapter 中的所有命令,因為 OleDbAdapter 在使用它們之前從構(gòu)建器中獲取命令。我在互聯(lián)網(wǎng)上讀到過,如果我讓適配器.Update(...) 調(diào)用,則不需要adapter.Fill(...)。但是沒有 adapter.Fill(...) 我無法從數(shù)據(jù)庫中獲取內(nèi)容。終于有一個問題有了名字:現(xiàn)在,在搜索問題后,我收到以下消息: System.Data.OleDbException:對于至少一個參數(shù)沒有給出值。我的問題:1)我期望有什么問題嗎?2)哪個參數(shù)沒有值?解決這幫助我理解:https : //docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.parameters?redirectedfrom=MSDN&view= netframework-4.7.2#System_Data_SqlClient_SqlCommand_Parameters3) 適配器、構(gòu)建器...是否按正確順序放置?4) 我有沒有其他事情要做,比如調(diào)用一個函數(shù)來更新帶有適配器的 SQL 命令?5) 我怎樣才能改進我解決這個問題的方式?例如:有沒有什么事件可以幫助我更多地了解正在發(fā)生的事情?如何捕捉這樣的事件?提前謝謝了!
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經(jīng)驗 獲得超7個贊

您通過向數(shù)據(jù)表添加新列來修改數(shù)據(jù)表的結(jié)構(gòu),這不會反映在生成的更新/插入/刪除 sql 命令中。

看看這個例子:OleDbCommandBuilder Class

很簡單:

   adapter.Update(table);

只更新服務(wù)器中基表中的數(shù)據(jù)(如果有變化)

1)我期望有什么問題嗎?

不,它正在工作,但 MS 訪問中的基表結(jié)構(gòu)沒有變化

2)哪個參數(shù)沒有值?

你沒有在 SQL 命令中傳遞參數(shù)

3) 適配器、構(gòu)建器...是否按正確順序放置?

是的,但刪除修改數(shù)據(jù)表的部分。它沒有效果

4) 我有沒有其他事情要做,比如調(diào)用一個函數(shù)來更新帶有適配器的 SQL 命令?

用評論查看我的代碼。

5) 我怎樣才能改進我解決這個問題的方式?例如:有沒有什么事件可以幫助我更多地了解正在發(fā)生的事情?如何捕捉這樣的事件?

您不能通過添加新列來修改數(shù)據(jù)表的結(jié)構(gòu)

更新

我測試了你的代碼,用注釋修改了它:

     public  bool AddColumnOfString_ToDataTable(string tableName, string newColumnName, string defaultCellValue)

            {

                // Approach: Accessing database at minimum time.

                //    returns true if  column name could not be found and column could be added


                DataTable table = new DataTable();



                //string strSQL = "SELECT " + tableName; // not valid syntax

                string strSQL = "SELECT * from " + tableName;

                OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL, myConnectionString);

                adapter.Fill(table);

                OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);


                bool result = false;

                // remove this code, it has no effect on the underlying base table in MS Access databas

                //any change in the structure of datatable has no effect on the database


                /*

                if (false == table.HasColumn(newColumnName))

                {

                    DataColumn newColumn = new DataColumn(newColumnName, typeof(System.String));

                    newColumn.DefaultValue = defaultCellValue;

                    table.Columns.Add(newColumn);

                    result = true;

                }

                */


                //  code to modify data in DataTable here


                //Without the OleDbCommandBuilder this line would fail

                adapter.Update(table);


                //just to review the generated code                 

                Console.WriteLine(builder.GetUpdateCommand().CommandText);

                Console.WriteLine(builder.GetInsertCommand().CommandText);

                return result;

            }

更新2:


如果您有興趣向 MS Access 數(shù)據(jù)庫添加新列,可以運行以下代碼:


 public   bool AddColumn(OleDbConnection con, 

                string tableName,string colName,string colType, object defaultValue)

            {

                string query = $"ALTER TABLE {tableName}  ADD COLUMN {colName} {colType} DEFAULT {defaultValue} ";

                var cmd = new OleDbCommand(query, con);

                try

                {

                    con.Open();

                    cmd.ExecuteNonQuery();

                    Console.WriteLine("Sql Executed Successfully");

                    return true;

                }

                catch (OleDbException e)

                {

                    Console.WriteLine("Error Details: " + e);

                }

                finally

                {

                    Console.WriteLine("closing conn");

                    con.Close();

                }

                return false;

            }


      public   void AddColumnTest()

            {

                OleDbConnection con = new OleDbConnection(myConnectionString);

                string tableName="table1";

                string colName="country";

                string colType="text (30)";

                object defaultValue = "USA";

                AddColumn(con, tableName, colName, colType, defaultValue);

            }               

我用 MS Access 測試了代碼,它工作正常。


查看完整回答
反對 回復(fù) 2021-12-05
  • 1 回答
  • 0 關(guān)注
  • 254 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號