我在添加数据到数据库的时候,报了个错误,无法将字符串转换成datetime类型,具体代码如下,各位高手多多指点。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Data.SqlClient;namespace FamilyFinance
{
public partial class NewShopping : Form
{
public NewShopping()
{
InitializeComponent();
} private void btn_No_Click(object sender, EventArgs e)
{
this.Close();
} private void btn_Yes_Click(object sender, EventArgs e)
{
string date = Time1.Text.Replace("年", "-").Replace("月", "-").Replace("日", "");
DateTime dt = Convert.ToDateTime(date);
string sql = string.Format("insert into Shopping values('{0}','{1}',{2},'{3}','{4}')",cbo_Type.Text,txt_Item.Text.Trim(),txt_Money.Text.Trim(),dt,txt_Re.Text.Trim());
SqlCommand cmd = new SqlCommand(sql,DBHelper.conn);
DBHelper.conn.Open();
int count = cmd.ExecuteNonQuery();
DBHelper.conn.Close();
if (count > 0)
{
MessageBox.Show("成功");
}
else
{
MessageBox.Show("失败");
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Data.SqlClient;namespace FamilyFinance
{
public partial class NewShopping : Form
{
public NewShopping()
{
InitializeComponent();
} private void btn_No_Click(object sender, EventArgs e)
{
this.Close();
} private void btn_Yes_Click(object sender, EventArgs e)
{
string date = Time1.Text.Replace("年", "-").Replace("月", "-").Replace("日", "");
DateTime dt = Convert.ToDateTime(date);
string sql = string.Format("insert into Shopping values('{0}','{1}',{2},'{3}','{4}')",cbo_Type.Text,txt_Item.Text.Trim(),txt_Money.Text.Trim(),dt,txt_Re.Text.Trim());
SqlCommand cmd = new SqlCommand(sql,DBHelper.conn);
DBHelper.conn.Open();
int count = cmd.ExecuteNonQuery();
DBHelper.conn.Close();
if (count > 0)
{
MessageBox.Show("成功");
}
else
{
MessageBox.Show("失败");
}
}
}
}
解决方案 »
- csv问价导入数据库出错
- URL截取求助
- public All<Money> getIn2010(Work harder):base(2009){}
- 如何用命令启动outlook express,并添加附件(解决问题后,增加到100分)
- C#如何创建未知类型类的数组?
- 请教C#打开SQL Express TCP协议
- sql 时间查询 问题?。。。
- 当方法中有out 或者ref参数时,应该怎么用MethodInfo.Invoke(...)调用
- C# 添加Newtonsoft.Json.dll引用的问题
- 请问开发数据库应用程序用c#结合ADO.net好?还是用PowerBuilder好???开发数据库应用程序用哪一种语言最好???
- C# 画直线问题
- SQL语句的问题
查看你机器上日期时间的显示格式是yyyy-MM-dd 还是yyyy/MM/dd,如果是前者的话,
string date = Time1.Text.Replace("年", "-").Replace("月", "-").Replace("日", "");
这行代码中替换 日 是应该以一空格替代,这样
string date = Time1.Text.Replace("年", "-").Replace("月", "-").Replace("日", " ");
DateTime dt = Convert.ToDateTime(date);
string sql = string.Format("insert into Shopping values('{0}','{1}',{2},'{3}','{4}')",cbo_Type.Text,txt_Item.Text.Trim(),txt_Money.Text.Trim(),dt,txt_Re.Text.Trim());改成这样试试: DateTime dt = DateTime.Parse(Time1.Text);
string sql = string.Format("insert into Shopping values('{0}','{1}',{2},'{3}','{4}')",cbo_Type.Text,txt_Item.Text.Trim(),txt_Money.Text.Trim(),dt.ToString("yyyy-MM-dd"),txt_Re.Text.Trim());
string sql = string.Format("insert into Shopping values('{0}','{1}',{2},'{3}','{4}')",cbo_Type.Text,txt_Item.Text.Trim(),txt_Money.Text.Trim(),dt,txt_Re.Text.Trim());看你上面的代码后 你看下你数据库的时间格式是什么,然后数据库的格式将 dt 格式化字符串 dt.ToString("yyyy-MM-dd");//yyyyMMdd等等
string sql = string.Format("insert into Shopping values('{0}','{1}',{2},'{3}','{4}')",cbo_Type.Text,txt_Item.Text.Trim(),txt_Money.Text.Trim(),dt(这里应该用时间的字符串格式,而不是日期型数据),txt_Re.Text.Trim());5楼已经给了解决方法