C#控制台程序如何连接数据库????

zlubuntu 2008-06-15 05:26:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace datebase
{
class SqlOperateInfo

{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=localhost; Initial Catalog=stuinfo_table; User ID=sa; Password=123;");

SqlCommand Scommand = new SqlCommand();
Scommand.Connection = conn;
Scommand.CommandType = "select * from stuinfo_table";

conn.Open();

SqlDataReader sdr;
sdr = Scommand.ExecuteNonQuery();

while(sdr.Read())
{

这的内容不知如何去写????? 我的表结构是四个字段分别为name
age brithday sex

}
}
}
}


谢谢大家了..........
...全文
744 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuhuan88 2008-06-15
  • 打赏
  • 举报
回复
SqlConnection conn = new SqlConnection("Server=localhost; Initial Catalog=stuinfo_table; User ID=sa; Password=123;");

SqlCommand Scommand = new SqlCommand();
Scommand.Connection=conn;
Scommand.CommandType=CommandType.Text;
Scommand.CommandText = "select * from stuinfo_table";

conn.Open();

SqlDataReader sdr;
sdr = Scommand.ExecuteReader(CommandBehavior.CloseConnection);

while(sdr.Read())
{
Console.WriteLine(sdr["name"].ToString().Trim());
Console.WriteLine(sdr["age"].ToString().Trim());
Console.WriteLine(sdr["birthday"].ToString().Trim());
Console.WriteLine(sdr["sex"].ToString().Trim());
}
conn.Close();
zspsys 2008-06-15
  • 打赏
  • 举报
回复
好象就是上边的吧 记不太清楚 刚学的,得复习复习
conan304 2008-06-15
  • 打赏
  • 举报
回复
            SqlConnection conn = new SqlConnection("Server=localhost; Initial Catalog=stuinfo_table; User ID=sa; Password=123;"); 

SqlCommand Scommand = new SqlCommand();
Scommand.Connection=conn;
Scommand.CommandType=CommandType.Text;
Scommand.CommandText = "select * from stuinfo_table";

conn.Open();

SqlDataReader sdr;
sdr = Scommand.ExecuteReader();

while(sdr.Read())
{
Console.WriteLine(sdr["name"].ToString().Trim());
Console.WriteLine(sdr["age"].ToString().Trim());
Console.WriteLine(sdr["birthday"].ToString().Trim());
Console.WriteLine(sdr["sex"].ToString().Trim());
}
conn.Close();
amandag 2008-06-15
  • 打赏
  • 举报
回复
SqlConnection conn = new SqlConnection("Server=localhost; Initial Catalog=stuinfo_table; User ID=sa; Password=123;");

SqlCommand Scommand = new SqlCommand();
Scommand.Connection = conn;
Scommand.CommandText = "select * from stuinfo_table";

conn.Open();

SqlDataReader sdr = Scommand.ExecuteReader();

while(sdr.Read())
{
System.Console.Write(sdr["name"]);
System.Console.Write(sdr["age"]);
System.Console.Write(sdr["brithday"]);
System.Console.Write(sdr["sex"]);
System.Console.WriteLine();
}
czk598478 2008-06-15
  • 打赏
  • 举报
回复
我只能帮你顶下,让其他人帮你解决!
pupstar 2008-06-15
  • 打赏
  • 举报
回复
连接数据库不是在那里边去写的!
SuperTyro 2008-06-15
  • 打赏
  • 举报
回复
连的是 SQL 2005把?

你是要把数据库里的信息 输出出来吗? 是这样的
while (sdr.Read())
{
System.Console.Write(sdr["name"]);
System.Console.Write(sdr["age"]);
System.Console.Write(sdr["brithday"]);
System.Console.Write(sdr["sex"]);
System.Console.WriteLine();
}

这样就可以了
我是小菜鸟! 说的不好清见谅, 希望能够帮到你!

111,094

社区成员

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

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

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