如何使用C#从数据库中找到最近或最近的数字 [英] How Do I Find The Nearest Or Closest Numbers From Database Using C#

查看:123
本文介绍了如何使用C#从数据库中找到最近或最近的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库表学生(姓名,标记)。

i想要选择标记等于或最接近x的学生姓名;

其中x是用户在c#窗口中输入的整数表格



请帮帮我,我真的需要它



Tushar

i have a database table student(name,marks).
i want to select student names where marks is equal to or closest to x;
where x is the integer number entered by user in c# windows form

please help me i really need it

Tushar

推荐答案

类似于:

something like:
select name from students where abs(mark-x)=(select min(abs(mark-x)) from students)


5月这会对你有所帮助。



http://stackoverflow.com/questions/19967117/how-to-find-the-value-closest-to-zero-using-sql-server [ ^ ]
May be this will help you.

http://stackoverflow.com/questions/19967117/how-to-find-the-value-closest-to-zero-using-sql-server[^]


<pre lang="SQL">

YourTable 
studentName	Marks
Mark	        100
Tisa	        200
John	        250
Michelle	150
Parker	        450
Pete	        50
Ben	        400
Col	        325
Mis	        435
Aai	        265

First Create a Procedure Like this

CREATE PROCEDURE [dbo].[usp_FetchRecord] 
	@UserEnteredValue int
AS
BEGIN
	SET NOCOUNT ON;
	SELECT TOP 1 studentName,Marks FROM YourTable 
        WHERE Marks>=@UserEnteredValue order by Marks asc
END

-- To run the Procedure, here user has enetered 200
--EXEC [dbo].[usp_FetchRecord] 200

Result
studentName	Marks
Tisa	        200





按钮后面的代码点击:





Code Behind on Button Click :

private void button1_Click(object sender, EventArgs e)
        {
            DataTable dttable = new DataTable();
            using (var con = new      SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString))
            using (var cmd = new SqlCommand("usp_FetchRecord", con))
            using (var da = new SqlDataAdapter(cmd))
            {
                cmd.Parameters.AddWithValue("@UserEnteredValue", textBox1.Text);
                cmd.CommandType = CommandType.StoredProcedure;
                da.Fill(dttable);
            }
            // You can use this data table dttable to get the result and display it.
        }


这篇关于如何使用C#从数据库中找到最近或最近的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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