应该使用哪种NpgsqlDbType清除“无法编写CLR类型”。错误 [英] What NpgsqlDbType should be used to clear "Can't write CLR type" error

查看:818
本文介绍了应该使用哪种NpgsqlDbType清除“无法编写CLR类型”。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PostgreSQL作为数据库。我有一个用

I am using PostgreSQL as my database. I have an application that is written in


  • ASP.NET

  • C#
  • $写的应用程序b $ b
  • WebForms

  • 在PostgreSQL中使用Npgsql

  • ASP.NET
  • C#
  • WebForms
  • Using Npgsql with PostgreSQL

我有一个名为<我数据库中的strong> tblAppt ,在该表中,有一列名为 appttime ,该列的数据类型为没有时区的时间

I have table named tblAppt in my database, in that table I have a column named appttime that column's Data type is time without time zone

在pgAdmin4的查询工具中,如果我运行以下查询:

In pgAdmin4's Query Tool if I run the following query:

INSERT INTO tblAppt (appttime) VALUES ('00:30:00');

结果是:

INSERT 0 1 Query returned successfully in 105 msec.

这意味着该记录已添加到我的表中,在图像中您可以看到我有一行:




现在,在我的Web应用程序中,我有一个名为 txtCustTime 的文本框,在该文本框中,我输入以下值 00:30:00

Which means the record was added in my table, in the image you can see I have a row:

Now in my web application I have a text box named txtCustTime, in that text box I enter this value 00:30:00

当我按下提交按钮时,出现以下错误:

When I press the submit button I get the following error:


System.InvalidCastException
不能用
处理程序类型TimeHandler编写CLR类型System.String

System.InvalidCastException
Can't write CLR type System.String with handler type TimeHandler

以下是提交内容的摘要:

Here is a snippet of what happens on the submit:

string insertstmt = "INSERT INTO tblAppt(appttime) VALUES (@ApptTime)";
NpgsqlCommand cmd = new NpgsqlCommand (insertstmt, con);
cmd.Parameters.Add("@ApptTime", NpgsqlDbType.Time );
cmd.Parameters ["@ApptTime"].Value = txtCustTime.Text;
con.Open ();
cmd.ExecuteNonQuery (); 
con.Close ();

我的问题是我是否需要强制转换,因为文本框中的值或使用其他数据类型,

My question is do I need to cast it because the values in a textbox or use a different datatype, or something else.

请注意: 该表中我还有其他列,这些列不是时间或日期,因此可以提交。我的问题似乎专门针对此特定列类型。


其他说明:
如果我在提交时运行,则值为添加到数据库中:

PLEASE NOTE: I have other columns in that table that are not time or dates and they submit fine. My problem seems to be specifically to this particular column type.
Additional NOTE: If I run this on submit the value is added to the database:

string insertstmt = "INSERT INTO tblAppt(appttime) VALUES ('00:30:00')";
NpgsqlCommand cmd = new NpgsqlCommand (insertstmt, con);
con.Open ();
cmd.ExecuteNonQuery (); 
con.Close ();

这告诉我我的问题我不明白与该参数有关。

Which tells me my problem I am not understanding has something to do with the parameter.

推荐答案

注释中的人是正确的,因为 TimeSpan 是必经之路。但是,有关如何使用它的文档并不十分清楚。

The folks in the comments were correct in that TimeSpan is the way to go. However the documentation was not very clear on how to use it.

针对上述问题使用 TimeSpan 的正确方法是:

The correct way to use TimeSpan for the problem above is:

string insertstmt = "INSERT INTO tblAppt(appttime) VALUES (@ApptTime)"; 
NpgsqlCommand cmd = new NpgsqlCommand (insertstmt, con);
TimeSpan thetime = TimeSpan.Parse(txtCustTime.Text);

cmd.Parameters.Add("@ApptTime", NpgsqlDbType.Time ); 
cmd.Parameters["@ApptTime"].Value = thetime; 
con.Open (); 
cmd.ExecuteNonQuery (); 
con.Close ();

我使用此链接作为参考:
https://docs.microsoft.com/zh-CN /dotnet/api/system.timespan?redirectedfrom=MSDN&view=netframework-4.7.2

I used this link as a reference: https://docs.microsoft.com/en-us/dotnet/api/system.timespan?redirectedfrom=MSDN&view=netframework-4.7.2

这篇关于应该使用哪种NpgsqlDbType清除“无法编写CLR类型”。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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