2016-04-01 15 views
-2

こんにちはジェンダー列をドロップダウンメニューに変更し、年齢列を毎年変更したいと考えています。年齢列を毎年変更し、性別のドロップダウンメニュー

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

年齢が増分されているかどうかは、どのように知ることができますか?計算されたフィールドは通常*計算され、保存された値が最新のものかどうかを判断できないため、保存されません。 DateOfBirthを(日付として)保存します。性別については、それにMFだけの表に対する制約を持つText(1)列にします。メニューはドメインテーブル – Plutonix

+0

から来ることができます。こんにちは。なぜあなたはそれが働いていなくても、あなたが自分のために働いてくれたことを自分で試したことを教えてください。それから私たちはあなたがそれが正常に動作するように助けることができます –

+1

あなたの質問は**それに**攻撃的なトーン**を持っています。 *** "私はこれが欲しい!" ***あなたの投稿を形成するための良い方法ではありません。投稿を編集して特定のコーディングに関する質問を含めると、私たちはその回答を基にすることができます。 S/Oへようこそ。 –

答えて

0

あなたの質問は少し曖昧です。あなたがASP.NET C#でこれをやっている場合、それは次のようになります。

<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> 

コントロールは、データベースからのデータを取り込むために使用できるコントロールです。ドロップダウンリストを作成する場合は、さまざまな方法で行うことができます。この例は、マークアップから行う方法を示しています。 ddlMyControlは仮定(

  1. 作成し、コントロールの接続を設定します。

    は、次のようにロジックがあり、背後にあるコードから同じことを行うには(すでにあなたのマークアップ・ページ上のコントロールを作成していると仮定)既にマークアップページにIDを設定しています)。
  2. コントロールのデータソースを設定します。
  3. Dataコントロールをページにバインドします。

次のようにこれは何かのようになります。(C#および以下VB)

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 

接続とデータソースを設定し、それ自体のポストです。

ここに挙げられているものよりも多くのことがありますが、これはあなたが望むものを達成するための基本的な考え方です。

関連する問題