Oledb를 데이터베이스로 사용하고 있는데 사용자가 탭을 눌러 탭을 할 때마다 "상태"열을 업데이트하려고합니다. 따라서 이렇게됩니다 ...C# 데이터베이스 업데이트
사용자가 탭하면 "IN", "OUT"또는 이전에 비었던 db의 "Status"열을 쿼리합니다. "IN"으로 쿼리되면 현재 상태가 "OUT"으로 설정되고 그 반대의 경우도 마찬가지입니다. 업데이트는 정확히 내가 원하는대로 작동합니다. "NullReferenceException 처리되지 않은"이라는 경고가 실행될 때마다이 행에 계속 나타나고 더 이상 계속 실행할 수 없습니다. str = cmd1.ExecuteScalar(). ToString();
저는 C#에 매우 익숙합니다. 누군가가이 문제를 어떻게 해결할 수 있는지 자세히 설명해 주시면 좋을 것 같습니다. 고맙습니다!
// prepare command string
string selectString = @"SELECT Status FROM jiahe where [Tag ID] = '" + textBox1.Text + "'";
// 1. Instantiate a new command with command text only
OleDbCommand cmd = new OleDbCommand(selectString, objConnection);
// 2. Set the Connection property
cmd.Connection.Open();
// 3. Call ExecuteNonQuery to send command
string str = cmd.ExecuteScalar().ToString();
cmd.Connection.Close();
if (str.Length == 2 || str.Length == 0)
{
//MessageBox.Show("Status: " + str);
// prepare command string
string updateString = @"update jiahe set Status = 'OUT' where [Tag ID] = '" + textBox1.Text + "'";
// 1. Instantiate a new command with command text only
OleDbCommand cmd1 = new OleDbCommand(updateString, objConnection);
// 2. Set the Connection property
cmd1.Connection.Open();
// 3. Call ExecuteNonQuery to send command
str = cmd1.ExecuteScalar().ToString();
MessageBox.Show("Status: OUT");
cmd1.Connection.Close();
}
else
{
//string str1 = cmd2.ExecuteScalar().ToString();
//MessageBox.Show("Status: " + str);
// prepare command string
string updateString = @"update jiahe set Status = 'IN' where [Tag ID] = '" + textBox1.Text + "'";
// 1. Instantiate a new command with command text only
OleDbCommand cmd1 = new OleDbCommand(updateString, objConnection);
// 2. Set the Connection property
cmd1.Connection.Open();
// 3. Call ExecuteNonQuery to send command
//string str1 = cmd2.ExecuteScalar().ToString();
str = cmd1.ExecuteScalar().ToString();
MessageBox.Show("Status: IN");
cmd1.Connection.Close();
}
}
왜 ExecuteScalar를 업데이트 용으로 호출하고 있습니까? –