在插入查询时获取错误

时间:2022-07-28 15:40:14

Please help me to find the error in the below statements where i am getting error in only from code.

请帮助我查找下面语句中的错误,其中我只从代码中获得错误。

Create table Statement

Create table语句

cmmd.CommandText = "CREATE TABLE Users([User_ID] AUTOINCREMENT PRIMARY KEY, UserName Text(50), UserPwdText(200), [IsActive] YesNo, [ModBy] Long REFERENCES Users (User_ID), [ModDate] Date)";
cmmd.ExecuteNonQuery();

In the database table is created successfully.
Here I am Inserting values through code.

在数据库表中成功创建。我在这里通过代码插入值。

cmmd.CommandText = @"INSERT INTO Users ([UserName], [UserPwd], [IsActive], [ModBy],
           [ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=', 'true', '1', 
           'Datetime.Now.Date')";
cmmd.ExecuteNonQuery();

The exception is

唯一的例外是

Data type mismatch in criteria expression

标准表达式中的数据类型不匹配

6 个解决方案

#1


2  

UPDATE:

更新:

The recommended way is to use parameters. Using a parameterized query not only steps around SQL injection issues, but solves SQL Server date localization issues as well.

推荐的方法是使用参数。使用参数化查询不仅可以解决SQL注入问题,还可以解决SQL Server日期本地化问题。

SqlCommand cmd = new SqlCommand(@"INSERT INTO Users ([UserName], [UserPwd], [IsActive], [ModBy], [ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=', 'true', '1', @dateValue)");
cmd.Parameters.AddWithValue("@dateValue", DateTime.Now.Date);
.......
// do same for the other.

You have 2 parentheses after VALUES in your code

代码中的值后面有两个圆括号

Check this :

检查:

string format = "yyyy-mm-dd"; //or with date yyyy-mm-dd hh:mm:ss
cmmd.CommandText = string.Format(@"INSERT INTO Users ([UserName], [UserPwd], [IsActive], [ModBy], [ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=', 'true', '1', '{0}')", DateTime.Now.Date.ToString(format));

Also you should use parameters.

您还应该使用参数。

#2


5  

First of all check for the DataTypes of each Fields. And make sure that you have used appropriate DataTypes as per the tables. and if everythings ok then just try below query.

首先检查每个字段的数据类型。确保您已经使用了适当的数据类型。如果一切顺利,那么试试下面的查询。

cmmd.CommandText = @"INSERT INTO Users ([UserName], [UserPwd], [IsActive], 
            [ModBy],[ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=',
            'true', '1', 'Datetime.Now.Date')";
cmmd.ExecuteNonQuery();

#3


4  

There are two errors in your query first is double bracket and second one is DateTime.Now.Date. it is a .Net feature that you cannot execute it in string. And here you don't need to add @ at the beginning of the string. Also change 'True' to Yes

查询中有两个错误,第一个是双括号,第二个是DateTime.Now.Date。它是一个。net特性,你不能在字符串中执行它。这里不需要在字符串的开头添加@。也可以将“True”改为“Yes”

try this

试试这个

cmmd.CommandText = "INSERT INTO Users ([UserName], [UserPwd], [IsActive], 
            [ModBy],[ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=',
            Yes, '1', Date())";
cmmd.ExecuteNonQuery();

#4


2  

You have a typo:

你有一个错误:

cmmd.CommandText = @"INSERT INTO Users ([UserName], [UserPwd], [IsActive], [ModBy], [ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=', 'true', '1', 'Datetime.Now.Date')";

One parenthesis, not two.

一个括号,而不是两个。

#5


2  

With DateTime.Now.Date, are u trying to get the date?

DateTime.Now。约会,你想约会吗?

Try this:

试试这个:

cmmd.CommandText = @"INSERT INTO Users ([UserName], [UserPwd], [IsActive], [ModBy], [ModDate]) VALUES ('Admin', 'kov1ozyKAjas8awoej3oijhrqoi6q=', 'true', '1', Datetime.Now.Date)";
cmmd.ExecuteNonQuery();   

#6


1  

Try this, The datetime is a string if you put inside of the double quote or single quote, So you need the query look like.

试试这个,datetime是一个字符串,如果你把它放在双引号或单引号里面,所以你需要查询看起来像这样。

cmmd.CommandText = @"INSERT INTO Users ([UserName], [UserPwd], [IsActive], 
            [ModBy],[ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=',
            'true', '1', '" + DateTime.Now.Date + "')";
                    cmmd.ExecuteNonQuery();

I hopes my query is does not return any errors.

我希望我的查询不会返回任何错误。

#1


2  

UPDATE:

更新:

The recommended way is to use parameters. Using a parameterized query not only steps around SQL injection issues, but solves SQL Server date localization issues as well.

推荐的方法是使用参数。使用参数化查询不仅可以解决SQL注入问题,还可以解决SQL Server日期本地化问题。

SqlCommand cmd = new SqlCommand(@"INSERT INTO Users ([UserName], [UserPwd], [IsActive], [ModBy], [ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=', 'true', '1', @dateValue)");
cmd.Parameters.AddWithValue("@dateValue", DateTime.Now.Date);
.......
// do same for the other.

You have 2 parentheses after VALUES in your code

代码中的值后面有两个圆括号

Check this :

检查:

string format = "yyyy-mm-dd"; //or with date yyyy-mm-dd hh:mm:ss
cmmd.CommandText = string.Format(@"INSERT INTO Users ([UserName], [UserPwd], [IsActive], [ModBy], [ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=', 'true', '1', '{0}')", DateTime.Now.Date.ToString(format));

Also you should use parameters.

您还应该使用参数。

#2


5  

First of all check for the DataTypes of each Fields. And make sure that you have used appropriate DataTypes as per the tables. and if everythings ok then just try below query.

首先检查每个字段的数据类型。确保您已经使用了适当的数据类型。如果一切顺利,那么试试下面的查询。

cmmd.CommandText = @"INSERT INTO Users ([UserName], [UserPwd], [IsActive], 
            [ModBy],[ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=',
            'true', '1', 'Datetime.Now.Date')";
cmmd.ExecuteNonQuery();

#3


4  

There are two errors in your query first is double bracket and second one is DateTime.Now.Date. it is a .Net feature that you cannot execute it in string. And here you don't need to add @ at the beginning of the string. Also change 'True' to Yes

查询中有两个错误,第一个是双括号,第二个是DateTime.Now.Date。它是一个。net特性,你不能在字符串中执行它。这里不需要在字符串的开头添加@。也可以将“True”改为“Yes”

try this

试试这个

cmmd.CommandText = "INSERT INTO Users ([UserName], [UserPwd], [IsActive], 
            [ModBy],[ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=',
            Yes, '1', Date())";
cmmd.ExecuteNonQuery();

#4


2  

You have a typo:

你有一个错误:

cmmd.CommandText = @"INSERT INTO Users ([UserName], [UserPwd], [IsActive], [ModBy], [ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=', 'true', '1', 'Datetime.Now.Date')";

One parenthesis, not two.

一个括号,而不是两个。

#5


2  

With DateTime.Now.Date, are u trying to get the date?

DateTime.Now。约会,你想约会吗?

Try this:

试试这个:

cmmd.CommandText = @"INSERT INTO Users ([UserName], [UserPwd], [IsActive], [ModBy], [ModDate]) VALUES ('Admin', 'kov1ozyKAjas8awoej3oijhrqoi6q=', 'true', '1', Datetime.Now.Date)";
cmmd.ExecuteNonQuery();   

#6


1  

Try this, The datetime is a string if you put inside of the double quote or single quote, So you need the query look like.

试试这个,datetime是一个字符串,如果你把它放在双引号或单引号里面,所以你需要查询看起来像这样。

cmmd.CommandText = @"INSERT INTO Users ([UserName], [UserPwd], [IsActive], 
            [ModBy],[ModDate])VALUES('Admin','kov1ozyKAjas8awoej3oijhrqoi6q=',
            'true', '1', '" + DateTime.Now.Date + "')";
                    cmmd.ExecuteNonQuery();

I hopes my query is does not return any errors.

我希望我的查询不会返回任何错误。