简体   繁体   English

如何从数据库表中提取一个浮点值,并将其分配给asp.net C#中类型为double的变量?

[英]How do I pull an float value from a database table and assign it to a variable of type double in asp.net C#?

Lets say I have a db table called "Tabel1" and its fields are "Id," "username," "x-coordinate," "y-coordinate." 假设我有一个名为“ Tabel1”的数据库表,其字段为“ Id”,“用户名”,“ x坐标”,“ y坐标”。 The fields ""x-coordinate" and "y-coordinate" are type float. 字段“ x坐标”和“ y坐标”是float类型。

If I know that one of the usernames on "Table1" is "Michael," how do I take the results of "SELECT x-coordinate FROM Tabel1 WHERE username = Michael" and assign what is sitting in "x-coordinate" to a variable of type double called currentuserxcoord? 如果我知道“ Table1”上的用户名之一是“ Michael”,我该如何获取“从Tabel1 WHERE用户名= Michael选择SELECT x坐标”的结果,并将“ x坐标”中的内容分配给变量类型为double的类型称为currentuserxcoord?

I am trying to do this only using C# in asp.net. 我试图仅在asp.net中使用C#来执行此操作。

将查询“从Tabel1 WHERE用户名= Michael选择SELECT x坐标”分配给SqlCommand对象并使用ExecuteScalar()函数,如下所示

currentuserxcoord = Convert.ToDouble(sqlcmdobject.ExecuteScalar());

If you are using something like Link to SQL to connect your DB, you would be able to achieve what you want like this example bellow. 如果您使用链接到SQL之类的东西来连接数据库,则可以像下面的示例所示实现所需的功能。

using (var data = new DataBaseContext())
{

   var currentX = data.Tabel1.Where(x => x.Username.Equals("Miachel")).Select(x => x.XCoordinate).FirstOrDefault();

   var currentXDouble = Convert.ToDouble(currentX);
}

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

相关问题 如何从ASP.NET MVC和C#中的数据库中为下拉列表选择值? - How do I select a value for a dropdownlist from a database in ASP.NET MVC and C#? ASP.NET C#如何从数据库中填充表 - ASP.NET C# How to fill table from database 如何从 MySQL 读取双列并将其分配给 C# 双变量? - How do I read a double column from MySQL and assign it to a C# double variable? 如何在C#asp.net中拉回DateTime并存储在type = date的文本框中? - How can I pull back DateTime and store in textbox with type=date in C# asp.net? 如何在asp.net mvc中使用razor viewmodel将c#guid值赋给javascript变量? - How to assign c# guid value to javascript variable using razor viewmodel in asp.net mvc? 在 ASP.NET/C# 中,如何将数据从数据库获取到 CSS 样式表中? - In ASP.NET/C#, how do I get data from the database into a CSS stylesheet? 如何从数据库驱动的数据在C#和asp.net中设置GridView的字段宽度 - How do i set the field widths of a GridView in C# and asp.net from database driven data 如何将POST和GET数据保存到会话中,并使用ASP.NET C#将其提取? - How do i save POST and GET data into a session and pull it up with ASP.NET C#? 如何使用 c# 从 SQL 数据库中提取数据到本地双变量中? - How do I pull data from an SQL database into local double var using c#? 检查数据库表Asp.net C#中的值 - Check if value in database table Asp.net C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM