新问题:
我建立的一个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()方法不能将内容显示到控制台 有什么其他方法吗?
我建立的一个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()方法不能将内容显示到控制台 有什么其他方法吗?
Console.writeLine();
{
Console.WriteLine(SqlRd[0].ToString());
Console.WriteLine(SqlRd[1].ToString());
...................
}
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();
}
}
}
C#
public override string GetString (
int i
)这是MSDN中对sqlDataReader.GetString()方法的定义,lz都没有给GetString()传参数
{
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();
}
}
}
这样!!!!!!!!!!!!!!!
基础书去看看啊
{
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();
}