然后在Webform_ aspx cs里面获取连接字符串 要添加如下引用
复制代码 代码如下: using System Configurationusing System Datausing System Data SqlClient 复制代码 代码如下: SqlConnection conprotected void Page_Load(object sender EventArgs e) { ConnectDB()} private void ConnectDB() { string ConString = ConfigurationManager ConnectionStrings["ConnectionString"] ConnectionStringcon = new SqlConnection(ConString)con Open()SqlCommand = new SqlCommand()SqlDataReader sdrstring sqlstr = "select * from item"CommandText = sqlstrConnection = consdr = ExecuteReader()while (sdr Read()) { Response Write(sdr["字段名"] ToString()+"</br>")} sdr Close()sdr = null} lishixinzhi/Article/program/net/201311/14106例子:
//建立数据库连接对象sc
SqlConnection sc = new SqlConnection()
//数据库连接字符串
sc.ConnectionString = "server=.database=wangjunuid=sapwd=sqlserver"
//打开数据库
sc.Open()
Console.WriteLine("已经连接")
//数据库命令对象字符串
string sql = "select rtrim(学号),rtrim(姓名),rtrim(性别),rtrim(总分),rtrim(address),rtrim(所属院系) from student"
//新建数据库命令对象
SqlCommand cmd = new SqlCommand(sql,sc)
//新建数据集对象reader 并得到命令对象读取的结果集
SqlDataReader reader = cmd.ExecuteReader()
//使用循环输出数据集中的数据
while (reader.Read())
{
Console.WriteLine("{0} | {1} | {2}| {3} | {4} | {5}",reader[0],reader[1],reader[2],reader[3],reader[4],reader[5])
}
还可以用SqlDataAdapter 然后填充到dataset里面
然后对dataset *** 作即可
id是数字的话这样写:string sqlstr = "select * from video where vid=" + id
id是字符串的话这样写:
string sqlstr = "select * from video where vid='" + id + "'" '前面句子里最后一个字符是单引号,最后面一对双引号包着的也是个单引号,别写错,直接从这里复制最好。
而且你上面queryString写的也不对吧,应该是括号包裹变量,你怎么中括号呢?这样也行?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)