控制台数据显示问题

ChargeForward 2008-07-16 11:09:38
新问题:
我建立的一个SQL数据表,并且我用C#编程,我要检索某个班级的全体成员,


表如下 : 班级详单

1班 A B C D
2班 E F G H
3班 I J K L

我的程序如下

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;


namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{

SqlConnection SqlCon = new SqlConnection("Data Source=;Database=AAA;User id=sa;pwd=SQL4567");
SqlCon.Open();
string SqlStr="SELECT * FROM Table_1 WHERE 班级=1班";
SqlCommand SqlCmd = new SqlCommand(SqlStr, SqlCon);
SqlDataReader SqlRd = SqlCmd.ExecuteReader();
while (SqlRd.Read())
{
Console.WriteLine(SqlRd.GetString());
}
SqlCon.Close();
}
}
}


貌似我用的GetString()方法不能将内容显示到控制台 有什么其他方法吗?
...全文
137 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
birdyone 2008-07-16
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
string conString = "Data Source=.;Database=Northwind;User id=sa;pwd=";
SqlConnection con = new SqlConnection(conString);

SqlCommand command = new SqlCommand("select * from Orders",con);
con.Open();
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
Console.WriteLine(reader.GetString(1).ToString());
}
con.Close();
con.Dispose();
Console.ReadLine();
}
ChargeForward 2008-07-16
  • 打赏
  • 举报
回复
比如你们说的那个 FieldCount()方法 我从来没在书上见过 是我读的书不对吗?
ChargeForward 2008-07-16
  • 打赏
  • 举报
回复
能给推荐哪本书上有相关内容吗? 我看了ADO.net 可是书上基本找不到我用的着的东西...
ChargeForward 2008-07-16
  • 打赏
  • 举报
回复
请看6楼的陈述 谢谢
zzyhuian06142 2008-07-16
  • 打赏
  • 举报
回复
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int i = 0;
SqlConnection SqlCon = new SqlConnection("Data Source=;Database=AAA;User id=sa;pwd=SQL4567");
SqlCon.Open();
string SqlStr="SELECT * FROM Table_1 WHERE 班级='1'";
SqlCommand SqlCmd = new SqlCommand(SqlStr, SqlCon);
SqlDataReader SqlRd = SqlCmd.ExecuteReader();
while (SqlRd.Read())
{

for(int i=0;i<SqlRd.FieldCount();i++)
{
Console.Write(SqlRd[i]);
}

}
Console.WriteLine();
SqlCon.Close();
}
}
}
这样!!!!!!!!!!!!!!!
基础书去看看啊
birdyone 2008-07-16
  • 打赏
  • 举报
回复

C#
public override string GetString (
int i
)

这是MSDN中对sqlDataReader.GetString()方法的定义,lz都没有给GetString()传参数

ChargeForward 2008-07-16
  • 打赏
  • 举报
回复
我这样写对吗?为什么记过只有一个 A 呢?

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;


namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int i = 0;
SqlConnection SqlCon = new SqlConnection("Data Source=;Database=AAA;User id=sa;pwd=SQL4567");
SqlCon.Open();
string SqlStr="SELECT * FROM Table_1 WHERE 班级='1'";
SqlCommand SqlCmd = new SqlCommand(SqlStr, SqlCon);
SqlDataReader SqlRd = SqlCmd.ExecuteReader();
while (SqlRd.Read())
{
while (SqlRd[i] != NULL)
{
Console.Write(SqlRd[i].ToString() + " ");
i++;
}
}
Console.WriteLine();
SqlCon.Close();
}
}
}
zzyhuian06142 2008-07-16
  • 打赏
  • 举报
回复
SqlRd.Read()每次读取一行
ChargeForward 2008-07-16
  • 打赏
  • 举报
回复
SqlRd.Read()只能是读取列中的下一条记录 而不能读出行中的下一条记录 对吗?
zzyhuian06142 2008-07-16
  • 打赏
  • 举报
回复
SqlRd.GetString()这里面要传递一个Index
初吻给了烟 2008-07-16
  • 打赏
  • 举报
回复
while (SqlRd.Read())
{
Console.WriteLine(SqlRd[0].ToString());
Console.WriteLine(SqlRd[1].ToString());
...................
}
zzyhuian06142 2008-07-16
  • 打赏
  • 举报
回复
Console.Write(SqlRd[0].ToString() + " " + SqlRd[1].ToString() +" " + SqlRd[2].ToString())
Console.writeLine();

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧