2016-04-01 4 views
-2

Hallo Ich möchte die Spalte Geschlecht in Dropdown-Menü und Alter Spalte ändern, um jährlich zu ändern.Ich möchte Alter Spalte jährlich ändern und ein Dropdown-Menü für Geschlecht

CREATE TABLE tbl_Patient 
    (
    PatientID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED, 
    LabControlID AS Cast(Right(Year(getDate()),4) as varchar(4)) +'-' + RIGHT('00000' + CAST(PatientID AS VARCHAR(5)), 5), 
    First_Name varchar(50) 
    ,Last_Name varchar(50) 
    ,Age int 
    ,Male bit 
    ,Female bit 
) 
+2

Wie wird jemals wissen, ob/wann das Alter erhöht wurde? Berechnete Felder werden normalerweise nicht gespeichert *, weil * die berechnet werden können und Sie nie wissen können, ob der gespeicherte Wert aktuell ist. Sabe das DateOfBirth (als Datum). Für Geschlecht machen Sie es eine Text (1) -Spalte mit einer Beschränkung auf eine Tabelle mit nur MF darin. Das Menü kann aus der Domain-Tabelle – Plutonix

+0

Hallo und Willkommen zu Stack Overflow. Warum erzählst du uns nicht, was du selbst versucht hast, damit das für dich funktioniert (auch wenn es nicht funktioniert). Dann können wir Ihnen helfen, damit es richtig funktioniert –

+1

Ihre Frage hat einen ** beleidigenden Ton ** dazu. *** "Ich will das!" *** ist kein guter Weg, um deine Post zu bilden. Bearbeiten Sie Ihren Post, um eine bestimmte Codierungsfrage einzubeziehen, auf die wir unsere Antworten aufbauen können. Willkommen bei S/O. –

Antwort

0

Ihre Frage ist ein wenig vage. Wenn Sie in ASP.NET C# tun dies sind, wäre es in etwa wie folgt aussehen:

<asp:DropDownList runat="server" ID="ddlMyControl" DataSourceID="SqlDataSource1" DataTextField="Age" DataValueField="PatientID" ></asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT * FROM [tbl_Patient]"></asp:SqlDataSource> 

Die Steuerung ist eine Steuerung, die Sie verwenden können Daten aus Ihrer Datenbank zu füllen. Denken Sie daran, wenn Sie eine Drop-Down-Liste füllen möchten, können Sie dies auf verschiedene Arten tun. Dieses Beispiel zeigt, wie man es aus Markup macht.

die gleiche Sache aus dem Code zu tun die Logik hinter, wie folgt ist (vorausgesetzt, Sie haben bereits die Kontrolle über Ihre Markup-Seite erstellt):

  1. erstellen und die Verbindung des Steuersatzes (ddlMyControl unter der Annahme, Sie legen die ID bereits auf der Markup-Seite fest).
  2. Legen Sie die DataSource des Steuerelements fest.
  3. DataBind das Steuerelement auf der Seite.

Dies würde etwas wie folgt aussehen: (C# und VB unten)

string tableNameSpace = "MyDataSet"; //represents the namespace for the dataset 
string queryText = "SELECT * FROM [tbl_Patient]"; 
string connectionString = "My Connection String Details"; 
//string tableName = "MyDataTable";//if you were adding tables on the fly, you would use this 
DataSet ds = new DataSet(tableNameSpace); 

using (SqlConnection connection = new SqlConnection(connectionString)) 
     { 

      SqlCommand cmd = new SqlCommand(queryText, connection);//this can also be enclosed in a using statement as well. See MSDN documentation for more info. 
      SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd.CommandText, connection);    

      dataAdapter.Fill(ds); //fills the dataset with data from your query 

      //access the table from your dataset. 
      //If you are returning multiple tables, this is not 
      //going to work correctly. You'll need to know 
      //what the tables are called. I would suggest here 
      //creating a strongly-typed dataset so you can 
      //reference them by name instead of index. 

      ddlMyControl.DataSource = ds.Tables[0]; 
      //We grab the age to show in the control 
      ddlMyControl.DataTextField = "Age"; 
      //To access the control and reference it through 
      //code, you can do something like: 
      //ddlMyControl.SelectedValue and that would return the 
      //PatientID. 
      ddlMyControl.DataValueField = "PatientID"; 
      ddlMyControl.DataBind(); 
     } 

VB.NET:

Dim tableNameSpace As String = "MyDataSet" 
    Dim queryText As String = "SELECT * FROM [tbl_Patient]" 
    Dim connectionString As String = "My Connection String Details" 
    Dim ds As DataSet = New DataSet(tableNameSpace) 

    'Enclose in a using block to properly dispose resources 
    Using connection As SqlConnection = New SqlConnection(connectionString) 

     Dim cmd As SqlCommand = New SqlCommand(queryText, connection) 'Like my post for C# says, this can also be enclosed in a using block. 
     Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(cmd.CommandText, connectionString) 
     dataAdapter.Fill(ds) 

      ddlMyControl.DataSource = ds.Tables[0] 
     ' We grab the age to show in the control 
     ddlMyControl.DataTextField = "Age" 
     ' To access the control and reference it through 
     ' code, you can do something like: 
     ' ddlMyControl.SelectedValue and that would return the 
     ' PatientID. 
     ddlMyControl.DataValueField = "PatientID" 
     ddlMyControl.DataBind() 

    End Using 

Einstellung der Verbindung und Datenquelle ist ein Beitrag an und für sich.

Es ist mehr als das, was hier aufgeführt ist, aber das ist die Grundidee, wie Sie erreichen, was Sie wollen.

Verwandte Themen