using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace CMS.Admin
{
public partial class select : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sql = "select * from [Column] where ColumnId in (@number1,@number2)"; //带参数的sql语句
SqlParameter[] p = ]; //定义sql语句的参数数组
p[] = new SqlParameter("@number1", SqlDbType.Int); //定义参数的数据类型
p[] = new SqlParameter("@number2", SqlDbType.Int); //定义参数的数据类型
p[].Value = ; //赋值
p[].Value = ; //赋值
DataTable data = QuerySql(sql, p); //调用数据库查询方法,获取 DataTable 数据
foreach (DataRow row in data.Rows) //遍历 DataTable 中的具体数据
{
foreach (DataColumn column in data.Columns)
{
Response.Write(row[column]);
}
}
}
/// <summary>
/// 获取配置的数据库连接字符转
/// </summary>
public static string ConnStr
{
get
{
return System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString.ToString();
}
}
/// <summary>
/// 执行数据库查询
/// </summary>
/// <param name="sql">带或不带参数的sql语句</param>
/// <param name="parm">sql语句的参数数组</param>
/// <returns>查询结果(数据表对象)</returns>
public static DataTable QuerySql(string sql, SqlParameter[] par)
{
SqlConnection conn = new SqlConnection(ConnStr); //实例化数据库连接对象
SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //实例化数据库识别器对象
) //给数据库识别对象赋予参数
{
foreach (SqlParameter p in par)
{
sda.SelectCommand.Parameters.Add(p);
}
}
DataTable data = new DataTable(); //定义表数据
sda.Fill(data); //填充数据表
return data;
}
}
}