显示数据库中的信息的问题???

wormwormwormworm 2005-11-24 02:43:44
/*
FirstExample.cs illustrates how to:
1. Connect to the SQL Server Northwind database.
2. Retrieve a row from the Customers table using
a SQL SELECT statement.
3. Display the columns from the row.
4. Close the database connection.
*/

using System;
using System.Data.SqlClient;

class FirstExample
{
public static void Main()
{
try
{
// step 1: create a SqlConnection object to connect to the
// SQL Server Northwind database
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"
);

// step 2: create a SqlCommand object
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

// step 3: set the CommandText property of the SqlCommand object to
// a SQL SELECT statement that retrieves a row from the Customers table
mySqlCommand.CommandText =
"SELECT CustomerID, CompanyName, ContactName, Address " +
"FROM Customers " +
"WHERE CustomerID = 'ALFKI'";

// step 4: open the database connection using the
// Open() method of the SqlConnection object
mySqlConnection.Open();

// step 5: create a SqlDataReader object and call the ExecuteReader()
// method of the SqlCommand object to run the SELECT statement
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();

// step 6: read the row from the SqlDataReader object using
// the Read() method
mySqlDataReader.Read();

// step 7: display the column values
Console.WriteLine("mySqlDataReader[\"CustomerID\"] = " +
mySqlDataReader["CustomerID"]);
Console.WriteLine("mySqlDataReader[\"CompanyName\"] = " +
mySqlDataReader["CompanyName"]);
Console.WriteLine("mySqlDataReader[\"ContactName\"] = " +
mySqlDataReader["ContactName"]);
Console.WriteLine("mySqlDataReader[\"Address\"] = " +
mySqlDataReader["Address"]);

// step 8: close the SqlDataReader object using the Close() method
mySqlDataReader.Close();

// step 9: close the SqlConnection object using the Close() method
mySqlConnection.Close();
}
catch (SqlException e)
{
Console.WriteLine("A SqlException was thrown");
Console.WriteLine("Number = " + e.Number);
Console.WriteLine("Message = " + e.Message);
Console.WriteLine("StackTrace:\n" + e.StackTrace);
}
}
}
调试不通过,不知道错在哪里?
...全文
131 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
JzeroBiao 2005-11-24
  • 打赏
  • 举报
回复
给错分了吧,应该是给我楼上的?
haidazi 2005-11-24
  • 打赏
  • 举报
回复
把提示错误贴出来看看
sunnystar365 2005-11-24
  • 打赏
  • 举报
回复
怎么样调试不通过啊,是显示不出信息还是有错误啊
使用SqlDataReader 要使用if 或者while
if(mySqlDataReader.Read())
{
}
或者
while(mySqlDataReader.Read())
{
}
sfwxw0456 2005-11-24
  • 打赏
  • 举报
回复
你可以先一步一部进行测试,先测试连数据库,然后可以无调节的执行select语句,专业一点一点往下测,应该可以找出来问题所在...
Baby_Happy 2005-11-24
  • 打赏
  • 举报
回复
学习
JzeroBiao 2005-11-24
  • 打赏
  • 举报
回复
好快,我来学习的~
accpdingyi 2005-11-24
  • 打赏
  • 举报
回复
错在mySqlCommand.CommandText =
"SELECT CustomerID, CompanyName, ContactName, Address " +
"FROM Customers " +
"WHERE CustomerID = 'ALFKI'";

// step 4: open the database connection using the
// Open() method of the SqlConnection object
mySqlConnection.Open();

// step 5: create a SqlDataReader object and call the ExecuteReader()
// method of the SqlCommand object to run the SELECT statement
SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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