簡體   English   中英

使用C#數據對象將記錄插入SQL Server數據庫

[英]Insert Records into SQL Server Database using C# Data Objects

我試圖將數據適配器正確綁定到我的SQL數據庫,以插入某人在ac#程序中提交的記錄。 我覺得自己已經走了80%,但現在我遇到了麻煩。

因此,如下面的代碼所示,我可以創建並綁定數據表。 我已經測試了刪除功能,它們工作正常。 我現在正在嘗試使用“保存”按鈕向數據庫中插入新行。 我現在遇到的問題是,應該讓用戶輸入“筆記”,然后單擊“保存”。 我會自動填充其余的列,但是我不知道如何獲取用戶輸入的注釋。

到目前為止,這是我的代碼:

string userVerify = User.CurrentUser.UserName.ToString();
int participantID = this.mParticipant.ParticipantID;
DateTime date = DateTime.Now;
string properRow = dtNotes[1, dtNotes.NewRowIndex - 1].Value.ToString();

sqlDataAdapter.InsertCommand = new SqlCommand("INSERT INTO xxMyDatabasexx (ParticipantID,Verifier,Notes,Date) VALUES  (@participantID,@notes, @userVerify,@date);");
sqlDataAdapter.InsertCommand.Parameters.AddWithValue("@participantID", participantID);
sqlDataAdapter.InsertCommand.Parameters.AddWithValue("@userVerify", userVerify);
sqlDataAdapter.InsertCommand.Parameters.AddWithValue("@date", date);
sqlDataAdapter.InsertCommand.Parameters.AddWithValue("@notes", properRow);

sqlDataAdapter.Fill(dataTable);

sqlDataAdapter.Update(dataTable);

我知道properRow變量的邏輯是錯誤的。 當然,如果沒有行,則程序將崩潰,而且,如果未輸入新的音符,它將僅再現最后輸入的音符,這當然也是錯誤的。 當我在sqlDataAdapter.Fill時查看dataTable時,可以在正確的列中看到該注釋,但是我不知道如何簡單地保存它。

任何幫助表示贊賞。 謝謝。

編輯:我不知道的是InsertCommand(自然)還需要ExecuteNonQuery命令。 我當時的假設是,既然Delete和Update都沒有,那么Insert也不會。 這似乎解決了這個問題。 謝謝大家的幫助。

您可以僅通過使用Command對象,而無需DataAdapter將記錄插入SQL數據庫,如下面的代碼片段所示(只需傳遞您的Insert SQL語句字符串):

void SqlExecuteCommand(string InsertSQL)
{
    try
    {
        using (SqlConnection _connSqlCe = new SqlConnection("Conn String to SQL DB"))
        {
            _connSql.Open();
            using (SqlCommand _commandSqlCe = new SqlCommand(CommandSQL, _connSql))
            {
                _commandSql.CommandType = CommandType.Text;
                _commandSql.ExecuteNonQuery();
            }
        }
    }
    catch { throw; }
}

SQL INSERT查詢字符串的一般格式如下所示:

INSERT INTO YourTable (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

您可以通過向SQL String / Command添加參數來進一步擴展此解決方案,以防止SQL注入的可能性(請參見以下示例):

INSERT INTO YourTable (column1,column2,column3,...)
VALUES (@param1,@param2,@param3,...);

_commandSql.Parameters.Add("@param1","abc");
_commandSql.Parameters.Add("@param2","def");
_commandSql.Parameters.Add("@param3","ghijklm");

您還可以對SQL Parameters使用替代語法,例如:

_commandSql.Parameters.Add("@param1", SqlDbType.NChar).Value = "abc";
_commandSql.Parameters.Add("@param2", SqlDbType.NChar).Value = "def";
_commandSql.Parameters.Add("@param3", SqlDbType.NVarChar).Value = "ghijklm";

與您的特定問題有關,它應該類似於:

"INSERT INTO xxMyDatabasexx (ParticipantID, Verifier, Notes, [Date]) VALUES  (@participantID, @userVerify, @notes, @dt)"

 _commandSql.Parameters.Add("@ParticipantID",SqlDbType.NChar).Value= participantID;
 _commandSql.Parameters.Add("@userVerify",SqlDbType.NChar).Value= userVerify ;
 _commandSql.Parameters.Add("@notes",SqlDbType.NVChar).Value= properRow ;
 _commandSql.Parameters.Add("@dt",SqlDbType.DateTime).Value= DateTime.Now;

注意 :如果ParticipantIDIDENTITY (即自動編號)列,則不要在INSERT語句中包括它。

希望這會有所幫助。

在我看來,您有點迷路了。 適配器的工作方式是

  • 通過適配器從數據庫填充表(或獲取空表)
  • 將表綁定到GUI或將信息手動傳輸到GUI
  • 通過綁定或手動更改/添加/刪除表中的數據
  • 通過適配器將更改更新(插入/更新/刪除)到數據庫

表中的更改會自動跟蹤,因此適配器知道應該更新/插入/刪除的內容並使用適當的命令。

如果您僅將適配器用作命令的持有人,則可以使用任意參數執行ExecuteNonQuery,則可以傳遞整個概念,並且根本不需要適配器(請參閱@AlexBells答案)。

除此之外,您真的要手動編寫所有這些管道代碼嗎? 人生如此短暫。 如果我是您,我會尋找一些ORM。 您可以輕松獲得簡單的CRUD或並發檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM