C#中sqlDataRead 的三种方式遍历读取各个字段数值的方法
程序员文章站
2023-12-17 09:32:28
数据库的查询分析器中写上如下代码:
create table studentname
(
id int primary key identity(1,1)...
数据库的查询分析器中写上如下代码:
create table studentname ( id int primary key identity(1,1) not null , name nvarchar(500) not null ) insert into studentname values('long') insert into studentname values('long') insert into studentname values('long') insert into studentname values('long') insert into studentname values('long') insert into studentname values('chao') insert into studentname values('chao')
--sql server 2012 通过。
---------------------------------------------------------------------------------------------------
以下是第一种方法:
using system.data; using system.data.sqlclient; string constr = "server=localhost;uid=sa;pwd=longshicheng;database=student"; sqlconnection conn = new sqlconnection(constr); sqlcommand cmd = new sqlcommand("select * from studentname", conn); conn.open(); sqldatareader rdr = cmd.executereader(); string makestr = ""; datatable table = new datatable(); table.load(rdr); //数据表装入 datareader 。 string result=""; for (int i = 0; i < table.rows.count; i++) // 遍历行 { for (int j = 0; j < table.columns.count; j++) //遍历列 { result += table.rows[i][j].tostring(); //逐行逐列显示每个单元格的数值。 } result += environment.newline; //一行遍历完成就写入回车。 } txtlab.text = result; /***************************************************************/
以下是第二种方法:
string constr = "server=localhost;uid=sa;pwd=longshicheng;database=student"; sqlconnection conn = new sqlconnection(constr); sqlcommand cmd = new sqlcommand("select * from studentname", conn); conn.open(); sqldatareader rdr = cmd.executereader(); string makestr = ""; for (int k = 0; k < rdr.fieldcount; k++) //获取字段名称 { makestr += rdr.getname(k) + "\t"; //载入字段名称 } makestr += environment.newline; //换行 makestr = makestr + "---------------------------------------------"+environment.newline; while (rdr.read()) //逐行读取每一笔记录 { for (int t = 0; t < rdr.fieldcount; t++) //逐列读出 { makestr = makestr + rdr[t].tostring() + "\t"; //累加每一行,也就是逐个字段读出对应的数值 } makestr = makestr + environment.newline; //又一行 } txtinfo.text = makestr; rdr.close(); //关闭 conn.close(); }
以下是第三种方法:
string constr = "server=localhost;uid=sa;pwd=longshicheng;database=student"; sqlconnection conn = new sqlconnection(constr); sqlcommand cmd = new sqlcommand("select * from studentname", conn); conn.open(); sqldatareader rdr = cmd.executereader(); string makestr = ""; for (int k = 0; k < rdr.fieldcount; k++) //获取字段名称 { makestr += rdr.getname(k) + "\t"; //载入字段名称 } makestr += environment.newline; //换行 makestr = makestr + "---------------------------------------------" + environment.newline; while (rdr.read()) //逐行读取每一笔记录 { makestr = makestr + rdr.getvalue(0) + " \t" + rdr.getvalue(1).tostring() + "\t"; makestr = makestr + environment.newline; } txtother.text = makestr; rdr.close(); //关闭 conn.close();
以上所有方法运行结果均如下:
id name
---------------------------------------------
1 long
2 long
3 long
4 long
5 long
6 chao
7 chao
总结
以上所述是小编给大家介绍的c#中sqldataread 的三种方式遍历读取各个字段数值的方法,希望对大家有所帮助