问个C#的DATAROW问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 21:50:13
private void student_update_Load(object sender, EventArgs e)
{
OleDbConnection thisConnection = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;
Data Source = C:\Users\hp\Desktop\TEMP\论文\论文.mdb"); //连接数据库
OleDbDataAdapter thisAdapter = new OleDbDataAdapter("SELECT * FROM student", thisConnection);
OleDbCommandBuilder thisBuilder = new OleDbCommandBuilder(thisAdapter);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "student");
DataColumn[] keys = new DataColumn[1];
keys[0] = thisDataSet.Tables["student"].Columns["学号"];
thisDataSet.Tables["student"].PrimaryKey = keys;
DataRow findRow = thisDataSet.Tables["student"].Rows.Find(学生成绩管理系统.account.accounts.ToString());
if (findRow != n

你不是用的findRow这个变量吗?怎么又变成thisRow了? 这个thisRow是New出来的,里面是没有值的吧。

DataRow findRow = thisDataSet.Tables["student"].Rows[0];
if (findRow != null)
{

textBoxStu_Num.Text = findRow[“学号”].ToString();

textBoxSex.Text = findRow["性别"].ToString();
textBoxClass.Text = findRow["班级"].ToString();
textBoxAdd.Text = findRow["家庭住址"].ToString();
textBoxTel.Text = findRow["联系电话"].ToString();
}
else MessageBox.Show("测试", "错误");
thisConnection.Close();

//如果没有东西,说明没有查询到记录。

可能是这个原因,因为你的数据库数据是空的,然后你使用它的方法。一个空对象,它是没有任何方法可以用的。你用了一个TOSTRING()方法,所以报异常,你在赋值前先作一下判断,看是否有值,有值再取,没有值的话,你根据自己业务来给一个默认值或者是不处理,就这样,希望对你有用。

看错