【问题标题】:How to update date in SQL Server through C# using a stored procedure如何使用存储过程通过 C# 更新 SQL Server 中的日期
【发布时间】:2015-09-17 20:36:39
【问题描述】:

在我的项目中更新客户的出生日期时遇到问题。我使用存储过程来执行此操作,每次尝试更新它都会给我,即使我指定了我的存储过程

列 'dateBirth' 不属于表数据集。

存储过程:

 ALTER PROCEDURE [dbo].[uspUpdateDate]
     @clientID INT,
     @dateBirth DATE
 AS
 BEGIN
     UPDATE Client
     SET DOB = @dateBirth
     WHERE ClientID = @clientID
 END

更新功能:

 public void Update(string proc, string[] param, string[] value)
 {
        updateCommand = new SqlCommand(proc, connectionString.UConnect);
        updateCommand.CommandType = CommandType.StoredProcedure;

        for (int x = 0; x < param.Length; x++)
            updateCommand.Parameters.Add('@' + param[x], SqlDbType.VarChar, 0, param[x]).Value = value[x];

        for (int x = 0; x < param.Length; x++)
        {
            if (value[x] == "")
                dataTable.Rows[0][param[x]] = DBNull.Value;
            else
                dataTable.Rows[0][param[x]] = value[x];
        }

        dataAdapter.UpdateCommand = updateCommand;
        dataAdapter.Update(dataTable);
 }

这是我在 Visual Studio 中使用的代码:

 private void btnSaveC_Click(object sender, EventArgs e)
 {
        string nodeTag = treeView.SelectedNode.Tag.ToString();
        Select("uspSelectClientByID",
           new string[] { "clientID" },
           new string[] { nodeTag });
        Update("uspUpdateDate", new string[] { "clientID", "dateBirth" },
            new string[] { nodeTag, DateTime.Now.ToString() });
 }

我知道我的函数有效,因为我尝试将它与具有 VARCHAR 数据类型的不同列一起使用。

【问题讨论】:

  • 你的代码在哪里Executing update 语句.. 对如何使用 C# 和 Sql 服务器更新表进行简单的谷歌搜索.. 有大量示例,例如 google search the following C# Command.ExecuteNonQuery()并查看许多产生的结果..
  • 这是一个来自之前堆栈溢出帖子的简单链接,您可以关注该链接还有更多 stackoverflow.com/questions/28676319/…
  • 一目了然,我注意到您正在将要发送的日期转换为字符串,但是,您的存储过程期望该值是一个日期。也许你应该看看那里。
  • 显示Update 函数的代码。
  • 好了,伙计。我认为(很可能)这是因为我所做的整个字符串的事情

标签: c# sql sql-server visual-studio


【解决方案1】:

存储过程中的变量应该与列名相似。例如,如果表上的列是LastName,那么存储过程中的变量应该是lastName 或一些类似的变体;否则,您将在 Visual Studio 中遇到错误。就我而言,将dob 作为变量起作用。由于我的函数中的代码,我正在传递要转换为 VARCHAR 的字符串:

updateCommand.Parameters.Add('@' + param[x], SqlDbType.VarChar, 0, param[x]).Value = value[x]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多