对于mycommand.ExecuteNonQuery()的一个或多个必需参数,没有给出任何值... 我想知道问题是什么...... 愿任何人帮忙吗? thx =)
System.Data.OleDb.OleDbConnection cnregister;
System.Data.OleDb.OleDbCommand cnUpreg;
System.Data.OleDb.OleDbDataReader ReaderReg;
private void cmdregister_Click(object sender, EventArgs e)
{
cnregister = new System.Data.OleDb.OleDbConnection();
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=|DataDirectory|Register.mdb";
OleDbConnection myConnection = new OleDbConnection(connectionString);
string InputId_reg;
string InputPass_reg;
InputId_reg = txtuserid_reg.Text;
InputPass_reg = txtpass_reg.Text;
myConnection.Open();
OleDbCommand cnUpreg = new OleDbCommand("SELECT * FROM tblRegister", myConnection);
ReaderReg = cnUpreg.ExecuteReader(); //reader open
while (ReaderReg.Read())
{
if (InputId_reg == (ReaderReg["UserID"].ToString()) )
{ //to check whether the UserID is same with the ID in the database
// if yes, a message box will promt
MessageBox.Show("The user had register");
txtuserid_reg.Focus();
txtuserid_reg.Clear ();
ReaderReg.Close();
myConnection.Close();
break;
}
else
{
string query123 = "INSERT INTO [tblRegister] ([UserID], [Password], [UserName], [UserJob]) VALUES(add1, add2, add3, add4)";
OleDbCommand mycommand = new OleDbCommand(query123, myConnection);
mycommand.Parameters.AddWithValue("add1", txtuserid_reg.Text);
mycommand.Parameters.AddWithValue("add2", txtpass_reg.Text);
mycommand.ExecuteNonQuery();
ReaderReg.Close();
myConnection.Close();
MessageBox.Show("Data save successfully!");
break;
}
}
MessageBox.Show("Break succesffully");
}
}
答案 0 :(得分:7)
您在查询中要求4个参数,但只分配2:
string query123 = "INSERT INTO [tblRegister] ([UserID], [Password], [UserName], [UserJob]) VALUES(add1, add2, add3, add4)";
OleDbCommand mycommand = new OleDbCommand(query123, myConnection);
mycommand.Parameters.AddWithValue("add1", txtuserid_reg.Text);
mycommand.Parameters.AddWithValue("add2", txtpass_reg.Text);
// Need UserName and UserJob to satisfy your insert query
mycommand.ExecuteNonQuery();
...
我不确定您的应用程序是如何设置的,但您需要添加如下内容:
mycommand.Parameters.AddWithValue("add3", txtusername_reg.Text);
mycommand.Parameters.AddWithValue("add4", txtuserjob_reg.Text);
或者将您的查询更改为:
string query123 = "INSERT INTO [tblRegister] ([UserID], [Password]) VALUES(add1, add2)";
答案 1 :(得分:2)
我的猜测是这一行用4个“参数”设置查询
string query123 = "INSERT INTO [tblRegister] ([UserID], [Password], [UserName], [UserJob]) VALUES(add1, add2, add3, add4)";
...但是你只能在ExecuteNonQuery
之前添加其中两个