Sql Server CE - 临时禁用特定列上的自动增量

发布于 2024-08-29 21:35:36 字数 2064 浏览 3 评论 0原文

我有一个小问题,这个问题已经在我脑海里萦绕了一段时间了。

是这样的:

是否可以暂时禁用列 ID 上的 Auto_Increment。

这样我就可以向表中添加新行,并在插入行时指定 ID 值。

然后最后再次启用 Auto_Increment,并让它照常工作?

如果可能的话我该怎么做?

表结构很简单

列名(属性)

ID (Primary Key, Auto Increment, int, not null)
Name (nvarchar(100), not null)

注:

  • 表名是:People
  • 我们还考虑该表已经有数据并且无法更改。
  • 数据库服务器是SQL Server CE。
  • 如果有帮助的话,SQL 命令将在 C# 程序中执行。

我真的希望它是可能的,它会非常方便。

谢谢

编辑

SqlActions SqlActions = new SqlActions();

SqlCeCommand SqlCmd = new SqlCeCommand("SET IDENTITY_INSERT People ON", SqlActions.Connection());

try
{
    SqlCmd.ExecuteNonQuery();
}
catch (SqlCeException Error)
{
    Console.WriteLine(Error.ToString());
}

string query = "INSERT INTO People SET (ID, Nome) VALUES (@ID, @Nome)";

SqlCeCommand SqlInsert = new SqlCeCommand(query, SqlActions.Connection());

SqlInsert.Parameters.AddWithValue("@ID", 15);
SqlInsert.Parameters.AddWithValue("@Nome", "Maria");

try
{
    SqlInsert.ExecuteNonQuery();
}
catch (SqlCeException Error)
{
    Console.WriteLine(Error.ToString());
}

连接字符串正在工作,我已经尝试过。

他报告说:

解析查询时出错。 [ 令牌行号 = 1,令牌行 抵消 = 20,令牌错误 = SET ]

解决方案 感谢 OrbMan >

        SqlActions SqlActions = new SqlActions();

        SqlCeCommand SqlCmd = new SqlCeCommand("SET IDENTITY_INSERT People ON", SqlActions.Connection());
        try
        {
            SqlCmd.ExecuteNonQuery();

            string query = "INSERT INTO People (ID, Nome) VALUES (@ID, @Nome)";
            SqlCmd.CommandText = query;
            SqlCmd.Parameters.AddWithValue("@ID", 15);
            SqlCmd.Parameters.AddWithValue("@Nome", "Vania");
            SqlCmd.ExecuteNonQuery();
        }
        catch (SqlCeException Error)
        {
            Console.WriteLine(Error.ToString());
        }

I have this little question that's been on my mind for a while now.

Here it goes:

Is it possible to temporary disable the Auto_Increment on the column ID.

So that I can add a new row to the table and being able specify the ID value when inserting the row.

And then in the end enable the Auto_Increment again, and let do its work as usual?

And if its possible how can I do it?

The table structure is very simple

Column name (attributes)

ID (Primary Key, Auto Increment, int, not null)
Name (nvarchar(100), not null)

Note:

  • The table name is: People.
  • Let's also consider that the table already has data and cannot be changed.
  • The database server is SQL Server CE.
  • The SQL commands will be executed in a C# program, if it's of any help.

I really hope its possible, it would come very handy.

Thanks

EDIT

SqlActions SqlActions = new SqlActions();

SqlCeCommand SqlCmd = new SqlCeCommand("SET IDENTITY_INSERT People ON", SqlActions.Connection());

try
{
    SqlCmd.ExecuteNonQuery();
}
catch (SqlCeException Error)
{
    Console.WriteLine(Error.ToString());
}

string query = "INSERT INTO People SET (ID, Nome) VALUES (@ID, @Nome)";

SqlCeCommand SqlInsert = new SqlCeCommand(query, SqlActions.Connection());

SqlInsert.Parameters.AddWithValue("@ID", 15);
SqlInsert.Parameters.AddWithValue("@Nome", "Maria");

try
{
    SqlInsert.ExecuteNonQuery();
}
catch (SqlCeException Error)
{
    Console.WriteLine(Error.ToString());
}

The connection string is working, I have tried it.

He reports:

There was an error parsing the query.
[ Token line number = 1,Token line
offset
= 20,Token in error = SET ]

SOLUTION thanks to OrbMan

        SqlActions SqlActions = new SqlActions();

        SqlCeCommand SqlCmd = new SqlCeCommand("SET IDENTITY_INSERT People ON", SqlActions.Connection());
        try
        {
            SqlCmd.ExecuteNonQuery();

            string query = "INSERT INTO People (ID, Nome) VALUES (@ID, @Nome)";
            SqlCmd.CommandText = query;
            SqlCmd.Parameters.AddWithValue("@ID", 15);
            SqlCmd.Parameters.AddWithValue("@Nome", "Vania");
            SqlCmd.ExecuteNonQuery();
        }
        catch (SqlCeException Error)
        {
            Console.WriteLine(Error.ToString());
        }

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

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

发布评论

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

评论(1

我的鱼塘能养鲲 2024-09-05 21:35:36

我相信您可以使用 SET IDENTITY_INSERT 。我不确定这是否适用于所有版本。

更新 2:

尝试此版本:

SqlActions SqlActions = new SqlActions();
SqlCeCommand SqlCmd = new SqlCeCommand("SET IDENTITY_INSERT People ON", SqlActions.Connection());
try
{
    SqlCmd.ExecuteNonQuery();
    string query = "INSERT INTO People (ID, Nome) VALUES (@ID, @Nome)";
    SqlCmd.CommandText = query;
    SqlCmd.Parameters.AddWithValue("@ID", 15);
    SqlCmd.Parameters.AddWithValue("@Nome", "Maria");
    SqlCmd.ExecuteNonQuery();
}
catch (SqlCeException Error)
{
    Console.WriteLine(Error.ToString());
}

I believe you can use SET IDENTITY_INSERT. I am not sure if this works in all versions.

Update 2:

Try this version:

SqlActions SqlActions = new SqlActions();
SqlCeCommand SqlCmd = new SqlCeCommand("SET IDENTITY_INSERT People ON", SqlActions.Connection());
try
{
    SqlCmd.ExecuteNonQuery();
    string query = "INSERT INTO People (ID, Nome) VALUES (@ID, @Nome)";
    SqlCmd.CommandText = query;
    SqlCmd.Parameters.AddWithValue("@ID", 15);
    SqlCmd.Parameters.AddWithValue("@Nome", "Maria");
    SqlCmd.ExecuteNonQuery();
}
catch (SqlCeException Error)
{
    Console.WriteLine(Error.ToString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文