c# 将字段写入数组,并添加到access数据库时提示:“未将对象引用设置到对象的实例。第102行出错

2024-12-20 03:11:09
推荐回答(2个)
回答1:

object o=null;
using (OleDbConnection con = new OleDbConnection(connectionStr))
{
string query = string.Format("select top 1 STUname from form1 where username = '{0}',username);
using(OleDbCommand cmd = new OleDbCommand(query,con)
{
con.Open();
object o = cmd.ExecuteScalar();//根据你的STUname
数据类型,再将 o 转换成相应的类型,比如 是字符串 就可以 string s = o.ToString();
}

回答2:

ToString之前先判断是否null