c datetime数据类型(sql server怎么插入datetime啊)

本文目录
- sql server怎么插入datetime啊
- c# int型转化为 日期类型!
- C# 关于DateTime类型 精确到毫秒
- sql server 插入datetime数据:从字符串转换日期和/或时间时,转换失败
- 时间序列数据处理基础
sql server怎么插入datetime啊
试试:
string sql = "insert into SharingTable(share_title,share_content,share_agreement,share_date,share_type)values(@ShareTitle,@ShareContent,convert(int,@ShareDate,8),@ShareAgreement,@ShareType)";
SqlConnection conn=new SqlConnection(connectionString);//connectionString连接字符串
SqlCommand cmd=new SqlCommand(sql,conn);//conn 为SqlConnection实例
//下面这种方式的好处是 类型不用管它,让系统自己去判断,只要传值是传对的就可以了
cmd.Parameters.AddWithValues("@ShareTitle",sEvent.ShareTitle);
cmd.Parameters.AddWithValues("@ShareContent",sEvent.ShareContent);
cmd.Parameters.AddWithValues("@ShareDate",DateTime.Now);
cmd.Parameters.AddWithValues("@ShareAgreement",0);
cmd.Parameters.AddWithValues("@ShareType",shareTypeId);
try{
if(conn.State!=ConnectionState.Opened)
conn.Open();
cmd.ExecuteNonQuery();
}
catch{}
finally
{
if(conn.State!=ConnectionState.Closed)
conn.Close();
}
-----------------------------------------
从你上面的提示看,我觉得不是错在日期格式,“提示无法隐式的转成int”,可能是这个ShareAgreement 或 shareTypeId,没看到表结构,只是猜的。
c# int型转化为 日期类型!
c# 各数据类型转换
1、数据类型的类名
这里讲的数据的类名指的是: Sytem.data.DbType对应的类型,我是这样理解的。
类名 System中相对应的类型
bool System.Boolean (布尔型,其值为 true 或者 false)
char System.Char (字符型,占有两个字节,表示 1 个 Unicode 字符)
byte System.Byte (字节型,占 1 字节,表示 8 位正整数,范围 0 ~ 255)
sbyte System.SByte (带符号字节型,占 1 字节,表示 8 位整数,范围 -128 ~ 127)
ushort System.UInt16 (无符号短整型,占 2 字节,表示 16 位正整数,范围 0 ~ 65,535)
uint System.UInt32 (无符号整型,占 4 字节,表示 32 位正整数,范围 0 ~ 4,294,967,295)
ulong System.UInt64 (无符号长整型,占 8 字节,表示 64 位正整数,范围 0 ~ 大约 10 的 20 次方)
short System.Int16 (短整型,占 2 字节,表示 16 位整数,范围 -32,768 ~ 32,767)
int System.Int32 (整型,占 4 字节,表示 32 位整数,范围 -2,147,483,648 到 2,147,483,647)
long System.Int64 (长整型,占 8 字节,表示 64 位整数,范围大约 -(10 的 19) 次方到 10 的 19 次方)
float System.Single (单精度浮点型,占 4 个字节)
double System.Double (双精度浮点型,占 8 个字节)
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TypeTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bool bo = true;
byte b = 9;
char c = ’a’;
sbyte sb = 8;
short s = 8;
int i= 7;
uint u = 6;
long l = 5;
this.textBox1.Text = "typetest";
this.textBox1.AppendText("bool -》 " + bo.GetType().FullName + ""n");
this.textBox1.AppendText("byte -》 " + b.GetType().FullName + ""n");
this.textBox1.AppendText("char -》 " + c.GetType().FullName + ""n");
this.textBox1.AppendText("sbyte -》 " + sb.GetType().FullName + ""n");
this.textBox1.AppendText("short -》 " +s.GetType().FullName + ""n");
this.textBox1.AppendText("int -》 " + i.GetType().FullName + ""n");
this.textBox1.AppendText("uint -》 " +u.GetType().FullName + ""n");
this.textBox1.AppendText("long -》 " +l.GetType().FullName + ""n");
//其实类型就不写上,自己可以真接加上去!
}
}
}
结果可看到:
typetestbool -》 System.Boolean
byte -》 System.Byte
char -》 System.Char
sbyte -》 System.SByte
short -》 System.Int16
int -》 System.Int32
uint -》 System.UInt32
long -》 System.Int64
//
说明,以后所编的代码都是写在private void Form1_Load(object sender, EventArgs e)中的
2、Value Type间的转换。
bool bo = true;
byte b = 9;
char c = ’a’;
sbyte sb = 8;
short s = 8;
int i = 7;
uint u = 6;
long l = 5;
this.textBox1.Text = "datatype";
this.textBox1.AppendText("bool bo=" + bo.ToString() + ""n");
this.textBox1.AppendText("byte b= " + b.ToString() + ""n");
this.textBox1.AppendText("char c= " + c.ToString() + ""n");
this.textBox1.AppendText("sbyte sb= " + sb.ToString() + ""n");
this.textBox1.AppendText("short s= " + s.ToString() + ""n");
this.textBox1.AppendText("int i= " + i.ToString() + ""n");
this.textBox1.AppendText("uint u=" + u.ToString() + ""n");
this.textBox1.AppendText("long l= " + l.ToString() + ""n");
此段代码并没有转换数据类型,只说明它们的类型公别还是System.bool型…System.long型。
追加一行:int g = 1;
short h = g;
this.textBox1.AppendText("h = " + h.ToString() + ""n");
结果编译报错:
G:"Projects"Visual C#"Convert"Form1.cs(118): 无法将类型“int”隐式转换为“short”
数据要进行强制转换。
如上例修改如下:
short g = 1;
byte h = (byte) g; // 将 short 型的 g 的值强制转换成byte型后再赋给变量 h
this.textBox1.AppendText("h = " + h.ToString() + ""n");
就可以了!
Short-》byte
short g = 265; //265 = 255 + 10
byte h = (byte) g;
this.textBox1.AppendText("h = " + h.ToString() + ""n");
注意:溢出问题!
3、ASCII《-》Unicode
char ch = ’a’;
short ii = 65;
this.textBox1.Text = "";
this.textBox1.AppendText("The ASCII code of "’" + ch + ""’ is: " + (short)ch + ""n");
this.textBox1.AppendText("ASCII is " + ii.ToString() + ", the char is: " + (char)ii + ""n");
char name1 = ’屈’;
char name2 = ’志’;
short name3 = 21195;
this.textBox1.AppendText("The Unicode of "’" + name1 + ""’ is: " + (short)name1 + ""n");
this.textBox1.AppendText("The Unicode of "’" + name2 + ""’ is: " + (short)name2+ ""n");
this.textBox1.AppendText("Unicode is " + name3.ToString() + ", the name3 is: " + (char)name3 + ""n");
它的运行结果是
The ASCII code of ’a’ is: 97
ASCII is 65, the char is: A
The Unicode of ’屈’ is: 23624
The Unicode of ’志’ is: 24535
Unicode is 21195, the name3 is: 勋
4、int《-》string
float f = 12.3f;
string str = "258";
this.textBox1.Text = "";
this.textBox1.AppendText("f = " + f.ToString() + ""n");//float-》string
if (int.Parse(str) == 258) //string-》int
{
this.textBox1.AppendText("str convert to int successfully.");
}
else
{
this.textBox1.AppendText("str convert to int failed.");
5、String《-》char
string str = "quzhixun";
char
this.textBox1.Text = "";
this.textBox1.AppendText("Length of ""quzhixun"" is " + str.Length + ""n");
this.textBox1.AppendText("Length of char array is " + chars.Length + ""n");
this.textBox1.AppendText("char + ""n");
char name = { ’q’, ’u’, ’z’, ’h’, ’i’, ’x’, ’u’,’n’ };
string sname = new String(name);//char-》string
this.textBox1.AppendText("sname = """ + sname + """"n");
6、String《-》byte
string s = "hi,屈志勋";
byte,半个英文1个字节,汉字2 个字节。
byte,都是两个字节。
string t1 = "", t2 = "";
foreach (byte b in b1)
{
t1 += b.ToString("") + " ";
}
foreach (byte b in b2)
{
t2 += b.ToString("") + " ";
}
this.textBox1.Text = "";
this.textBox1.AppendText("b1.Length = " + b1.Length + ""n");
this.textBox1.AppendText(t1 + ""n");
this.textBox1.AppendText("b2.Length = " + b2.Length + ""n");
this.textBox1.AppendText(t2 + ""n");
//
byte b = { 65, 66, 67 };
string s = System.Text.Encoding.ASCII.GetString(b);//byte-》string
this.textBox1.AppendText("The string is: " + s + ""n");
//
7、转换十六进制
int a = 159357;
this.textBox1.Text = "";
this.textBox1.AppendText("a(10) = " + a.ToString() + ""n");
this.textBox1.AppendText("a(16) = " + a.ToString("x6") + ""n");
this.textBox1.AppendText("a(16) = " + a.ToString("X6") + ""n");
8、DateTime《-》long
double doubleDate = DateTime.Now.ToOADate();//按原来的double值输出,DateTime-》long
DateTime theDate = DateTime.FromOADate(doubleDate);//从原来的的double值获得System.DateTime对象,long-》DateTime
this.textBox1.Text = "";
this.textBox1.AppendText("Double value of now: " + doubleDate.ToString() + ""n");
this.textBox1.AppendText("DateTime from double value: " + theDate.ToString() + ""n");
//
9、form DateTime
DateTime now = DateTime.Now;
string format;
this.textBox1.Text = "";
format = """year"":yyyy,""month"":MM,""day"":dd HH:mm:ss";
this.textBox1.AppendText(format + ": " + now.ToString(format) + ""n");
format = "yy年M日d日";
this.textBox1.AppendText(format + ": " + now.ToString(format) + ""n");
C# 关于DateTime类型 精确到毫秒
datetime包含毫秒,要格式化输出,用fff
DateTime t = DateTime.Now;
Console.WriteLine(t.ToString("yyyy-MM-dd hh:mm:ss fff"));
注:mysql里面的datetime类型的精确度是可以到1/ 10 ^ 6 秒的,某些客户端(如navicat for mysql)的显示经常只能看到精确到秒,其实是设计表的时候的配置问题。
扩展资料:
mysql中DateTime和Timestamp
DateTime
1、8个字节储存(8 bytes storage)
2、实际格式储存(Just stores what you have stored and retrieves the same thing which you have stored.)
3、与时区无关(It has nothing to deal with the TIMEZONE and Conversion.)
4、存储的时间范围为:’1000-01-01 00:00:00.000000’ 到 ’9999-12-31 23:59:59.999999’
Timestamp
1、4个字节储存(Time stamp value is stored in 4 bytes)
2、值以UTC格式保存( it stores the number of milliseconds)
3、时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。
4、存储的时间范围为:’1970-01-01 00:00:01.000000’ 到 ’2038-01-19 03:14:07.999999’
sql server 插入datetime数据:从字符串转换日期和/或时间时,转换失败
1.
datetime
是类型(日期类型)
2.
如果是要向一个表中插入日期
3.
sql
如下:
4.
创建一个表table,字段为cdatetime
5.
insert
into
table(cdatetime)
6.
values(getdate())
时间序列数据处理基础
时间序列是指将某种现象某一个统计指标在不同时间上的各个数值,按时间先后顺序排列而形成的序列。时间序列法是一种 定量预测 方法,亦称简单外延方法。在统计学中作为一种常用的预测手段被广泛应用。 时间序列分析 在第二次世界大战前应用于经济预测。二次大战中和战后,在军事科学、 空间科学 、气象预报和工业自动化等部门的应用更加广泛。 时间序列分析 (Time series analysis)是一种动态数据处理的 统计方法 。该方法基于随机过程理论和数理统计学方法,研究随机数据序列所遵从的 统计规律 ,以用于解决实际问题。- 360百科
随着移动互联和物联网等的发展,现在很多领域也都会有时间序列数据需要进行处理,除了金融、农业、经济学、生态学、物理学等,还有现在很多地方常见的数据形式,数据点是根据某种规律定期出现(比如每15秒,每5分钟、每月出现一次)。
构成要素:长期趋势,季节变动,循环变动,不规则变动
长期趋势( T )现象在较长时期内受某种根本性因素作用而形成的总的变动趋势,例如股票的价格有的会看几年内的发展,需要考虑整体大的经济环境,行业,已经公司等的发展。
季节变动( S )现象在一年内随着季节的变化而发生的有规律的周期性变动,比如股票会受短期的疫情,天气,人员行业变动等等内外部因素影响,在某些时段出现波动。
循环变动( C )现象以若干年为周期所呈现出的波浪起伏形态的有规律的变动,比如岁冷热季节涨跌的变化,旅游的热季淡季等。
不规则变动(I )是一种无规律可循的变动,包括严格的随机变动和不规则的突发性影响很大的变动两种类型
根据不同使用场景,一般分为几种
datetime.datetime(2020, 4, 28, 9, 45, 2, 474920)
看当前时间的各个值
2020 4 28 9 45 2
时间类型可以直接相减,得到间隔
结果是间隔类型
datetime.timedelta(days=218, seconds=53100)
218
同样的,可以给 datetime 对象加上(或减去)一个或多个 timedelta,这样会产生一个新对象:
datetime.datetime(2014, 6, 17, 0, 0)
datetime 模块中的数据类型:
’2015-09-09 00-00-00’
’2015-09-09’
datetime.datetime(2015, 9, 9, 0, 0)
datetime.strptime 是通过已知格式进行日期解析的最佳方式。但是每次都要编写格式定义是很麻烦的事情,尤其是对于一些常见的日期格式。这种情况下,你可以用 deteutil 这个第三方包中的 parser.parse 方法:
datetime.datetime(2015, 8, 9, 0, 0)
dateutil 可以解析几乎所有人类能够理解的日期表示形式]: i ,不过可惜中文不行
datetime.datetime(1993, 1, 31, 22, 10)
国际通用的格式中,日通常出现在月的前面
传入 dayfirst=True 即可解决这个问题
datetime.datetime(1993, 1, 31, 22, 10)
pandas 通常是用于处理成组日期的,不管这些日期是 DataFrame 的轴索引还是列。to_datetime 方法可以解析多种不同的日期表示形式。对标准日期格式(如 ISO8601)的解析非常快
它还可以处理缺失值(None、空字符串等)
NaT(Not a Time) 是 pandas 中的时间戳数据的 NA 值
datetime 格式定义:
有了时间数据对象基础,一系列的时间和指标就可以组成时间序列,可以用series对象存储处理,日期可以变成索引列
这些 datetime 对象实际是被放在一个 DatetimeIndex 中
现在 ts 就变成为一个 TimeSeries 了
不同索引的时间序列之间的算术运算会自动按日期对齐
只要需要,TimeStamp 可以随时自动转换为 datatime 对象。此外,它还可以存储频率信息(如果有的话),且知道如何执行时区转换以及其他操作。
由于 TimeSeries 是 Series 的一个子类,所以在索引以及数据选取方面他们的行为是一样的
由于大部分时间序列数据都是按照时间先后排序的
因此我们可以用不存在于时间序列中的时间戳进行切片
跟之前一样,这里可以传入字符串日期、datetime 或 Timestamp。此外还有一个等价的实例方法也可以截取两个日期之间的 TimeSeries:
在某些应用场景中,可能会存在多个观测数据落在同一个时间点上的情况
通过检查索引的 is_unique 属性,我们可以知道它是不是唯一的
对这个时间序列进行索引,要么产生标量值,要么产生切片
具体要看所选的时间点是否重复
假设我们想要对具体非唯一时间戳的数据进行聚合。一个办法是使用 groupby,并传入 level=0(索引的唯一一层):

更多文章:
myeclipse激活了还是要激活(myeclipse为什么要激活)
2026年7月3日 22:30
phpcms 自定义字段标签模型(phpcms建议PHPCMS在评论功能上进行扩展,实现评论自定义字段)
2025年10月13日 22:30
android根据源码解决问题(如何解决android ndk r8c 老是重新编译源代码的问题)
2026年3月30日 00:30
selectorgadget下载(bootntrselector加载不成功)
2025年9月21日 13:30
如何输入string类型字符串(博图画面中string类型怎么输入)
2026年5月6日 23:15
format函数千位分隔符(15、pandas的设置数字格式,小数位数、百分号、千位分隔符)
2026年4月6日 17:30
c++多线程并发(C++的多线程里的事件是怎样定义的,事件和线程有啥联系和区别)
2026年9月20日 11:45
停止天然砂出口台湾(急求中国2010年10月11日起禁止出口的货物目录)
2025年6月19日 16:00














