Sql Server CE - 临时禁用特定列上的自动增量
我有一个小问题,这个问题已经在我脑海里萦绕了一段时间了。
是这样的:
是否可以暂时禁用列 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您可以使用
SET IDENTITY_INSERT
。我不确定这是否适用于所有版本。更新 2:
尝试此版本:
I believe you can use
SET IDENTITY_INSERT
. I am not sure if this works in all versions.Update 2:
Try this version: