我对 C# 参数有疑问,
我得到这个错误:
方法“ExecuteInsert”没有过载需要“5”
这是我的代码:
private void ExecuteInsert(string FirstName, string LastName, string MiddleName, string UserName, string Password, string MemberStatus)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO Registration (FirstName, LastName, MiddleName, UserName, Password, MemberStatus, DateRegistration) VALUES"
+ " (@FirstName,@LastName,@MiddleName,@UserName,@Password,'ACTIVE',CURRENT_TIMESTAMP)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[5];
param[0] = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
param[2] = new SqlParameter("@MiddleName", SqlDbType.VarChar, 50);
param[3] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
param[4] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
param[5] = new SqlParameter("ACTIVE", SqlDbType.VarChar, 50);
param[6] = new SqlParameter("CURRENT_TIMESTAMP", SqlDbType.VarChar, 50);
param[0].Value = FirstName;
param[1].Value = LastName;
param[2].Value = MiddleName;
param[3].Value = UserName;
param[4].Value = Password;
param[5].Value = MemberStatus;
param[6].Value = DateRegistration;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TxtPassword.Text == TxtRePassword.Text)
{
//call the method to execute insert to the database
ExecuteInsert(TxtFName.Text,
TxtLName.Text,
TxtMName.Text,
TxtUserName.Text,
TxtPassword.Text);
Response.Write("Record was successfully added!");
ClearControls(Page);
}
else
{
Response.Write("Password did not match");
TxtPassword.Focus();
}
}
public static void ClearControls(Control Parent)
{
if (Parent is TextBox)
{ (Parent as TextBox).Text = string.Empty; }
else
{
foreach (Control c in Parent.Controls)
ClearControls(c);
}
}
}