C#中sqlDataRead 的三种方式遍历读取各个字段数值的方法
数据库的查询分析器中写上如下代码:
createtablestudentname
(
idintprimarykeyidentity(1,1)notnull,
namenvarchar(500)notnull
)
insertintostudentnamevalues('long')
insertintostudentnamevalues('long')
insertintostudentnamevalues('long')
insertintostudentnamevalues('long')
insertintostudentnamevalues('long')
insertintostudentnamevalues('chao')
insertintostudentnamevalues('chao')
--sqlserver2012通过。
---------------------------------------------------------------------------------------------------
以下是第一种方法:
usingSystem.Data;
usingSystem.Data.SqlClient;
stringconstr="server=localhost;uid=sa;pwd=longshicheng;database=Student";
SqlConnectionconn=newSqlConnection(constr);
SqlCommandcmd=newSqlCommand("select*fromstudentname",conn);
conn.Open();
SqlDataReaderrdr=cmd.ExecuteReader();
stringmakestr="";
DataTabletable=newDataTable();
table.Load(rdr);//数据表装入dataReader。
stringresult="";
for(inti=0;i以下是第二种方法:
stringconstr="server=localhost;uid=sa;pwd=longshicheng;database=Student";
SqlConnectionconn=newSqlConnection(constr);
SqlCommandcmd=newSqlCommand("select*fromstudentname",conn);
conn.Open();
SqlDataReaderrdr=cmd.ExecuteReader();
stringmakestr="";
for(intk=0;k
以下是第三种方法:
stringconstr="server=localhost;uid=sa;pwd=longshicheng;database=Student";
SqlConnectionconn=newSqlConnection(constr);
SqlCommandcmd=newSqlCommand("select*fromstudentname",conn);
conn.Open();
SqlDataReaderrdr=cmd.ExecuteReader();
stringmakestr="";
for(intk=0;k
以上所有方法运行结果均如下:
id name
---------------------------------------------
1 long
2 long
3 long
4 long
5 long
6 chao
7 chao
总结
以上所述是小编给大家介绍的C#中sqlDataRead的三种方式遍历读取各个字段数值的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!