C# 如何在 Windows 窗体应用程序中使用 Console.WriteLine()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18601515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Console.WriteLine() in windows Forms Application
提问by jeastham1993
I am teaching myself basic C# with a view to develop applications that link to a SQL Server database. I am creating a very basic application that allows me type 2 values a form and on button click insert the values. I also want a separate button which shows me all the data from the table in the ConsoleWindow. The insert works perfectly, my only issue now is that when I click the ShowData button nothing happens, any ideas?
我正在自学基本的 C#,目的是开发链接到 SQL Server 数据库的应用程序。我正在创建一个非常基本的应用程序,它允许我在表单中输入 2 个值,然后单击按钮插入值。我还需要一个单独的按钮,用于显示 ConsoleWindow 中表格中的所有数据。插入工作完美,我现在唯一的问题是当我单击 ShowData 按钮时没有任何反应,有什么想法吗?
I can imagine it's going to be something pretty basic but as I said I'm still learning.
我可以想象这将是非常基本的东西,但正如我所说,我仍在学习。
public partial class InsertNames : Form
{
public InsertNames()
{
InitializeComponent();
}
private SqlConnection thisConnection = new SqlConnection("Data Source=(localdb)\V11.0;database=Dev");
private void FirstName_TextChanged(object sender, EventArgs e)
{
string firstName = FirstName.Text;
}
private void LastName_TextChanged(object sender, EventArgs e)
{
string lastName = LastName.Text;
}
private void Insert_Click(object sender, EventArgs e)
{
try
{
thisConnection.Open();
SqlCommand insertCommand = thisConnection.CreateCommand();
insertCommand.CommandText = "INSERT INTO Names (FirstName,LastName) Values (@FirstName, @LastName)";
insertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = FirstName.Text;
insertCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 50).Value = LastName.Text;
insertCommand.ExecuteNonQuery();
Console.WriteLine(insertCommand);
thisConnection.Close();
}
catch (SqlException excep)
{
Console.WriteLine(excep.Message);
}
}
private void ShowData_Click(object sender, EventArgs e)
{
try
{
thisConnection.Open();
SqlDataReader myReader = null;
SqlCommand selectCommand = new SqlCommand("Select * from Names", thisConnection);
myReader = selectCommand.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine(myReader["FirstName"].ToString());
Console.WriteLine(myReader["LastName"].ToString());
}
thisConnection.Close();
}
catch (SqlException excep)
{
Console.WriteLine(excep.Message);
}
}
}
采纳答案by Thilina H
Console.WriteLine
command is working only in console application type. so using windows app doesn't display required output,seems your application is windows form application.
Console.WriteLine
命令仅适用于控制台应用程序类型。所以使用 windows 应用程序不会显示所需的输出,似乎您的应用程序是 windows 窗体应用程序。
if you want show output on Console window using Console.WriteLine
withing the windows form application need to add this property and call it from the main form constructor.then it will open with console as well.
如果您想使用Console.WriteLine
windows 窗体应用程序在控制台窗口上显示输出,则 需要添加此属性并从主窗体构造函数调用它。然后它也将与控制台一起打开。
public InsertNames()
{
AllocConsole();
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AllocConsole();
OR
或者
In project settings you can set application type as Console. Then you will get console and form as well.
在项目设置中,您可以将应用程序类型设置为控制台。然后你也会得到控制台和表格。