Refresh table column with default column value

System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection();
dataConnection.ConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;

System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand();
dataCommand.Connection = dataConnection;

long MachineGroupID = Convert.ToInt64(Request.QueryString["node"]);
dataCommand.CommandText = "UPDATE [MachineGroups] SET [MachineGroupName]=@MachineGroupName,[MachineGroupDesc]=@MachineGroupDesc WHERE [MachineGroupID]= @MachineGroupID";

//add our parameters to our command object  
dataCommand.Parameters.AddWithValue("@MachineGroupName", MachineGroupName);
dataCommand.Parameters.AddWithValue("@MachineGroupDesc", MachineGroupDesc);

dataCommand.Parameters.AddWithValue("@MachineGroupID", MachineGroupID);

dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();

here the MachineGroups table has a tval column that was first inserted with a custom value, and then when updating it should put the default value specified by the SQl value when creating ...

eg

[tval] [int] NOT NULL DEFAULT ((2147483647))

this is how the column was created, and now I want to put the value 2147483647 when updating.

Also, I cannot fix the value as "2147483647", which may change.

any suggestions

thank

+3
source share
4 answers

Have you tried to use a keyword DEFAULT? For example:

UPDATE [MachineGroups] SET [tval] = DEFAULT 
WHERE [MachineGroupID] = @MachineGroupID

SQL, docs UPDATE , Microsoft SQL Server DEFAULT.

+5

SQL- MS SQL, :

UPDATE [MachineGroups] 
SET [MachineGroupName]=@MachineGroupName
,[MachineGroupDesc]=@MachineGroupDesc 
,[tval]=DEFAULT
WHERE [MachineGroupID]= @MachineGroupID

SQL-, , .

+2

[tval] , , :

dataCommand.CommandText = "UPDATE [MachineGroups] SET [MachineGroupName]=@MachineGroupName,[MachineGroupDesc]=@MachineGroupDesc, [tval] = default** WHERE [MachineGroupID]= @MachineGroupID"; 

, .

+1
0

Source: https://habr.com/ru/post/1727682/


All Articles