无法向 SQL Server 表添加或更新简单行

发布于 2024-11-29 03:01:33 字数 2124 浏览 1 评论 0原文

来自使用 SQL select 语句的 Access DB 背景,我发现将数据推送到 SQL Server 数据库并更新记录涉及更多工作。我对在 SQL Server 中添加记录的最初理解需要一个包含 INSERT 命令的非常长的语句。对我来说,这似乎不必要地复杂,而且我必须相信,对于今天的 .NET,它必须要简单得多。

我研究并审查了 DataTableDataSetSqlCommandSqlAdapters 等的几个代码示例。其中没有一个尽管我的代码没有错误,但为我工作。我经历了这些示例的许多变化,因此我向 Stack Overflow 寻求帮助。

这就是我正在尝试做的事情。

  1. 连接到外部托管的 SQL Server(有效!)
  2. 打开一个名为 Dongle 的表。
  3. 使用序列号作为条件检查行。
  4. 如果存在,则更新该记录。
  5. 如果没有,则添加新记录。

我提供了我使用过的代码示例的最新突变。在运行代码之前,我手动向数据库添加了 1 条记录,以便我知道 SerialNumber 参数匹配。不幸的是,if (dt.Rows.Count > 0) 语句返回 false,并且从未尝试更新。

现在我会告诉你......我已经尝试了其他突变,这些突变确实成功地通过了更新每个字段的行。但是,当(成功)调用 Update() 函数时,没有任何内容传播到实际数据库。

这是代码:

SqlCommand cmd = new SqlCommand("SELECT * FROM Dongles WHERE SerialNumber = " + m_dongle.SerialNumber, server.SQLConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(da);

DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
   dt.Rows[0]["IsLeased"] = m_dongle.IsLeased == true ? 1 : 0;
   dt.Rows[0]["LeaseDaysRemaining"] = m_dongle.LeaseDaysRemaining;
   dt.Rows[0]["DateAssigned"] = DateTime.Now;
   dt.Rows[0]["ConfiguredBy"] = m_dongle.ConfiguredBy;
   dt.Rows[0]["LicenseHolder"] = m_dongle.LicenseHolder;
   dt.Rows[0]["ContactName"] = m_dongle.ContactName;
   dt.Rows[0]["ContactPhone"] = m_dongle.ContactPhone;
   dt.Rows[0]["ContactEmail"] = m_dongle.ContactEmail;
   dt.Rows[0]["SerialNumber"] = m_dongle.SerialNumber;
   dt.Rows[0]["IsNetworkDongle"] = m_dongle.IsNetworkDongle;
   dt.Rows[0]["NetworkMaxUsers"] = m_dongle.NetworkMaxUsers;
   dt.Rows[0]["NetworkAbsoluteMaxUsers"] = m_dongle.NetworkAbsoluteMaxUsers;
   dt.Rows[0]["IsAssigned"] = m_dongle.IsAssigned;
   dt.Rows[0]["DongleModel"] = m_dongle.DongleModel;
   dt.Rows[0]["SalesforceID"] = m_dongle.SalesforceID;

   da.Update(dt);

   MessageBox.Show("Dongle profile updated successfully.", "Profile Saved", 
                   MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}

Coming from an Access DB background using SQL select statements, I am finding that there is more work involved in pushing data into an SQL Server database and updating records. My initial understanding of adding records in SQL Server required a VERY LONG statement that included the INSERT command. This to me seems to be unnecessarily complicated and I have to believe that with .NET today, it has to be much simpler.

I have researched and reviewed several code samples for DataTable, DataSet, SqlCommand, SqlAdapters, etc. NONE of them work for me despite the fact that my code has no errors in it. I've gone through so many mutations of these examples that I am appealing to Stack Overflow for some help.

Here's what I am trying to do.

  1. Connect to a SQL Server hosted externally (works!)
  2. Open a single table called Dongles.
  3. Check for a row using the serial number as a criteria.
  4. If it exists, UPDATE the record.
  5. If not, then ADD the new record.

I am providing the latest mutation of the code samples that I have worked with. Before running the code, I manually added 1 record to the database so I know the SerialNumber argument matches. Unfortunately, the if (dt.Rows.Count > 0) statement returns false and the update is never attempted.

Now I will tell you this....I have tried other mutations that did successfully go through the lines that update each field. But when the Update() function was called (successfully), nothing was propagated to the actual database.

Here's the code:

SqlCommand cmd = new SqlCommand("SELECT * FROM Dongles WHERE SerialNumber = " + m_dongle.SerialNumber, server.SQLConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(da);

DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
   dt.Rows[0]["IsLeased"] = m_dongle.IsLeased == true ? 1 : 0;
   dt.Rows[0]["LeaseDaysRemaining"] = m_dongle.LeaseDaysRemaining;
   dt.Rows[0]["DateAssigned"] = DateTime.Now;
   dt.Rows[0]["ConfiguredBy"] = m_dongle.ConfiguredBy;
   dt.Rows[0]["LicenseHolder"] = m_dongle.LicenseHolder;
   dt.Rows[0]["ContactName"] = m_dongle.ContactName;
   dt.Rows[0]["ContactPhone"] = m_dongle.ContactPhone;
   dt.Rows[0]["ContactEmail"] = m_dongle.ContactEmail;
   dt.Rows[0]["SerialNumber"] = m_dongle.SerialNumber;
   dt.Rows[0]["IsNetworkDongle"] = m_dongle.IsNetworkDongle;
   dt.Rows[0]["NetworkMaxUsers"] = m_dongle.NetworkMaxUsers;
   dt.Rows[0]["NetworkAbsoluteMaxUsers"] = m_dongle.NetworkAbsoluteMaxUsers;
   dt.Rows[0]["IsAssigned"] = m_dongle.IsAssigned;
   dt.Rows[0]["DongleModel"] = m_dongle.DongleModel;
   dt.Rows[0]["SalesforceID"] = m_dongle.SalesforceID;

   da.Update(dt);

   MessageBox.Show("Dongle profile updated successfully.", "Profile Saved", 
                   MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

家住魔仙堡 2024-12-06 03:01:33

SerialNumber 是什么类型的列?如果它是一个字符串列,您需要在参数周围加上引号 - 当您使用它时,为了防止 SQL 注入攻击 - 使用参数化查询而不是将 SQL 语句连接在一起...... (作为一个额外的好处,参数将在需要时自动在值周围使用引号):

SqlCommand cmd = new SqlCommand("SELECT * FROM Dongles WHERE SerialNumber = @SerialNo", server.SQLConnection);

cmd.Parameters.Add("@SerialNo", SqlDbType.VarChar, 50).Value = m_dongle.SerialNumber.Trim();

SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(da);

DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
    .......
}

What kind of column is SerialNumber?? If it's a string column, you would need to put quotes around the parameter - and while you're at it, to prevent SQL injection attacks - use parametrized queries instead of concatenating together your SQL statements... (as an added benefit, the parameters will automagically use quotes around values, where needed):

SqlCommand cmd = new SqlCommand("SELECT * FROM Dongles WHERE SerialNumber = @SerialNo", server.SQLConnection);

cmd.Parameters.Add("@SerialNo", SqlDbType.VarChar, 50).Value = m_dongle.SerialNumber.Trim();

SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(da);

DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
    .......
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文