小编Zar*_*ood的帖子

错误:并非所有代码路径都返回值?

我收到此错误(并非所有代码路径返回值).我想用唯一键约束在我的数据库中插入数据.但是当我在我的代码中添加它时,我的方法给了我这个错误.

这是我的代码

  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)

c# sql sql-server-2012

3
推荐指数
1
解决办法
107
查看次数

标签 统计

c# ×1

sql ×1

sql-server-2012 ×1