Ideally you would just make TagID an identity field by changing the table definition. If you can't do that, next best would be:
CREATE PROCEDURE [dbo].[InsertTagProcdure]
@Value nvarchar(200),
@TagCount nvarchar(200)
AS
BEGIN
BEGIN TRANSACTION
DECLARE @TagID int;
SELECT @TagID = coalesce((select max(TagID) + 1 from Tag), 1)
COMMIT
INSERT INTO
Tag
(TagID,Value,TagCount)
VALUES
(@TagID,@Value,@TagCount)
END
The transaction ensures that you don't end up with unique TagIDs and the coalesce handles the special case where the table is empty and gives an initial value of 1.
EDIT:
Based on the change to your original question, the table already has an identity column so your stored procedure should be:
CREATE PROCEDURE [dbo].[InsertTagProcdure]
@Value nvarchar(200),
@TagCount nvarchar(200)
AS
BEGIN
INSERT INTO Tag (Value,TagCount) VALUES (@Value,@TagCount)
END
and your C# code should be
int TagID = int.Parse(txtTagID.Text); //This should fall away so auto increment.
String Value = txtValue.Text;
int TagCount = int.Parse(txtCount.Text);
using (var conn = new SqlConnection(Properties.Settings.Default.DBConnectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "InsertTagProcdure";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Value", Value);
cmd.Parameters.AddWithValue("@TagCount", TagCount);
cmd.ExecuteNonQuery();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…