【发布时间】:2016-06-25 06:27:25
【问题描述】:
我的数据库代码:
USE Student
GO
CREATE PROCEDURE spDisplayCountry
AS
BEGIN
SELECT countryName
FROM CountryList
END
这段代码创建了一个我想从 C# 代码中使用的存储过程。
检索数据的C#代码:
protected void Page_Load(object sender, EventArgs e)
{
// Connection String
string constr = ConfigurationManager.ConnectionStrings["EmployeeContext"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("spDisplayCountry",con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds); // Fill Dataset
countryList.DataTextField = ds.Tables[0].Columns["countryName"].ToString();
countryList.DataValueField = ds.Tables[0].Columns["countryId"].ToString(); // to retrieve specific text feild name.
countryList.DataSource = ds.Tables[0]; // assigning Datasource to DropDown List
countryList.DataBind(); // binding dropdown list.
}
【问题讨论】:
标签: .net sql-server database