我创建了这个简单的“员工”程序来训练我的sql.如果用户愿意,我决定显示数据库中的所有数据.using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;...
我创建了这个简单的“员工”程序来训练我的sql.
如果用户愿意,我决定显示数据库中的所有数据.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace MySQLQueryTest
{
class Program
{
static void Main(string[] args)
{
string response = "";
while (response != "exit")
{
Console.Write(">>");
response = Console.ReadLine();
switch (response.ToLower())
{
case "emp":
employeeMenu();
break;
case "-h":
Console.WriteLine("emp\tDisplays the Employee database menu");
Console.WriteLine("exit\tWill exit the program");
Console.WriteLine("clear\tWill clear the console window");
break;
case "clear":
Console.Clear();
break;
default:
Console.WriteLine("Invalid Command; for help type -h");
break;
}
}
}
static void employeeMenu()
{
Console.WriteLine("-------------Menu-------------");
Console.WriteLine("Type 'list' to list all employees.");
Console.WriteLine("Type 'add' to add an employee.");
Console.WriteLine("------------------------------");
string response = "";
while (response != "exit")
{
Console.Write("EmployeeMenu>");
response = Console.ReadLine();
switch (response.ToLower())
{
case "add":
getInfo();
break;
case "exit":
Console.WriteLine("Returning to Main Menu...");
break;
case "list":
Console.WriteLine("Here I will display all the SQL data");
break;
default:
Console.WriteLine("Invalid Command\n");
break;
}
}
}
static void getInfo()
{
Console.Write("First Name: ");
string fname = Console.ReadLine();
Console.Write("Last Name: ");
string lname = Console.ReadLine();
Console.Write("Job: ");
string job = Console.ReadLine();
addEmployee(fname, lname, job);
}
static void addEmployee(string the_fname, string the_lname, string the_job)
{
var connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename='c:\users\omri\documents\visual studio 2012\Projects\Map\Map\db.mdf';Integrated Security=True");
SqlCommand cmd;
connection.Open();
try
{
cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO Employees(fname, lname, job) values('" + the_fname + "', '" + the_lname + "', '" + the_job + "');";
cmd.ExecuteNonQuery();
Console.WriteLine("Added " + the_fname + " " + the_lname + " to employee database.");
}
catch (Exception)
{
throw;
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}
}
}
}
我在互联网上搜索一些答案,但没有成功,我发现最接近的是:Displaying result of query.
不确定这是我正在寻找的.
所以,我的问题是如何显示员工的所有数据?
先感谢您!
解决方法:
家庭作业?
在将员工添加到数据库中时,您已经拥有运行SQL查询的代码.要显示数据库中的所有员工,您希望执行类似于addEmployee方法的操作,但不要运行INSERT sql命令而是要SELECT
using (SqlConnection connection = new SqlConnection(YOURCONNECTIONSTRING))
{
connection.Open();
using (SqlCommand command = new SqlCommand("SELECT * FROM Employees", connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
Console.WriteLine(reader.GetValue(i));
}
Console.WriteLine();
}
}
}
}
沃梦达教程
本文标题为:c# – 在数据库的控制台窗口中显示数据
基础教程推荐
猜你喜欢
- c# – Python .NET,多线程和windows事件循环 2023-09-20
- WPF实现半圆形导航菜单 2023-03-10
- c#中WinForm使用OpencvSharp4实现简易抓边 2023-06-05
- 详解C# 代码占用的空间 2022-12-06
- C#操作进程的方法介绍 2023-05-22
- C# ADO.NET 离线查询的实现示例 2023-02-17
- c# – 使用MvvmCross添加Windows 8.1存储基本页面(不再继承自不存在的LayoutAware但基本页面类) 2023-09-18
- C#中读写INI配置文件的方法 2022-12-26
- Unity3D使用陀螺仪控制节点旋转 2023-02-02
- winform c#中子窗体关闭刷新父窗体的实例 2022-10-27