执行添加时,出项这样的异常,这是怎么回事啊?
#region 执行SqlCommand对象/// <summary>
/// 执行SqlCommand对象
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns></returns>
public int GetComm(string sql)
{
int res;
SqlConnection conn = GetConn();
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
res = cmd.ExecuteNonQuery();
conn.Close();
return res;
}
#endregion
#region 插入新纪录
/// <summary>
/// 插入一条新纪录
/// </summary>
/// <param name="name">姓名</param>
/// <param name="pwd">密码</param>
/// <returns>成功返回true,失败返回false</returns>
public bool Insert(string name, string pwd)
{
string sql = "insert into Users(username,password) values(@name,@pwd)";
SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name", name), new SqlParameter("@pwd", pwd) };
SQLHelper sqlhelper = new SQLHelper();
int res;
res = sqlhelper.GetComm(sql);
if (res > 0)
{
return true;
}
else
{
return false;
}
#endregion
string name = this.textBox1.Text;
string pwd = this.textBox2.Text;
DBOperate dbo = new DBOperate();
bool flag = false;
flag = dbo.Insert(name, pwd);
if (flag)
{
MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("添加失败!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}