如何在MessageBox()中显示从MS Access中选择的数值

本文关键字:Access MS 选择 显示 MessageBox | 更新日期: 2023-09-27 18:10:23

为什么我的代码显示消息

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

我代码:

connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string cq = "select sum(Fine) from studentbook where S_ID=" + textSID.Text + "";
command.CommandText = cq;
int a = Convert.ToInt32(command.ExecuteReader());
connection.Close();
MessageBox.Show(a.ToString());

如何在MessageBox()中显示从MS Access中选择的数值

像这样修改代码:

where S_ID = '" + textSID.Text + "'

也用command.ExecuteScalar()代替command.ExecuteReader():

int a = Convert.ToInt32(command.ExecuteScalar());
顺便说一下,你应该总是使用parameterized queries。这种字符串连接对SQL Injection开放。