首先,我允许用户输入组合框,以便在有很多项目时快速搜索。我想知道如果用户不小心按下另一个字母并且它将导致null,我是否可以停止插入查询,并且我有sscanf()
阻止但它只显示错误消息但主键跳过一个数字。
选择ProjectID以填充comboBox
try catch
插入数据库:
private void GetProjectID()
{
newConnection.ConnectionM();
SqlCommand cmd = SqlConnectionOLTP.cn.CreateCommand();
cmd.CommandText = "Select * from Project where ProjectName='" + comboBoxProjectTitle.Text + "' ";
try
{
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
ProjectID = reader.GetInt32(0);
}
reader.Close();
SqlConnectionOLTP.cn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:0)
SET IDENTITY_INSERT <TableName> ON
-- Do the inserting in the table with name <TableName>
SET IDENTITY_INSERT <TableName> OFF
答案 1 :(得分:0)
在SSMS上运行此代码,即sql server management studio
ALTER TABLE [Project] DROP ProjectID
SET IDENTITY_INSERT <Project> OFF