如何在C#中编写查询 [英] How to Write Query in C#

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

问题描述

你好朋友

请帮我用C#写一个查询..

EX:

string tstrres =选择(TIME)来自RAD_DETAILS的时间(varchar,TIME,108)=''+BETWEEN+ tstr1 +AND+ tstr2 +'';

以上是语法正确与否请告诉我..

Hello friends
pls help me to write a query in C#..
EX:
string tstrres = "Select (TIME)TIME FROM RAD_DETAILS WHERE (varchar,TIME,108)=''"+"BETWEEN " + tstr1 + "AND" +tstr2 + "''";
the above one is syntax wise correct or not please tell me..

推荐答案

string tstrres = @从RAD_DETAILS WHERE中选择(TIME)TIME(varchar,TIME,108)=''+BETWEEN + tstr1 +AND+ tstr2 +'';







''@' '对于c#中的字符串。
string tstrres = @"Select (TIME)TIME FROM RAD_DETAILS WHERE (varchar,TIME,108)=''"+"BETWEEN " + tstr1 + "AND" +tstr2 + "''";



''@'' for string in c#.


您能否详细说明这个问题?

您正在将TIME列转换为Varchar并检查其值是否在字符串tstr1和tstr2,你做不到。

更好的方法是将tstr1和tstr2的值转换为Time(DateTime)值,然后检查条件。
Can you pls eloborate on the issue?
You are converting TIME column to Varchar and checking if its value is between string tstr1 and tstr2, which you cannot do.
The better way to do is convert the values of tstr1 and tstr2 into Time(DateTime) values and then check the condition.


创建项目



启动Visual Studio。



在菜单栏上,选择文件,新建,项目。



新建项目对话框打开。



展开已安装,展开模板,展开Visual C#,然后选择控制台应用程序。



在名称文本框中,输入其他名称或接受默认名称,然后选择确定按钮。



新项目出现在解决方案资源管理器中。



请注意,您的项目引用了System.Core.dll和System.Linq命名空间的using指令。



查询的数据源是一个简单的列表学生对象。每个学生记录都有一个名字,姓氏和一个整数数组,代表他们在课堂上的考试成绩。将此代码复制到您的项目中。请注意以下特征:



学生班级由自动实施的属性组成。



每个学生在列表用对象初始化程序初始化。



列表本身用集合初始化程序初始化。



将初始化和实例化整个数据结构,而无需显式调用任何构造函数或显式成员访问。有关这些新功能的更多信息,请参阅自动实现的属性(C#编程指南)和对象和集合初始化程序(C#编程指南)。



并编写以下代码< br $>


To create a project

Start Visual Studio.

On the menu bar, choose File, New, Project.

The New Project dialog box opens.

Expand Installed, expand Templates, expand Visual C#, and then choose Console Application.

In the Name text box, enter a different name or accept the default name, and then choose the OK button.

The new project appears in Solution Explorer.

Notice that your project has a reference to System.Core.dll and a using directive for the System.Linq namespace.

The data source for the queries is a simple list of Student objects. Each Student record has a first name, last name, and an array of integers that represents their test scores in the class. Copy this code into your project. Note the following characteristics:

The Student class consists of auto-implemented properties.

Each student in the list is initialized with an object initializer.

The list itself is initialized with a collection initializer.

This whole data structure will be initialized and instantiated without explicit calls to any constructor or explicit member access. For more information about these new features, see Auto-Implemented Properties (C# Programming Guide) and Object and Collection Initializers (C# Programming Guide).

and write following code

public class Student
{
    public string First { get; set; }
    public string Last { get; set; }
    public int ID { get; set; }
    public List<int> Scores;
}

// Create a data source by using a collection initializer.
static List<Student> students = new List<Student>
{
   new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},
   new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
   new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},
   new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}},
   new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}},
   new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}},
   new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},
   new Student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}},
   new Student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}},
   new Student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}},
   new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}},
   new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }
};


这篇关于如何在C#中编写查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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