2013-05-15 40 views
0

我有一个日期字段在一个表中显示为日 - 月 - 年Hour.Minute.Seconds,我试图更新HOUR字段时,有相同的Ertnumber和日期。我可以使用相同的Ertnumber更新字段,但是当我尝试确保日期相同时,我得到一个错误。我有麻烦使我的日期时间格式相同sqls。 我在C#中通过创建日期时间:c#中的SQL更新语句的日期问题

DateTime dateStamp = new DateTime(2013, 2, 14, 1, 0, 0); 

这是我更新的字符串。

String.Format("update sdeadmin.meter_data_fixed_network set HOUR{2} = {0} where ERTNUMBER = '{1}' and DATETIME = '{3}'", this.Read, this.ertNumber, this.Stamp.Hour, this.DateStamp.ToString("MMddyyyyHHmmss")); 
+0

什么是错误? – meda

+0

*“我有麻烦”*不是SQL Server错误消息。 –

+1

但是'MMddyyyyHHmmss'无论如何都会很麻烦。国际上接受的通用日期格式是'yyyy-MM-dd [hh:mm [:ss [:nnn]]](时间,秒和毫秒可选) –

回答

4

试着做这样的事情: Datetime parameter for SQL Queries 你应该做一个参数化查询,而不是一个的String.Format()

+0

我觉得这是答案,所以+1,但是最好能展示一个代码示例来帮助解释OP。 –

0

(假设你的意思的SQL Server):与使用的最佳日期格式SQL Server是ISO 8601:

yyyyMMdd HHmmss。

然而,用String.Format编写你的SQL是一个可怕的做法。使用System.Data.SQLClient.SQLCommand和参数,格式不会打扰你。

DateTime dateStamp = new DateTime(2013, 2, 14, 1, 0, 0); 

System.Data.SQLClient.SQLConnection cxn; // make sure to set this up and open it 

System.Data.SQLClient.SQLCommand cmd = new System.Data.SQLClient.SQLCommand(
    String.Format("update sdeadmin.meter_data_fixed_network set HOUR{0} = @value where ERTNUMBER = @ert and DATETIME = @date", this.Stamp.Hour) 
    ,cxn); 

cmd.Parameters.Add(new SqlParameter("@value", this.Read); 
cmd.Parameters.Add(new SqlParameter("@ert", this.ertNumber); 
cmd.Parameters.Add(new SqlParameter("@date", this.Stamp); 
cmd.ExecuteNonQuery(); 
+1

如果我们正在教授正确的格式(我认为这是一个非常好的想法),那么您应该包括'使用'块 –

+0

请随时编辑我的答案。我是C#新手,我的实力是VB.NET –

+1

短跑在[他的回答](http://stackoverflow.com/a/16571134/80274)中增加了一个很好的例子, –

2

查询的参数化应该可以解决此问题;然而,你的问题实际上分为两部分。您需要首先构建引用可以更改的列名称的查询,HOUR+stamp.Hour,和查询参数。

因此,像下面应该为你工作:

string query = 
    String.Format("update sdeadmin.meter_data_fixed_network SET HOUR{0} = @read WHERE ERTNUMBER = @ertnumber AND DATETIME = @date;", this.Stamp.Hour); 

这建立你的基本的查询 - 你知道有一个参数化查询,将更新的sdeadmin.meter_data_fixed_network各自HOUR列。剩下的就是创建一个连接对象,一个命令对象,并在执行之前向其中添加参数。

例如:

//Create the connection 
using(SqlDbConnection connection = new SqlDbConnection("your_connection_string")) 
{ 
    //Create the Command 
    using(SqlDbCommand command = new SqlDbCommand(query)) 
    { 
     //Set up the properties of the command and the parameters 
     command.CommandType = CommandType.Text; 
     command.Connection = connection; 
     command.Parameters.AddWithValue("@read", Read); 
     command.Parameters.AddWithValue("@ertnumber", ertNumber); 
     command.Parameters.AddWithValue("@date", DateStamp); 
     //Have to open the connection before we do anything with it 
     connection.Open(); 
     //Execute the command. As we don't need any results back we use ExecuteNonQuery 
     command.ExecuteNonQuery(); 

    } 
}//At this point, connection will be closed and disposed - you've cleaned up 

有几个优点参数化查询:

  1. 可以帮助防止SQL注入攻击
  2. 许多数据库引擎可以重复使用参数化查询执行计划,改善性能

@JeffAtw ood在几年前在这个主题上写道:http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html

还要注意使用USING声明。这将确保连接和命令对象在您离开相应使用的范围后尽快处理。这很重要,因为虽然.Net会管理它控制的资源,但它不能管理文件句柄,数据库连接等外部资源,所以重要的是你要自己清理。 Dispose for Connection也将显式关闭它。