我是新来的C#我想知道如何从数据库中检索一个整数值并将其存储在一个变量中。检索int数据库并将其存储在一个变量中C#
之后,我需要使用此值来添加另一个已完成的整数并将其存回数据库。
我已经搜索了很多方法,但大多数给出的方法将它存储在数据网格视图中,但我想在不显示的情况下进行计算。
SqlDataAdapter sql1 = new SqlDataAdapter("Select finaldays from LeaveTest where Id = '" + comboBox1.SelectedText + "'", con);//it will take from the selection of combobox
DataTable dt = new DataTable();
sql1.Fill(dt);
con.Open();
TimeSpan timespan1;
timespan1 = dateTimePicker3.Value - dateTimePicker2.Value;
int TotalDays = timespan1.Days+1;//total days that are taken from the datetimepicker
MessageBox.Show(TotalDays.ToString());//displaying the date for testing
我还没有添加vari能够为整数值,但让我们想要使用的变量是int = finaldays – Darren
[阅读此](https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using -parameters-in-sql-statements)当你得到一分钟 – musefan
用以下代码替换上面的代码。 SqlCommand com = new SqlCommand(“Select Leavedest from LeaveTest where Id ='”+ comboBox1.SelectedText +“'”,con); int x =(int)com.ExecuteScalar(); – Koderzzzz