该程序使用C#WinForms和SQL Server 2008.当我想输入包含一个DateTimePicker我可以看到的是,措辞是在荷兰,然后我得到一个错误有关的价值转换。有什么办法可以预先编程来解决这个问题吗?我发现了这个错误,现在就是这样。一个程序在我的电脑上工作正常,但是当我用另一个Windows荷兰语发送它时,它与DateTimePicker存在问题
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的值到数据库的方式,我用上面的代码做同样得到这个错误。它在我的电脑上工作得很好,但在这里不起作用。有人可以解释吗?以下是错误:使用
代码:
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();
}
}
}
需要代码,您从选取器中获取日期。不应该包含任何字符串日期转换。 –
@TonyHopkinson 有插入值的代码,它不能在那台计算机上做,所以我有捕获,这就是我得到的错误图像。 – Alternatex
@Tony实际上,考虑到他似乎在串联一个字符串来构建查询,看起来就像是这样。 –