该程序使用C#WinForms和SQL Server 2008.当我想输入包含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());
}
Run Code Online (Sandbox Code Playgroud)
事情变得越来越厚..我在尝试将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();
Run Code Online (Sandbox Code Playgroud)
好的,这是我正在处理的表单的完整代码,显示此错误的表单.大多数表单的结构都是这样的,所以如果我把它弄好,那么其余表格应该没有任何问题.我正在我的计算机上测试它,所以如果它在这里工作它也应该在那里工作.
看一看,我不知道该怎么做了.
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
你应该使用这种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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
450 次 |
| 最近记录: |