如何在C#

时间:2019-06-07 09:38:14

标签: c# ms-access oledb oledbcommand

“ OleDbException”说问题是“ 表达式中的类型不匹配。”,但是我已经检查了数据库(访问权限)和组合框,两者均为字符串类型。 / p>

我尝试使用comboBox_CourseID.SelectedItem.ToString()将组合框制成字符串,然后更改为comboBox_CourseID.Text,但仍然是完全相同的错误。我也尝试检查Access中的列类型,但这是短文本。 我尝试使用文本框而不是组合框,但没有成功。

这是我的代码:

OleDbCommand show_table = new OleDbCommand
{
    Connection = connect,

    CommandText = "Select Student.StudentID, Student.First_Name, Student.Last_Name, Student.Contact_Number, Student.Email " +
    "From Student Inner Join Course on Course.CourseID = Student.CourseID " +
    "Where Course.CourseID = '" + comboBox_CourseID.SelectedItem.ToString() + "'"
};

OleDbDataReader dataReader =show_table.ExecuteReader();  
*//error here* 

if (dataReader.Read() == false)
    MessageBox.Show("Record Not Found.");

dataReader.Close();

OleDbDataAdapter displayCourseID = new OleDbDataAdapter(show_table);
DataTable table = new DataTable();
displayCourseID.Fill(table);
dataGridView_StudentInfo.DataSource = table;

我希望它用数据库(Access)中的数据作为表填充dataGridView。

注意:我将Student.CourseID设置为Access中的MultiValued,这会影响类型吗?

0 个答案:

没有答案