我收到此错误(并非所有代码路径返回值).我想用唯一键约束在我的数据库中插入数据.但是当我在我的代码中添加它时,我的方法给了我这个错误.
这是我的代码
public string Insert()
{
SqlConnection Conn = new SqlConnection(@"Data Source=ZARAK\SQLEXPRESS;Initial Catalog=ProjectDAL;integrated security=true");
try
{
Conn.Open();
SqlCommand cmd = new SqlCommand("Insert INTO tbl_User(Name,Email,Password) VALUES ('" + name + "','" + email + "','" + password + "')", Conn);
int restl = cmd.ExecuteNonQuery();
//temp = true;
return "Record Inserted successfully!";
}
catch (SqlException ex)
{
if (ex.Number == 2627)
{
return "Record Already Exists";
}
}
finally
{
Conn.Close();
}
}
Run Code Online (Sandbox Code Playgroud)