简体   繁体   中英

SQL Server 2014: Convert and save location coordinates

I want to save coordinates to SQL Server.

Decimal latitude, longitude;

String[] location = txt_coord.Text.Split(',');
latitude = Convert.ToDecimal(location[0]);
longitude = Convert.ToDecimal(location[1]);

Using LINQ to save

tbl_location loc = new tbl_location();
loc.ID = personal.id_personalinfo;
loc.latitude = latitude;
loc.longitude = longitude;

db.tbl_locations.InsertOnSubmit(loc);

db.SubmitChanges();

Error actually occurs on the conversion, I used the decimal(18, 9) data type in SQL Server. Error is:

System.FormatException: Input string was not in a correct format.

Does this mean that the string itself is the error...!?

Also, is it right to use decimal for coordinates? should I use geography (internet says LINQ does not support this data type.)

Help.

The exception you are getting indicates that Convert.ToDecimal(...) is failing because of invalid input parameters.

The value of either location[0] or location[1] is not what you expect it to be (if this is where the exception occurs).

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