2012-05-21 21 views
1

该程序使用C#WinForms和SQL Server 2008.当我想输入包含一个DateTimePicker我可以看到的是,措辞是在荷兰,然后我得到一个错误有关的价值转换。有什么办法可以预先编程来解决这个问题吗?我发现了这个错误,现在就是这样。一个程序在我的电脑上工作正常,但是当我用另一个Windows荷兰语发送它时,它与DateTimePicker存在问题

error

 try 
     { 
      SqlConnection connect = new SqlConnection("Data Source=Localhost\\SQLExpress;Initial Catalog=DataBase;Integrated Security=True"); 
      SqlDataAdapter da = new SqlDataAdapter(); 

      /******************** Inserting ********************/ 
      string query = "INSERT INTO spending VALUES ("; 
      query += "'" + date_dateTimePicker.Value + "', "; // Date 
      query += "'" + Convert.ToDecimal(amount_spent_textBox.Text) + "', "; // Amount spent 
      query += "'" + spent_on_textBox.Text + "')"; // Spent on 
      connect.Open(); 
      da.InsertCommand = new SqlCommand(query, connect); 
      da.InsertCommand.ExecuteNonQuery(); 

      connect.Close(); 
     } 

     catch (Exception error) 
     { 
      MessageBox.Show(error.ToString()); 
     } 

事情变得厚。我试图插入一个dateTimePicker的值到数据库的方式,我用上面的代码做同样得到这个错误。它在我的电脑上工作得很好,但在这里不起作用。有人可以解释吗?以下是错误:使用

error

代码:

string update = "UPDATE table SET the_date = '" + the_date_dateTimePicker.Value + "' WHERE instance_ID = 1"; 
     connect.Open(); 
     da.InsertCommand = new SqlCommand(update, connect); 
     da.InsertCommand.ExecuteNonQuery(); 
     connect.Close(); 

好这里是我现在工作的形式,在一个显示this error完整的代码。大多数形式的结构是这样,所以如果我得到这个权利,不应该有与他们的其余任何问题。我正在测试这个在我的电脑上,所以如果它在这里工作,它也应该在那里工作。

看看,我不知道该怎么办了。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace TheNamespace 
{ 
public partial class submit_spending : Form 
{ 
    public submit_spending() 
    { 
     InitializeComponent(); 
    } 

    private void submit_button_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      SqlConnection connect = new SqlConnection("Data Source=Localhost\\SQLExpress;Initial Catalog=TheDataBase;Integrated Security=True"); 
      SqlDataAdapter da = new SqlDataAdapter(); 

      /******************** Inserting ********************/ 
      string query = "INSERT INTO spending VALUES (@date, @amount_spent, @spent_on)"; 
      SqlCommand command = new SqlCommand(query); 
      command.Parameters.AddWithValue("date", date_dateTimePicker.Value); 
      command.Parameters.AddWithValue("amount_spent", Convert.ToDecimal(amount_spent_textBox.Text)); 
      command.Parameters.AddWithValue("spent_on", spent_on_textBox.Text); 

      connect.Open(); 
      da.InsertCommand = new SqlCommand(query, connect); 
      da.InsertCommand.ExecuteNonQuery(); 
      connect.Close(); 

      if (MessageBox.Show("Submitted.", "Spending submitted", MessageBoxButtons.OK) == DialogResult.OK) 
      { 
       this.Close(); 
      } 
     } 
     catch (Exception error) 
     { 
      MessageBox.Show(error.ToString()); 
     } 
    } 

    private void cancel_button_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 
} 
} 
+0

需要代码,您从选取器中获取日期。不应该包含任何字符串日期转换。 –

+0

@TonyHopkinson 有插入值的代码,它不能在那台计算机上做,所以我有捕获,这就是我得到的错误图像。 – Alternatex

+2

@Tony实际上,考虑到他似乎在串联一个字符串来构建查询,看起来就像是这样。 –

回答

1

你或许可以用一个明确的ToString与文化-invariant格式来确保它可以在任何语言环境中使用。这是一个黑客攻击的一位,但将这一行:

command.Parameters.AddWithValue("date", date_dateTimePicker.Value); 

与这应该工作:

command.Parameters.AddWithValue("date", date_dateTimePicker.Value.ToString("s")); 

这会给你一个ISO 8601格式的日期 - 时间字符串,这是一个国际标准具有明确的规范。此外,此格式不受SQL Server实例上SET DATEFORMAT或SET LANGUAGE设置的影响。

3

也许你有日期时间格式化的问题,它可以通过改变系统日期时间格式到同一个如电脑,它正在被解决,或者你可以在你的代码做一些额外的工作。对于自定义日期时间格式有一个看看这里: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

+2

或者你可以简单地通过'ToString()'输出服务器可以理解的日期格式。 –

0

只需使用dateTime.ToString("s"),它会给在通用格式的日期时间,无论计算机的文化背景。

7

您应该使用Parameters.AddWithValue()方法。我希望它能正确格式化DateTime

string cmdText = "UPDATE table SET colX = @value WHERE id = @currId"; 
var cmd = new SqlCommand(cmdText, conn); 
cmd.Parameters.AddWithValue("value", dateTimePicker1.Value); 
cmd.Parameters.AddWithValue("currId", id); 
+0

当我尝试 'string query =“INSERT INTO花费VALUES(@date,@amount_spent,@spent_on)”; connect.Open(); SqlCommand command = new SqlCommand(query); command.Parameters.AddWithValue(“date”,date_dateTimePicker.Value); command.Parameters.AddWithValue(“amount_spent”,Convert.ToDecimal(amount_spent_textBox.Text)); command.Parameters.AddWithValue(“spent_on”,spent_on_textBox.Text);' 它说[this](http://i.imgur.com/6n7Xu.jpg) – Alternatex

+0

你确定'date'不是空值?您也可以在创建命令后进行连接。我没有附近有C#的电脑来测试。 – Minustar

+0

我试着在代码后打开连接,但出现同样的错误。日期不能为空,因为它来自dateTimePicker,其默认值始终设置为Now/Today。 – Alternatex

1

理想情况下,你应该写在T-SQL语句中使用datetime参数,应用程序,以便转换永远不会进入画面。

在您的处置的其他途径是通过您的应用程序使用的连接(一个或多个)上添加SET DATEFORMAT。因此,您可以将格式更改为应用程序预期/使用的格式。

0
"UPDATE table SET the_date = '" + the_date_dateTimePicker.Value + "' WHERE instance_ID = 1"; 

使用默认的CultureInfo转换为String时发生在这里。日期时间可以转换到两个“29/05/2012”和“05月29日”,这取决于你的工作站上选择的区域设置。然而,Sql Server可能被配置为另一个区域设置,因此你有问题(sql server无法解析传递的DateTime的字符串表示)

如果你不想面对另一个与CultureInfo相关的问题,你必须使用参数化查询后来。

1

我认为你需要指定SqlDBType在command.Parameters分配值到年月日时。并请交叉检查您已在INSERT语句与参数中指定的参数中指定的参数名称。添加

string sql = "INSERT INTO spending VALUES (@date, @amount_spent, @spent_on)"; 
using (var cn = new SqlConnection("..connection string..")) 
using (var cmd = new SqlCommand(sql, cn)) 
    { 
cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = date_dateTimePicker.Value; 
command.Parameters.AddWithValue("@amount_spent",Convert.ToDecimal(amount_spent_textBox.Text)); 
     command.Parameters.AddWithValue("@spent_on", spent_on_textBox.Text); 

     connect.Open(); 

有的时候它能够更好地使用固定区域性存储之类的东西在数据库中的日期/时间。你可以尝试CultureInfo.Invariantculture

DateTime date = date_dateTimePicker.Value.Date; 
string sDate = date.ToString("dd-MM-yy", System.Globalization.CultureInfo.InvariantCulture); 
DateTime dateInsert = Convert.ToDateTime(sDate); 
相关问题