csharp 用message.show()输出在sqlserver中查询到的数据
时间: 2024-05-14 10:15:57 浏览: 13
您好,可以使用以下代码将在SQL Server中查询到的数据显示在C#中:
```csharp
using System;
using System.Data.SqlClient;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// 创建数据库连接字符串
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;";
// 创建SQL查询语句
string query = "SELECT * FROM myTable";
// 创建连接对象和命令对象
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
{
try
{
// 打开连接
connection.Open();
// 执行查询,并返回数据读取器
using (SqlDataReader reader = command.ExecuteReader())
{
// 读取每行数据
while (reader.Read())
{
// 输出每列数据
Console.WriteLine("{0}\t{1}\t{2}", reader.GetInt32(0), reader.GetString(1), reader.GetDecimal(2));
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
Console.ReadLine();
}
}
}
```
这段代码会查询名为 `myTable` 的数据库表中的所有数据,并将这些数据显示在控制台中。您可以根据需要修改这段代码,以适应您的具体情况。