您好我想将Gender列更改为下拉菜单,将Age列更改为每年更改。
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
)
答案 0 :(得分: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>
该控件是一个控件,可用于填充数据库中的数据。请记住,如果要填充下拉列表,可以通过各种不同的方式进行填充。此示例显示了如何从标记执行此操作。
要从Code Behind执行相同的操作,逻辑如下(假设您已在标记页面上创建了控件):
这将是以下内容:(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
设置连接和数据源本身就是一个帖子。
除了此处列出的内容之外,还有更多内容,但这是如何实现您想要的基本想法。