2014-11-24 155 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