我是编程新手,尤其是在使用 sql server 方面。我有一个带有文本框和提交按钮的用户页面。当用户在文本框中输入信息并按下按钮时,信息将被添加到数据库中。数据库中有一个 UserID 列,应在将所有信息添加到数据库后创建。UserID 应由 UserLastName 和 AutoIncrementNumber(自动分配给新行的数据库的列)组成我有此 UserID 的功能列,我不知道如何使一切正常。请帮我。谢谢!
这是代码:
using (SqlConnection conn = new SqlConnection(@"Data Source=MyDataBase;Initial Catalog=MyDataBase;Integrated Security=True;"))
SqlCommand CmdSql = new SqlCommand
("INSERT INTO [tbluser] ([UserID],[UserLastName], [UserFirstName], [UserMiddleInitial] SELECT [dbo].[usernamehandle](@UserFirstName),@UserLastName, @UserFirstName, @UserMiddleInitial", conn);
conn.Open();
CmdSql.Parameters.AddWithValue("@UserLastName", txtNewUserLN.Text.ToString());
CmdSql.Parameters.AddWithValue("@UserFirstName", txtNewUserFN.Text.ToString());
CmdSql.Parameters.AddWithValue("@UserMiddleInitial", txtNewUserMI.Text.ToString());
CmdSql.Connection = conn;
CmdSql.ExecuteNonQuery();
conn.Close();
这是功能:
USE [MyDataBase]
GO
/****** Object: UserDefinedFunction [dbo].[usernamehandle] Script Date: 04/07/2013 17:25:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[usernamehandle]
(@newuserName nchar(10))returns nchar(10)
AS
BEGIN
DECLARE @s2 nchar(256)
SET @s2 = ''
DECLARE @p int
DECLARE @ULN char(256)
DECLARE @UNAI int
SELECT @ULN = UserLastName FROM tblUser
SELECT @UNAI = UserNameAutoIncre FROM tbluser
SET @s2 = @UNAI
while @p <=6 begin
DECLARE @c int
SET @c = ascii(substring(@newUserName, @p, 1))
SET @s2 += char(@c)
end
return @s2
end