阅读在C#中的SQL数据库值 [英] Reading values from SQL database in C#

查看:100
本文介绍了阅读在C#中的SQL数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习C#,我可以将数据写入到数据库没有问题。但我有问题,阅读时,SQL执行很好,但我在使用它存储的问题。如何将我存储应该返回四列,然后向他们展示一个消息框?谢谢你。

 的SqlCommand myCommand =新的SqlCommand(SELECT * FROM的要求,其中完成= 0,MyConnection的);
SqlDataReader的myReader = myCommand.ExecuteReader();
而(myReader.Read())

Console.WriteLine(myReader [用户名]的ToString());
Console.WriteLine(myReader [项目]的ToString());
Console.WriteLine(myReader [数量]的ToString());
Console.WriteLine(myReader [完成]的ToString());
 

解决方案

一个问题是在一段时间后失踪括号

 而(myReader.Read())
{//<<  - 在这里
    Console.WriteLine(myReader [用户名]的ToString());
    Console.WriteLine(myReader [项目]的ToString());
    Console.WriteLine(myReader [数量]的ToString());
    Console.WriteLine(myReader [完成]的ToString());
} //<<  - 在这里
 

如果你跳过只有第一行会在每个循环处理的大括号,其余的将在循环结束后进行处理,然后 myReader 是过去的最后一行。

i have just started learning C# and i can write data to the database without a problem. But i'm having problems with reading, the SQL executes fine but i'm having issues with storing it. How would i store the four columns that should be returned and then show them as a message box? Thanks.

SqlCommand myCommand = new SqlCommand("select * from Requests where Complete = 0", myConnection);
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())

Console.WriteLine(myReader["Username"].ToString());
Console.WriteLine(myReader["Item"].ToString());
Console.WriteLine(myReader["Amount"].ToString());
Console.WriteLine(myReader["Complete"].ToString());

解决方案

One problem is missing braces after the while

while (myReader.Read())
{  // <<- here
    Console.WriteLine(myReader["Username"].ToString());
    Console.WriteLine(myReader["Item"].ToString());
    Console.WriteLine(myReader["Amount"].ToString());
    Console.WriteLine(myReader["Complete"].ToString());
}  // <<- here

if you skip the braces only the first line will be processed in each loop, the rest will be processed after the loop, then myReader is past the last row.

这篇关于阅读在C#中的SQL数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆