如何向用户显示该名称在 sql 中不可用

发布于 2025-01-11 12:37:41 字数 2588 浏览 0 评论 0原文

用户将创建一个角色并为该角色命名。我已修复,因此如果名称存在,它不会插入数据,但现在我想知道如何向用户显示该名称不可用。该代码将返回 1,因为它没有失败。以下是我的代码:

public int InsertAddventurer(Character Ad, int ClassId, out string errormsg)
{
    SqlConnection dbConnection = new SqlConnection();

    dbConnection.ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Adventurer;Integrated Security=True";

    String sqlstring = "" +
        "BEGIN " +
        "DECLARE @Id int; " +
        "IF NOT EXISTS(SELECT Ad_Fornamn FROM [Tbl_Adventurer] WHERE Ad_Fornamn = @name) " +
        "BEGIN " +
        "INSERT INTO[Tbl_Adventurer] " +
        "(Ad_Fornamn, Ad_Efternamn, Ad_Age, Ad_Rank) " +
        "Values(@Ad_Fornamn, @Ad_Efternamn, @Ad_Age, @Ad_Rank) " +
        "SELECT @Id = IDENT_CURRENT('Tbl_Adventurer'); " +
        "INSERT INTO[Tbl_Stats] " +
        "(Stats_Id, Strength, Dexterity, Constitution, Intelligence, Wisdom, Charisma) " +
        "Values(@Id, @Strength, @Dexterity, @Constitution, @Intelligence, @Wisdom, @Charisma) " +
        "INSERT INTO[Tbl_AdventurerKlass] " +
        "(Ad_Id, Kl_Id) " +
        "Values(@Id, @ClassId) " +
        "END " +
        "END";
    SqlCommand dbCommand = new SqlCommand(sqlstring, dbConnection);

    dbCommand.Parameters.Add("name", SqlDbType.VarChar, 30).Value = Ad.Name;
    dbCommand.Parameters.Add("Ad_Fornamn", SqlDbType.VarChar, 30).Value = Ad.Name;
    dbCommand.Parameters.Add("Ad_Efternamn", SqlDbType.VarChar, 30).Value = Ad.Lastname;
    dbCommand.Parameters.Add("Ad_Age", SqlDbType.Int).Value = Ad.Age;
    dbCommand.Parameters.Add("Ad_Rank", SqlDbType.Int).Value = Ad.Rank;
    dbCommand.Parameters.Add("Strength", SqlDbType.Int).Value = Ad.Strength;
    dbCommand.Parameters.Add("Dexterity", SqlDbType.Int).Value = Ad.Dexterity;
    dbCommand.Parameters.Add("Constitution", SqlDbType.Int).Value = Ad.Constitution;
    dbCommand.Parameters.Add("Intelligence", SqlDbType.Int).Value = Ad.Intelligence;
    dbCommand.Parameters.Add("Wisdom", SqlDbType.Int).Value = Ad.Wisdom;
    dbCommand.Parameters.Add("Charisma", SqlDbType.Int).Value = Ad.Charisma;
    dbCommand.Parameters.Add("ClassId", SqlDbType.Int).Value = ClassId;

    try
    {
        dbConnection.Open();
        int i = 0;
        i = dbCommand.ExecuteNonQuery();
        if (i == 1)
        {
            errormsg = "";
        }
        else
        {
            errormsg = "No Adventurer was created";
        }
        return i;
    }
    catch (Exception ex)
    {
        errormsg = ex.Message;
        return 0;
    }
    finally
    {
        dbConnection.Close();
    }
}

The user is going to make a character and give a name on the caracter. I have fixed so it will not insert data if the name exists but now I want to know how to show the user that the name is not available. The code will return 1 because it's not failing. The following is my code:

public int InsertAddventurer(Character Ad, int ClassId, out string errormsg)
{
    SqlConnection dbConnection = new SqlConnection();

    dbConnection.ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Adventurer;Integrated Security=True";

    String sqlstring = "" +
        "BEGIN " +
        "DECLARE @Id int; " +
        "IF NOT EXISTS(SELECT Ad_Fornamn FROM [Tbl_Adventurer] WHERE Ad_Fornamn = @name) " +
        "BEGIN " +
        "INSERT INTO[Tbl_Adventurer] " +
        "(Ad_Fornamn, Ad_Efternamn, Ad_Age, Ad_Rank) " +
        "Values(@Ad_Fornamn, @Ad_Efternamn, @Ad_Age, @Ad_Rank) " +
        "SELECT @Id = IDENT_CURRENT('Tbl_Adventurer'); " +
        "INSERT INTO[Tbl_Stats] " +
        "(Stats_Id, Strength, Dexterity, Constitution, Intelligence, Wisdom, Charisma) " +
        "Values(@Id, @Strength, @Dexterity, @Constitution, @Intelligence, @Wisdom, @Charisma) " +
        "INSERT INTO[Tbl_AdventurerKlass] " +
        "(Ad_Id, Kl_Id) " +
        "Values(@Id, @ClassId) " +
        "END " +
        "END";
    SqlCommand dbCommand = new SqlCommand(sqlstring, dbConnection);

    dbCommand.Parameters.Add("name", SqlDbType.VarChar, 30).Value = Ad.Name;
    dbCommand.Parameters.Add("Ad_Fornamn", SqlDbType.VarChar, 30).Value = Ad.Name;
    dbCommand.Parameters.Add("Ad_Efternamn", SqlDbType.VarChar, 30).Value = Ad.Lastname;
    dbCommand.Parameters.Add("Ad_Age", SqlDbType.Int).Value = Ad.Age;
    dbCommand.Parameters.Add("Ad_Rank", SqlDbType.Int).Value = Ad.Rank;
    dbCommand.Parameters.Add("Strength", SqlDbType.Int).Value = Ad.Strength;
    dbCommand.Parameters.Add("Dexterity", SqlDbType.Int).Value = Ad.Dexterity;
    dbCommand.Parameters.Add("Constitution", SqlDbType.Int).Value = Ad.Constitution;
    dbCommand.Parameters.Add("Intelligence", SqlDbType.Int).Value = Ad.Intelligence;
    dbCommand.Parameters.Add("Wisdom", SqlDbType.Int).Value = Ad.Wisdom;
    dbCommand.Parameters.Add("Charisma", SqlDbType.Int).Value = Ad.Charisma;
    dbCommand.Parameters.Add("ClassId", SqlDbType.Int).Value = ClassId;

    try
    {
        dbConnection.Open();
        int i = 0;
        i = dbCommand.ExecuteNonQuery();
        if (i == 1)
        {
            errormsg = "";
        }
        else
        {
            errormsg = "No Adventurer was created";
        }
        return i;
    }
    catch (Exception ex)
    {
        errormsg = ex.Message;
        return 0;
    }
    finally
    {
        dbConnection.Close();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒带 2025-01-18 12:37:41

您不需要更改大量当前代码,只需在结束查询之前选择内部变量 @Id 并使用 ExecuteScalar 执行查询

String sqlstring = @
"BEGIN 
     DECLARE @Id int = 0; 
     IF NOT EXISTS(SELECT Ad_Fornamn FROM [Tbl_Adventurer] WHERE Ad_Fornamn = @name) 
     BEGIN
        INSERT INTO[Tbl_Adventurer]
               (Ad_Fornamn, Ad_Efternamn, Ad_Age, Ad_Rank) 
                Values(@Ad_Fornamn, @Ad_Efternamn, @Ad_Age, @Ad_Rank)
        SELECT @Id = IDENT_CURRENT('Tbl_Adventurer')
        INSERT INTO[Tbl_Stats] 
               (Stats_Id, Strength, Dexterity, Constitution, Intelligence, Wisdom, Charisma) 
                Values(@Id, @Strength, @Dexterity, @Constitution, @Intelligence, @Wisdom, @Charisma) 
        INSERT INTO[Tbl_AdventurerKlass]
                    (Ad_Id, Kl_Id) 
                    Values(@Id, @ClassId)
    END 
SELECT @Id
END;";

注意最后的 SELECT 和(要清楚)@Id 的初始化为 0多变的。当您使用时,@Id将返回到您的调用代码中
执行标量。

int result = (int)dbCommand.ExecuteScalar();
if (i > 0)
{
    errormsg = "";
}
else
{
    errormsg = "No Adventurer was created";
}
return result;

You don't need to change a lot of your current code, just, select the internal variable @Id before ending the query and execute the query with ExecuteScalar

String sqlstring = @
"BEGIN 
     DECLARE @Id int = 0; 
     IF NOT EXISTS(SELECT Ad_Fornamn FROM [Tbl_Adventurer] WHERE Ad_Fornamn = @name) 
     BEGIN
        INSERT INTO[Tbl_Adventurer]
               (Ad_Fornamn, Ad_Efternamn, Ad_Age, Ad_Rank) 
                Values(@Ad_Fornamn, @Ad_Efternamn, @Ad_Age, @Ad_Rank)
        SELECT @Id = IDENT_CURRENT('Tbl_Adventurer')
        INSERT INTO[Tbl_Stats] 
               (Stats_Id, Strength, Dexterity, Constitution, Intelligence, Wisdom, Charisma) 
                Values(@Id, @Strength, @Dexterity, @Constitution, @Intelligence, @Wisdom, @Charisma) 
        INSERT INTO[Tbl_AdventurerKlass]
                    (Ad_Id, Kl_Id) 
                    Values(@Id, @ClassId)
    END 
SELECT @Id
END;";

Note the final SELECT and (to be clear) the initialization to 0 for the @Id variable. The @Id will be returned to your calling code when you use
ExecuteScalar.

int result = (int)dbCommand.ExecuteScalar();
if (i > 0)
{
    errormsg = "";
}
else
{
    errormsg = "No Adventurer was created";
}
return result;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文