简体   繁体   中英

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." The fields ""x-coordinate" and "y-coordinate" are type 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?

I am trying to do this only using C# in asp.net.

将查询“从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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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