Datagridview日期时间转换问题 [英] Datagridview datetime converion problem

查看:106
本文介绍了Datagridview日期时间转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试将日期时间插入sql表,并且在这里遇到错误.代码是:

Hello,
i''m trying to insert a datetime into a sql table and i''m getting an error here. the code is:

da.InsertCommand = new SqlCommand("INSERT INTO tblTestTable3 VALUES (@PrveCislo, @DruheCislo, @TretieCislo, @StvrteCislo, @PiateCislo, @Date)", conn);

da.InsertCommand.Parameters.Add("@Date", SqlDbType.DateTime).Value = (DateTime)this.dataGridView1.CurrentRow.Cells[0].Value;
da.InsertCommand.Parameters.Add("@PrveCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[1].Value;
da.InsertCommand.Parameters.Add("@DruheCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[2].Value;
da.InsertCommand.Parameters.Add("@TretieCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[3].Value;
da.InsertCommand.Parameters.Add("@StvrteCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[4].Value;
da.InsertCommand.Parameters.Add("@PiateCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[5].Value;
    da.InsertCommand.ExecuteNonQuery();




我到达这里的错误是:

不允许从数据类型datetime到int的隐式转换.使用CONVERT函数运行此查询."

sql表中的Date列是DateTime,它应该是唯一的.

我如何转换以及为什么要转换为int.有人可以帮忙吗?
JL




the error I''m getting here is :

"Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query."

Date column in sql table is DateTime and it should be unique.

How do I convert and why is it trying to convert to int. Can anybody help?
JL

推荐答案

da.InsertCommand = new SqlCommand("INSERT INTO tblTestTable3 VALUES (@PrveCislo, @DruheCislo, @TretieCislo, @StvrteCislo, @PiateCislo, @Date)", conn);
da.InsertCommand.Parameters.Add("@PrveCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[0].Value;
da.InsertCommand.Parameters.Add("@DruheCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[1].Value;
da.InsertCommand.Parameters.Add("@TretieCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[2].Value;
da.InsertCommand.Parameters.Add("@StvrteCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[3].Value;
da.InsertCommand.Parameters.Add("@PiateCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[4].Value;
da.InsertCommand.Parameters.Add("@Date", SqlDbType.DateTime).Value = (DateTime)this.dataGridView1.CurrentRow.Cells[5].Value;
    da.InsertCommand.ExecuteNonQuery();



希望以上信息对您有所帮助.如果您还有其他顾虑,请告诉我.



I hope the above information will be helpful. If you have more concerns, please let me know.


sql server for your date列中的数据类型是什么.似乎是时间戳,这是导致此问题的原因.
What is data type in sql server for you date column. It seems that it is timestamp and this is causing this issue.


这篇关于Datagridview日期时间转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆