using System.Collections
using System.Collections.Specialized
using System.Data
using System.Data.SqlClient
using System.Configurationnamespace DAL
{//数据库连接字符串
protected static string connectionString="server=.database=datauid=sapwd=sa"public static string ConnectionString
{
set { connectionString = value}
get { return connectionString}}/// <summary>执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet()
try
{
connection.Open()
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection)
command.Fill(ds, "ds")
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message)
}
return ds
}
} 查询类:using System
using System.Data
using System.Text
using System.Data.SqlClient
using System.Collections
using System.Collections.Generic
using Business.Modelsnamespace Business.SQLServerDAL
{/// <summary>
/// 数据访问类handbook。
/// </summary>
public partial class handbookService
{public handbookService()
{}/// <summary>
/// 获数据列表
/// </summary>
public DataSetGetList()
{
return DbHelperSQL.Query("select * from [2002handbook]")
}}} 页面后台.cshandbookService bll=new handbookService ()GridView1.DataSource=bll.GetList()GridView1.DataBind()
假设你下拉框已经绑定数据源,同时datatextfield和datavaluefield也绑定,那么:using
(sqlconnection
con
=
new
sqlconnection("你的数据库字符连接串"))
{
sqlcommand
cmd
=
new
sqlcommand("select
top
1
from
表
where
字段名=@字段名")
cmd.parameters.addwithvalue("@字段名",dropdownlist1.selectedvalue)
con.open()
textbox1.text
=
cmd.executescalar().tostring()
}
注意:下拉列表autopostback=true
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)