2014-11-24 254 views
-1

我一直在为医院管理创建C#项目,其中我已经考虑了一些基本的文本值,如患者姓名,PatientID,Gender等,以及一些用于功能保存等的按钮。我已经使用SqlClient来存储值。我只是想知道如何设置PatientID,以便每次输入新记录时PatientID自动增加1。必须为用户禁用PatientID文本,并且需要自动为每个新记录自动递增。自动递增患者ID

任何人都可以请帮忙吗? 感谢提前:)

+5

在SQLServer中放入自动增量并将其增加1 – 2014-11-24 13:47:31

+2

它在* database *中的列上完成 - 不在C#中。 – 2014-11-24 13:47:37

+0

检查此问题http://stackoverflow.com/questions/5083846/sql-server-2005-using-generated-sequences-instead-of-identity-columns,它可能会帮助您 – Monah 2014-11-24 13:48:18

回答

2

这样创建表:

CREATE TABLE [dbo].[Patient](
    [PatientID] [int] IDENTITY(1,1) NOT NULL, 
    [PatientName] [nvarchar](max) NOT NULL, 
    [Gender] [bit] NULL, 
CONSTRAINT [PK_Patient] PRIMARY KEY CLUSTERED 
(
    [PatientID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 
+0

你真的需要一个人的名字nvarchar max。我从来没有听说过超过8000个字符的名字。还有一点性别?哪一个性别是1?为什么不把它设置为char(1)而不是限制为只允许'M'或'F'。我意识到你的答案是处理原始问题,但如果你要建议表结构,那么他们应该是合理的。 – 2014-11-24 14:54:21

+0

看看肖恩兰格,他没有分享他的桌子结构,所以我怎么能知道他的实际需求......是的,当然你给了我很好的建议..谢谢你与我分享你的知识......我会照顾它从下一个答案... – 2014-11-24 17:32:48

0

如果您使用的是DataTable那么你可以设置AutoIncrementColumn = True将在每个条目增加。

Dim column As DataColumn = New DataColumn 
    column.DataType = System.Type.GetType("System.Int32") 
    With column 
     .AutoIncrement = True 
     .AutoIncrementSeed = 1000 
     .AutoIncrementStep = 10 
    End With 

    ' Add the column to a new DataTable. 
    Dim table As DataTable 
    table = New DataTable 
    table.Columns.Add(column) 

DataColumn.AutoIncrement

1

你可以做到这一点@表水平,同时创建表set Identity to YES and increament it by 1

这里是你enter image description here

-2

我创建的表编程像这样在我的项目的一个参考

public void CreateTables() 
    { 
     SqlConnection conn1 = new SqlConnection(); 

     try 
     { 

      // Drop and Create a new Patient Table 
      string queryDrop = "IF OBJECT_ID('Patient', 'U') IS NOT NULL DROP TABLE Patient"; 


      conn1.ConnectionString = GetConnectionString(); 
      conn1.Open(); 

      SqlCommand cmdDrp = new SqlCommand(queryDrop, conn1); 
      cmdDrp.ExecuteNonQuery(); 


      string query = "CREATE TABLE Patient(" + 
         "PatientId uniqueidentifier DEFAULT NEWSEQUENTIALID()," + 
         "PatientName varchar(50) NOT NULL," + 
         "Gender varchar(50) NOT NULL," + 
        ")"; 
      SqlCommand cmd1 = new SqlCommand(query, conn1); 

      cmd1.ExecuteNonQuery() 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
     finally 
     { 
      conn1.Close();//close the connection 
     } 
} 

    public string GetConnectionString() 
    { 
     string folderpath = Environment.GetFolderPath 
     (Environment.SpecialFolder.Personal);  

     string connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=" + folderpath 
     + "\\FolderName\\Hospitaldata.mdf;Integrated Security=True;" + 
     "Connect Timeout=30;User Instance=True"; 

     return connStr; 

    } 
+0

在这里不好的事情的数量是一种惊人的。如果您打算使用NEWSEQUENTIALID,为什么还要使用唯一标识符?它比int更宽,并且没有任何好处。该表没有主键。作为varchar(50)的性别?只有2个可能的值,它们都不会接近50个字符。然后在应用程序中检查和创建表的逻辑。连接字符串在应用程序中硬编码...坏的列表继续下去。 – 2014-11-24 14:58:35