这是我的数据库:
这是我的代码
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into StudentTable([StudentID], [Lastname], [Transferee]) values ('" +txtStudentID.Text+ "','" +txtLastname.Text +"',"' +chk.Transferee+ '")";
command.ExecuteNonQuery();
错误是数据类型不匹配条件表达式
如何将复选框添加到数据库检查中(而不是复选框的值)?
谢谢
关于代码的一些说明:
IDENTITY
该StudentID
列我还在您的代码中看到在insert语句的末尾有一个错字,您可以使用以下命令:
/*...*/ "',"' +chk.Transferee+ '")";
代替这个:
/*...*/ "'," + chk.Transferee.IsChecked + ")";
对此:
/*...*/ "','" + chk.Transferee.IsChecked + "')";
无论如何,这是SQL注入,并且chk.Transferee.IsChecked
是C#布尔值而不是Transact-SQL位。因此,我们可以转到下一个标题。
如果您在下面运行代码:
using System;
public class Program
{
public static void Main()
{
bool? yes = true;
Console.WriteLine("yes: {0}", yes);
bool? no = false;
Console.WriteLine("no: {0}", no);
bool? nothing = null;
Console.WriteLine("nothing: {0}", nothing);
}
}
它将打印以下内容:
yes: True
no: False
nothing:
您可以在此.NET小提琴上对其进行测试。
Transact-SQL对“ true”或“ false”使用一点。在Transact-SQL中,这分别是a1
和a 0
。如果错字是固定的,您愿意使用此代码分别是这样的:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', True)
对此:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', 'True')
这不是Transact-SQL的有效代码。因为布尔值true
和带值的字符串True
不是bit 1
。
旁:可为空的布尔值(仅当您使用WPF或必须插入时null
)
如果您使用的是WPF,则IsChecked
属性为nullabele,或者仅插入null
。您的代码将给出一个例外。这将是您的SQL查询:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', )
对此:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', '')
这当然是无效的。
正确陈述
正确的陈述必须是这样的:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', 1)
在此SQLfiddle中测试此代码。
Wikipedia谈到SQL注入:
SQL注入是一种代码注入技术,用于攻击数据驱动的应用程序,其中将恶意的SQL语句插入到输入字段中以执行(例如,将数据库内容转储给攻击者)。
您可以使用以下代码来代替您的代码:
command.CommandText = @"insert into StudentTable([StudentID], [Lastname], [Transferee])
values (@StudentID, @LastName,@Transferee)";
command.Parameters.AddWithValue("@StudentID", txtStudentID.Text);
command.Parameters.AddWithValue("@LastName", txtLastname.Text);
command.Parameters.AddWithValue("@Transferee", chk.Transferee.IsChecked);
// @ClintJoe: Will you check code of line above I think it's not correct.
// must it not to be this: `chk.IsChecked`? Not sure.
如果您使用上面的代码并解决了我添加的问题,则第一个问题(代码中的键入错误),第二个标题(C#布尔值≠Transact-SQL位)以及所有问题都会被解决。
这也将防止SQL注入。但是,为什么不进行SQL注入呢?看到这个卡通:
提示:还要使列StudentID
标识。
IDENTITY
在表中创建一个标识列。该属性与CREATE TABLE
和ALTER TABLE
Transact-SQL语句一起使用。
完成此操作后,您可以使用以下代码:
command.CommandText = @"insert into StudentTable([Lastname], [Transferee])
values (@LastName, @Transferee)";
command.Parameters.AddWithValue("@LastName", txtLastname.Text);
command.Parameters.AddWithValue("@Transferee", chk.Transferee.IsChecked);
并且不再需要在应用程序中请求或创建唯一的ID
MS-access不是一个很好的数据库。通过Google的快速搜索,我发现Access的主要缺点:
- Windows和Office版本取决于。
- Access没有触发器和高级功能。
- Access VBA是一种解释性语言。因此,它很慢。
- 不存在用于性能分析和优化数据库的访问工具。
- 如果您有5-10个以上的并发用户,即使在拆分(前端/后端)数据库中,访问也会变得非常慢。
- 当访问文件太大(每mdb> 100MB)时,它们很容易损坏。
- 即使在拆分数据库上,Access始终会计算客户端的所有内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句