我想通過C#將數據插入到我的Sql-Server數據庫中。我正在調用一個存儲過程,然後想要添加它。我不確定要做什麼更改,但最終我希望在存儲過程中完成這些更改。創建存儲過程添加與自動增量作爲其主要字段?
我的存儲過程現在:
CREATE PROCEDURE [dbo].[InsertTagProcdure]
@TagID int,
@Value nvarchar(200),
@TagCount nvarchar(200)
AS
IF NOT EXISTS (SELECT NULL FROM Tag
WHERE @TagID = @TagID)
BEGIN
INSERT INTO
Tag
(TagID,Value,TagCount)
VALUES
(@TagID,@Value,@TagCount)
END
而我的C#代碼:
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("@TagID", TagID);
cmd.Parameters.AddWithValue("@Value", Value);
cmd.Parameters.AddWithValue("@TagCount", TagCount);
cmd.ExecuteNonQuery();
}
表創建我用://不能改變這個公司什麼老闆給了我。
CREATE TABLE [dbo].[Tag](
[TagID] [int] IDENTITY(1,1) NOT NULL,
[Value] [varchar](200) NOT NULL,
[TagCount] [varchar](200) NULL,
CONSTRAINT [PK_Tag] PRIMARY KEY CLUSTERED
(
[TagID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
如果你打開自動增量在SQL Server的主鍵,你將不再需要通過標籤識別,它會自動遞增並分配給你。 – ThePower