如何将复选框 "check" 插入到我的 MS Access 数据库中
How to insert the checkbox "check" to my MS Access database
这是我的数据库:
这是我的代码
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into StudentTable([StudentID], [Lastname], [Transferee]) values ('" +txtStudentID.Text+ "','" +txtLastname.Text +"',"' +chk.Transferee+ '")";
command.ExecuteNonQuery();
错误是数据类型不匹配标准表达式
如何将复选框添加到我的数据库检查中(不是复选框的值)?
谢谢
我建议首先将您的查询更改为参数化查询,以避免 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", chkTransferee.Checked);
对您的代码的一些评论:
- 您的代码中有错别字(晚更新)
- C# 布尔值 ≠ Transact-SQL 位。
- 你进行SQL注射。
- 您可以对
StudentID
列使用 IDENTITY
您的代码中有错别字
我还在你的代码中看到你的插入语句末尾有一个拼写错误,你使用这个:
/*...*/ "',"' +chk.Transferee+ '")";
而不是这个:
/*...*/ "'," + chk.Transferee.IsChecked + ")";
xor 这个:
/*...*/ "','" + chk.Transferee.IsChecked + "')";
无论如何它是 SQL 注入并且 chk.Transferee.IsChecked
是 C# 布尔值而不是 Transact-SQL 位。所以我们可以转到下一个标题。
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 fiddle.
上进行测试
Transact-SQL 对 "true" 或 "false" 使用一点。在 Transact-SQL 中,这分别是一个 1
和一个 0
。如果拼写错误得到修复,您愿意使用此代码做什么,分别是:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', True)
xor 这个:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', 'True')
这不是 Transact-SQL 的有效代码。因为布尔值 true
和值为 True
的字符串不是位 1
.
旁白: 可为空的布尔值(仅当您使用 WPF 或必须插入 null
时)
如果您使用的是 WPF 是 IsChecked
property is nullabele,或者如果您只是插入一个 null
。您的代码将给出一个例外。这将是您的 SQL 查询:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', )
xor 这个:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', '')
这当然是无效的。
正确的说法
正确的说法应该是这样的:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', 1)
在此 SQLfiddle 中测试此代码。
防止SQL注入
维基百科关于 SQL injection 的说法:
SQL injection is a code injection technique, used to attack data-driven applications, in which nefarious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker).
您可以使用以下代码代替您的代码:
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# boolean ≠ Transact-SQL bit) and aside 就解决了.
这也将防止 SQL 注入。但是,为什么没有 SQL 注入?看这部漫画:
身份
提示: 也使列 StudentID
identity。
IDENTITY
creates an identity column in a table. This property is used with the CREATE TABLE
and ALTER TABLE
Transact-SQL statements.
完成此操作后,您可以使用此代码:
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 and Office version dependant.
- Access doesn’t have triggers and advanced functions.
- Access VBA is an interpreted language. As such it is slow.
- Access tools for performance analyzing and optimizing the database are non existent.
- Access becomes terribly slow if you have more than 5–10 concurrent users even in a split (front/back end) database.
- Access files are prone to corruption when they become too big (>100MB per mdb).
- Even on a split database Access always computes everything client-side.
Source: What are the major disadvantages of Microsoft Access? - Quora
这是我的数据库:
这是我的代码
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into StudentTable([StudentID], [Lastname], [Transferee]) values ('" +txtStudentID.Text+ "','" +txtLastname.Text +"',"' +chk.Transferee+ '")";
command.ExecuteNonQuery();
错误是数据类型不匹配标准表达式
如何将复选框添加到我的数据库检查中(不是复选框的值)?
谢谢
我建议首先将您的查询更改为参数化查询,以避免 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", chkTransferee.Checked);
对您的代码的一些评论:
- 您的代码中有错别字(晚更新)
- C# 布尔值 ≠ Transact-SQL 位。
- 你进行SQL注射。
- 您可以对
StudentID
列使用IDENTITY
您的代码中有错别字
我还在你的代码中看到你的插入语句末尾有一个拼写错误,你使用这个:
/*...*/ "',"' +chk.Transferee+ '")";
而不是这个:
/*...*/ "'," + chk.Transferee.IsChecked + ")";
xor 这个:
/*...*/ "','" + chk.Transferee.IsChecked + "')";
无论如何它是 SQL 注入并且 chk.Transferee.IsChecked
是 C# 布尔值而不是 Transact-SQL 位。所以我们可以转到下一个标题。
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 fiddle.
上进行测试Transact-SQL 对 "true" 或 "false" 使用一点。在 Transact-SQL 中,这分别是一个 1
和一个 0
。如果拼写错误得到修复,您愿意使用此代码做什么,分别是:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', True)
xor 这个:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', 'True')
这不是 Transact-SQL 的有效代码。因为布尔值 true
和值为 True
的字符串不是位 1
.
旁白: 可为空的布尔值(仅当您使用 WPF 或必须插入 null
时)
如果您使用的是 WPF 是 IsChecked
property is nullabele,或者如果您只是插入一个 null
。您的代码将给出一个例外。这将是您的 SQL 查询:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', )
xor 这个:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', '')
这当然是无效的。
正确的说法
正确的说法应该是这样的:
insert into StudentTable([StudentID], [Lastname], [Transferee])
values (7, 'Turner', 1)
在此 SQLfiddle 中测试此代码。
防止SQL注入
维基百科关于 SQL injection 的说法:
SQL injection is a code injection technique, used to attack data-driven applications, in which nefarious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker).
您可以使用以下代码代替您的代码:
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# boolean ≠ Transact-SQL bit) and aside 就解决了.
这也将防止 SQL 注入。但是,为什么没有 SQL 注入?看这部漫画:
身份
提示: 也使列 StudentID
identity。
IDENTITY
creates an identity column in a table. This property is used with theCREATE TABLE
andALTER TABLE
Transact-SQL statements.
完成此操作后,您可以使用此代码:
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 and Office version dependant.
- Access doesn’t have triggers and advanced functions.
- Access VBA is an interpreted language. As such it is slow.
- Access tools for performance analyzing and optimizing the database are non existent.
- Access becomes terribly slow if you have more than 5–10 concurrent users even in a split (front/back end) database.
- Access files are prone to corruption when they become too big (>100MB per mdb).
- Even on a split database Access always computes everything client-side.
Source: What are the major disadvantages of Microsoft Access? - Quora