简体   繁体   English

在 C# 中从 SQL 数据库中读取值

[英]Reading values from SQL database in C#

i have just started learning C# and i can write data to the database without a problem.我刚刚开始学习 C#,我可以毫无问题地将数据写入数据库。 But i'm having problems with reading, the SQL executes fine but i'm having issues with storing it.但是我在阅读时遇到了问题,SQL 执行得很好,但是我在存储它时遇到了问题。 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.如果您跳过大括号,则在每个循环中只处理第一行,其余的将在循环之后处理,然后myReader超过最后一行。

Don't forget to use the using(){} block :不要忘记使用using(){}块:

using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("select * from Requests where Complete = 0", connection))
{
    connection.Open();  
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            Console.WriteLine(reader["Username"].ToString());
            Console.WriteLine(reader["Item"].ToString());
            Console.WriteLine(reader["Amount"].ToString());
            Console.WriteLine(reader["Complete"].ToString());
        }
    }
}

Personally I'd write a class with 4 properties (with matching names and types), then use "dapper" (http://code.google.com/p/dapper-dot-net/):我个人会编写一个具有 4 个属性(具有匹配的名称和类型)的类,然后使用“dapper”(http://code.google.com/p/dapper-dot-net/):

var data = connection.Query<Request>(
    "select * from Requests where Complete = 0").ToList();

With something like:有类似的东西:

public class Request {
    public string Username{get;set;}
    ...
    public bool Complete {get;set;}
}

Dapper is free, simple, has parameterisation to avoid SQL-injection, and is very very fast. Dapper 是免费的,简单的,具有参数化以避免 SQL 注入,并且非常非常快。

我知道它有点晚了,但你可以使用本地字符串变量,或字符串数​​组或列表将数据插入数据库中,然后在你的控制台写入行中调用它

I would create an object with properties that holds those values and then pass that object around as needed.我将创建一个具有保存这些值的属性的对象,然后根据需要传递该对象。

public class YourObjectName
{
   public string Username { get; set; }
   public string Item { get; set; }
   public string Amount { get; set; }
   public string Complete { get; set; }
}

YourObjectName a = new YourObjectName();
a.Username = Reader['Username'].ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM