sql select中的foreach实例属性 [英] foreach instance property in sql select

查看:1117
本文介绍了sql select中的foreach实例属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个或多个车辆实例的类车辆,每个车辆都有一个属性注册。   如何遍历每个注册以在sql查询中使用,选择并更新?任何帮助表示赞赏。 我的代码假设
一个实例只使用@Reg和Query.Parameter.AddWithValue(" @Reg",vehicle.getReg());





解决方案

< blockquote>

你好RichardDunneBSc,


你是什么意思"我有代码假设一个实例使用..."?如果要从表中选择所有注册。你只需使用select语句而不添加"where" condition,将返回所有记录。以下
是一个简单的引用演示。

 string connectionString = @" Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = xxxxx \Test.mdf; Integrated Security = True;" ;; 
using(SqlConnection connection = new SqlConnection(connectionString))
{

SqlCommand command = new SqlCommand("选择从车辆注册",连接);
尝试
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
MessageBox.Show(reader [0] .ToString());
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

此致,


Fei Hu

I have a class Vehicle with one or more instances of vehicle, each with a property registration for example.   How do I iterate through each registration for use in sql queries, select and update? Any help appreciated.  I have the code assuming one instance only using @Reg with Query.Parameter.AddWithValue("@Reg", vehicle.getReg());


解决方案

Hello RichardDunneBSc,

What do you mean "I have the code assuming one instance using ..."? If you want to select all registrations from your table. you just use select statement without adding "where" condition, which will return all records. The following is a simple demo for refer.

 string connectionString = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename =xxxxx\Test.mdf; Integrated Security = True;";
            using (SqlConnection connection =new SqlConnection(connectionString))
            {

                SqlCommand command = new SqlCommand("Select registration from vehicle", connection);
                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                       MessageBox.Show(reader[0].ToString());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

Sincerely,

Fei Hu


这篇关于sql select中的foreach实例属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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